2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 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 61266923 2020-01-14 stsp #include <signal.h>
28 9f7d7167 2018-04-29 stsp #include <stdlib.h>
29 26ed57b2 2018-05-19 stsp #include <stdio.h>
30 9f7d7167 2018-04-29 stsp #include <getopt.h>
31 9f7d7167 2018-04-29 stsp #include <string.h>
32 9f7d7167 2018-04-29 stsp #include <err.h>
33 80ddbec8 2018-04-29 stsp #include <unistd.h>
34 26ed57b2 2018-05-19 stsp #include <util.h>
35 26ed57b2 2018-05-19 stsp #include <limits.h>
36 61e69b96 2018-05-20 stsp #include <wchar.h>
37 788c352e 2018-06-16 stsp #include <time.h>
38 84451b3e 2018-07-10 stsp #include <pthread.h>
39 5036bf37 2018-09-24 stsp #include <libgen.h>
40 60493ae3 2019-06-20 stsp #include <regex.h>
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 9f7d7167 2018-04-29 stsp #include "got_error.h"
44 80ddbec8 2018-04-29 stsp #include "got_object.h"
45 80ddbec8 2018-04-29 stsp #include "got_reference.h"
46 80ddbec8 2018-04-29 stsp #include "got_repository.h"
47 80ddbec8 2018-04-29 stsp #include "got_diff.h"
48 511a516b 2018-05-19 stsp #include "got_opentemp.h"
49 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
50 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
51 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
52 a70480e0 2018-06-23 stsp #include "got_blame.h"
53 c2db6724 2019-01-04 stsp #include "got_privsep.h"
54 1dd54920 2019-05-11 stsp #include "got_path.h"
55 b7165be3 2019-02-05 stsp #include "got_worktree.h"
58 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
67 9f7d7167 2018-04-29 stsp #ifndef nitems
68 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 9f7d7167 2018-04-29 stsp struct tog_cmd {
72 c2301be8 2018-04-30 stsp const char *name;
73 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
74 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
77 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
78 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
79 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
81 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
83 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
84 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
85 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
86 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
89 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
90 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
91 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
92 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
95 d6b05b5b 2018-08-04 stsp enum tog_view_type {
96 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
97 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
98 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
99 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
102 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
104 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
105 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
106 ba4f502b 2018-08-04 stsp struct got_object_id *id;
107 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
110 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
111 ba4f502b 2018-08-04 stsp struct commit_queue {
112 ba4f502b 2018-08-04 stsp int ncommits;
113 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
116 f26dddb7 2019-11-08 stsp struct tog_color {
117 f26dddb7 2019-11-08 stsp SIMPLEQ_ENTRY(tog_color) entry;
118 6d17833f 2019-11-08 stsp regex_t regex;
119 6d17833f 2019-11-08 stsp short colorpair;
121 f26dddb7 2019-11-08 stsp SIMPLEQ_HEAD(tog_colors, tog_color);
123 11b20872 2019-11-08 stsp static const struct got_error *
124 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
125 11b20872 2019-11-08 stsp int idx, short color)
127 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
128 11b20872 2019-11-08 stsp struct tog_color *tc;
129 11b20872 2019-11-08 stsp int regerr = 0;
131 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
132 11b20872 2019-11-08 stsp return NULL;
134 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
136 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
137 11b20872 2019-11-08 stsp if (tc == NULL)
138 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
139 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
140 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
141 11b20872 2019-11-08 stsp if (regerr) {
142 11b20872 2019-11-08 stsp static char regerr_msg[512];
143 11b20872 2019-11-08 stsp static char err_msg[512];
144 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
145 11b20872 2019-11-08 stsp sizeof(regerr_msg));
146 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
147 11b20872 2019-11-08 stsp regerr_msg);
148 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
150 11b20872 2019-11-08 stsp return err;
152 11b20872 2019-11-08 stsp tc->colorpair = idx;
153 11b20872 2019-11-08 stsp SIMPLEQ_INSERT_HEAD(colors, tc, entry);
154 11b20872 2019-11-08 stsp return NULL;
157 11b20872 2019-11-08 stsp static void
158 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
160 11b20872 2019-11-08 stsp struct tog_color *tc;
162 11b20872 2019-11-08 stsp while (!SIMPLEQ_EMPTY(colors)) {
163 11b20872 2019-11-08 stsp tc = SIMPLEQ_FIRST(colors);
164 11b20872 2019-11-08 stsp SIMPLEQ_REMOVE_HEAD(colors, entry);
165 11b20872 2019-11-08 stsp regfree(&tc->regex);
170 11b20872 2019-11-08 stsp struct tog_color *
171 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
173 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
175 11b20872 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
176 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
180 11b20872 2019-11-08 stsp return NULL;
184 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
186 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
187 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
188 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
189 11b20872 2019-11-08 stsp return COLOR_CYAN;
190 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
191 11b20872 2019-11-08 stsp return COLOR_YELLOW;
192 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
193 11b20872 2019-11-08 stsp return COLOR_GREEN;
194 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
195 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
196 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
197 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
198 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
199 91b8c405 2020-01-25 stsp return COLOR_CYAN;
200 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
201 11b20872 2019-11-08 stsp return COLOR_GREEN;
202 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
203 11b20872 2019-11-08 stsp return COLOR_GREEN;
204 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
205 11b20872 2019-11-08 stsp return COLOR_CYAN;
206 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
207 11b20872 2019-11-08 stsp return COLOR_YELLOW;
213 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
215 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
217 11b20872 2019-11-08 stsp if (val == NULL)
218 11b20872 2019-11-08 stsp return default_color_value(envvar);
220 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
221 11b20872 2019-11-08 stsp return COLOR_BLACK;
222 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
223 11b20872 2019-11-08 stsp return COLOR_RED;
224 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
225 11b20872 2019-11-08 stsp return COLOR_GREEN;
226 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
227 11b20872 2019-11-08 stsp return COLOR_YELLOW;
228 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
229 11b20872 2019-11-08 stsp return COLOR_BLUE;
230 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
231 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
232 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
233 11b20872 2019-11-08 stsp return COLOR_CYAN;
234 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
235 11b20872 2019-11-08 stsp return COLOR_WHITE;
236 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
239 11b20872 2019-11-08 stsp return default_color_value(envvar);
243 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
244 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
246 15a087fe 2019-02-21 stsp int first_displayed_line;
247 15a087fe 2019-02-21 stsp int last_displayed_line;
249 15a087fe 2019-02-21 stsp int diff_context;
250 15a087fe 2019-02-21 stsp struct got_repository *repo;
251 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
252 bddb1296 2019-11-08 stsp struct tog_colors colors;
253 f44b1f58 2020-02-02 tracey int nlines;
254 f44b1f58 2020-02-02 tracey off_t *line_offsets;
255 f44b1f58 2020-02-02 tracey int matched_line;
256 f44b1f58 2020-02-02 tracey int selected_line;
257 f44b1f58 2020-02-02 tracey size_t filesize;
259 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
260 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
263 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
265 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
266 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
267 1a76625f 2018-10-22 stsp int commits_needed;
268 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
269 1a76625f 2018-10-22 stsp struct commit_queue *commits;
270 1a76625f 2018-10-22 stsp const char *in_repo_path;
271 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
272 1a76625f 2018-10-22 stsp struct got_repository *repo;
273 1a76625f 2018-10-22 stsp int log_complete;
274 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
275 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
276 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
277 13add988 2019-10-15 stsp int *searching;
278 13add988 2019-10-15 stsp int *search_next_done;
279 13add988 2019-10-15 stsp regex_t *regex;
282 1a76625f 2018-10-22 stsp struct tog_log_view_state {
283 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
284 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
285 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
286 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
287 b01e7d3b 2018-08-04 stsp int selected;
288 b01e7d3b 2018-08-04 stsp char *in_repo_path;
289 d01904d4 2019-06-25 stsp const char *head_ref_name;
290 b672a97a 2020-01-27 stsp int log_branches;
291 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
292 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
293 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
294 1a76625f 2018-10-22 stsp sig_atomic_t quit;
295 1a76625f 2018-10-22 stsp pthread_t thread;
296 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
297 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
298 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
299 11b20872 2019-11-08 stsp struct tog_colors colors;
302 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
303 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
304 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
305 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
306 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
307 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
308 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
309 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
310 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
311 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
312 11b20872 2019-11-08 stsp #define TOG_COLOR_DATE 11
314 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
315 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
316 e9424729 2018-08-04 stsp int nlines;
318 e9424729 2018-08-04 stsp struct tog_view *view;
319 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
323 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
324 e9424729 2018-08-04 stsp const char *path;
325 e9424729 2018-08-04 stsp struct got_repository *repo;
326 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
327 e9424729 2018-08-04 stsp int *complete;
328 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
329 fc06ba56 2019-08-22 stsp void *cancel_arg;
332 e9424729 2018-08-04 stsp struct tog_blame {
334 e9424729 2018-08-04 stsp size_t filesize;
335 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
336 6fcac457 2018-11-19 stsp int nlines;
337 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
338 e9424729 2018-08-04 stsp pthread_t thread;
339 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
340 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
341 e9424729 2018-08-04 stsp const char *path;
344 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
345 7cbe629d 2018-08-04 stsp int first_displayed_line;
346 7cbe629d 2018-08-04 stsp int last_displayed_line;
347 7cbe629d 2018-08-04 stsp int selected_line;
348 7cbe629d 2018-08-04 stsp int blame_complete;
351 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
352 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
353 e5a0f69f 2018-08-18 stsp char *path;
354 7cbe629d 2018-08-04 stsp struct got_repository *repo;
355 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
356 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
357 e9424729 2018-08-04 stsp struct tog_blame blame;
358 6c4c42e0 2019-06-24 stsp int matched_line;
359 11b20872 2019-11-08 stsp struct tog_colors colors;
362 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
363 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
364 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
365 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
366 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
367 ad80ab7b 2018-08-04 stsp int selected;
370 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
372 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
373 ad80ab7b 2018-08-04 stsp char *tree_label;
374 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
375 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
376 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
377 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
378 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
379 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
380 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
381 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
382 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
383 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
384 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
385 bddb1296 2019-11-08 stsp struct tog_colors colors;
389 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
391 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
392 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
393 669b5ffa 2018-10-07 stsp * there is enough screen estate.
395 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
396 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
398 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
399 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
400 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
402 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
403 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
405 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
407 cc3c9aac 2018-08-01 stsp struct tog_view {
408 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
409 80ddbec8 2018-04-29 stsp WINDOW *window;
410 80ddbec8 2018-04-29 stsp PANEL *panel;
411 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
412 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
413 1004088d 2018-09-29 stsp int focussed;
414 669b5ffa 2018-10-07 stsp struct tog_view *parent;
415 669b5ffa 2018-10-07 stsp struct tog_view *child;
416 669b5ffa 2018-10-07 stsp int child_focussed;
418 5dc9f4bc 2018-08-04 stsp /* type-specific state */
419 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
421 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
422 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
423 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
424 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
427 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
428 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
429 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
430 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
432 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
433 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
434 60493ae3 2019-06-20 stsp int searching;
435 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
436 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
437 60493ae3 2019-06-20 stsp int search_next_done;
438 1803e47f 2019-06-22 stsp regex_t regex;
441 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
442 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
443 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
444 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
445 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
446 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
447 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
448 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
449 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
451 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
452 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
453 b672a97a 2020-01-27 stsp struct got_repository *, const char *, const char *, int, int);
454 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
455 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
456 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
457 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
458 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
459 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
461 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
462 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
463 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
464 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
465 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
466 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
467 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
468 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
470 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
471 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
472 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
473 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
474 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
475 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
476 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
477 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
478 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
480 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
481 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
482 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
484 25791caa 2018-10-24 stsp static void
485 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
487 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
490 83baff54 2019-08-12 stsp static void
491 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
493 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
496 61266923 2020-01-14 stsp static void
497 61266923 2020-01-14 stsp tog_sigcont(int signo)
499 61266923 2020-01-14 stsp tog_sigcont_received = 1;
502 e5a0f69f 2018-08-18 stsp static const struct got_error *
503 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
505 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
507 669b5ffa 2018-10-07 stsp if (view->child) {
508 669b5ffa 2018-10-07 stsp view_close(view->child);
509 669b5ffa 2018-10-07 stsp view->child = NULL;
511 e5a0f69f 2018-08-18 stsp if (view->close)
512 e5a0f69f 2018-08-18 stsp err = view->close(view);
513 ea5e7bb5 2018-08-01 stsp if (view->panel)
514 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
515 ea5e7bb5 2018-08-01 stsp if (view->window)
516 ea5e7bb5 2018-08-01 stsp delwin(view->window);
517 ea5e7bb5 2018-08-01 stsp free(view);
518 e5a0f69f 2018-08-18 stsp return err;
521 ea5e7bb5 2018-08-01 stsp static struct tog_view *
522 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
523 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
525 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
527 ea5e7bb5 2018-08-01 stsp if (view == NULL)
528 ea5e7bb5 2018-08-01 stsp return NULL;
530 d6b05b5b 2018-08-04 stsp view->type = type;
531 f7d12f7e 2018-08-01 stsp view->lines = LINES;
532 f7d12f7e 2018-08-01 stsp view->cols = COLS;
533 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
534 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
535 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
536 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
537 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
538 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
539 96a765a8 2018-08-04 stsp view_close(view);
540 ea5e7bb5 2018-08-01 stsp return NULL;
542 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
543 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
544 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
545 96a765a8 2018-08-04 stsp view_close(view);
546 ea5e7bb5 2018-08-01 stsp return NULL;
549 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
550 ea5e7bb5 2018-08-01 stsp return view;
554 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
556 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
558 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
561 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
563 4d8c2215 2018-08-19 stsp static const struct got_error *
564 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
566 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
568 5c60c32a 2018-10-18 stsp view->begin_y = 0;
569 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
570 5c60c32a 2018-10-18 stsp view->nlines = LINES;
571 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
572 5c60c32a 2018-10-18 stsp view->lines = LINES;
573 5c60c32a 2018-10-18 stsp view->cols = COLS;
574 5c60c32a 2018-10-18 stsp err = view_resize(view);
576 5c60c32a 2018-10-18 stsp return err;
578 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
579 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
581 5c60c32a 2018-10-18 stsp return NULL;
584 5c60c32a 2018-10-18 stsp static const struct got_error *
585 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
587 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
589 5c60c32a 2018-10-18 stsp view->begin_x = 0;
590 5c60c32a 2018-10-18 stsp view->begin_y = 0;
591 5c60c32a 2018-10-18 stsp view->nlines = LINES;
592 5c60c32a 2018-10-18 stsp view->ncols = COLS;
593 5c60c32a 2018-10-18 stsp view->lines = LINES;
594 5c60c32a 2018-10-18 stsp view->cols = COLS;
595 5c60c32a 2018-10-18 stsp err = view_resize(view);
597 5c60c32a 2018-10-18 stsp return err;
599 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
600 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
602 5c60c32a 2018-10-18 stsp return NULL;
606 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
608 5c60c32a 2018-10-18 stsp return view->parent == NULL;
611 5c60c32a 2018-10-18 stsp static const struct got_error *
612 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
614 f7d12f7e 2018-08-01 stsp int nlines, ncols;
616 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
617 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
619 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
621 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
622 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
624 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
626 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
627 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
628 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
629 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
630 25791caa 2018-10-24 stsp wclear(view->window);
632 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
633 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
634 0cf4efb1 2018-09-29 stsp view->lines = LINES;
635 0cf4efb1 2018-09-29 stsp view->cols = COLS;
637 6e3e5d9c 2018-10-18 stsp if (view->child) {
638 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
639 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
640 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
641 5c60c32a 2018-10-18 stsp if (view->child->focussed)
642 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
644 5c60c32a 2018-10-18 stsp show_panel(view->panel);
646 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
647 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
651 0cf4efb1 2018-09-29 stsp return NULL;
654 669b5ffa 2018-10-07 stsp static const struct got_error *
655 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
657 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
659 669b5ffa 2018-10-07 stsp if (view->child == NULL)
660 669b5ffa 2018-10-07 stsp return NULL;
662 669b5ffa 2018-10-07 stsp err = view_close(view->child);
663 669b5ffa 2018-10-07 stsp view->child = NULL;
664 669b5ffa 2018-10-07 stsp return err;
667 669b5ffa 2018-10-07 stsp static const struct got_error *
668 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
670 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
672 669b5ffa 2018-10-07 stsp view->child = child;
673 669b5ffa 2018-10-07 stsp child->parent = view;
674 669b5ffa 2018-10-07 stsp return err;
678 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
680 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
683 25791caa 2018-10-24 stsp static void
684 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
686 25791caa 2018-10-24 stsp int cols, lines;
687 25791caa 2018-10-24 stsp struct winsize size;
689 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
690 25791caa 2018-10-24 stsp cols = 80; /* Default */
691 25791caa 2018-10-24 stsp lines = 24;
693 25791caa 2018-10-24 stsp cols = size.ws_col;
694 25791caa 2018-10-24 stsp lines = size.ws_row;
696 25791caa 2018-10-24 stsp resize_term(lines, cols);
699 0cf4efb1 2018-09-29 stsp static const struct got_error *
700 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
702 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
703 2b49a8ae 2019-06-22 stsp char pattern[1024];
705 7d049c18 2019-08-21 stsp int begin_x = 0;
707 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
708 2b49a8ae 2019-06-22 stsp return NULL;
710 7d049c18 2019-08-21 stsp if (!view_is_parent_view(view))
711 7d049c18 2019-08-21 stsp begin_x = view_split_begin_x(view->begin_x);
712 2b49a8ae 2019-06-22 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
713 7d049c18 2019-08-21 stsp begin_x, "/");
714 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
716 2b49a8ae 2019-06-22 stsp nocbreak();
718 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
721 2b49a8ae 2019-06-22 stsp if (ret == ERR)
722 2b49a8ae 2019-06-22 stsp return NULL;
724 2b49a8ae 2019-06-22 stsp if (view->searching) {
725 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
726 2b49a8ae 2019-06-22 stsp view->searching = 0;
729 2b49a8ae 2019-06-22 stsp if (regcomp(&view->regex, pattern,
730 f5daf9b1 2019-06-24 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
731 7c32bd05 2019-06-22 stsp err = view->search_start(view);
733 7c32bd05 2019-06-22 stsp regfree(&view->regex);
734 7c32bd05 2019-06-22 stsp return err;
736 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
737 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
738 2b49a8ae 2019-06-22 stsp view->search_next(view);
741 2b49a8ae 2019-06-22 stsp return NULL;
744 2b49a8ae 2019-06-22 stsp static const struct got_error *
745 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
746 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
747 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
749 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
750 669b5ffa 2018-10-07 stsp struct tog_view *v;
751 1a76625f 2018-10-22 stsp int ch, errcode;
753 e5a0f69f 2018-08-18 stsp *new = NULL;
754 e5a0f69f 2018-08-18 stsp *dead = NULL;
755 0cf4efb1 2018-09-29 stsp *focus = NULL;
757 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
758 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
759 82954512 2020-02-03 stsp if (errcode)
760 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
761 82954512 2020-02-03 stsp "pthread_mutex_unlock");
762 82954512 2020-02-03 stsp pthread_yield();
763 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
764 82954512 2020-02-03 stsp if (errcode)
765 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
766 82954512 2020-02-03 stsp "pthread_mutex_lock");
767 60493ae3 2019-06-20 stsp view->search_next(view);
768 60493ae3 2019-06-20 stsp return NULL;
771 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
772 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
773 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
774 1a76625f 2018-10-22 stsp if (errcode)
775 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
776 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
777 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
778 1a76625f 2018-10-22 stsp if (errcode)
779 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
780 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
782 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
783 25791caa 2018-10-24 stsp tog_resizeterm();
784 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
785 61266923 2020-01-14 stsp tog_sigcont_received = 0;
786 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
787 25791caa 2018-10-24 stsp err = view_resize(v);
789 25791caa 2018-10-24 stsp return err;
790 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
792 25791caa 2018-10-24 stsp return err;
796 e5a0f69f 2018-08-18 stsp switch (ch) {
800 1e37a5c2 2019-05-12 jcs if (view->child) {
801 1e37a5c2 2019-05-12 jcs *focus = view->child;
802 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
803 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
804 1e37a5c2 2019-05-12 jcs *focus = view->parent;
805 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
809 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
810 1e37a5c2 2019-05-12 jcs *dead = view;
816 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
817 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
819 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
820 669b5ffa 2018-10-07 stsp *focus = view->child;
821 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
822 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
824 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
827 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
828 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
830 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
831 1e37a5c2 2019-05-12 jcs *focus = view;
832 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
833 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
835 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
839 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
840 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
843 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
846 60493ae3 2019-06-20 stsp if (view->search_start)
847 2b49a8ae 2019-06-22 stsp view_search_start(view);
849 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
853 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
854 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
855 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
856 60493ae3 2019-06-20 stsp view->search_next_done = 0;
857 60493ae3 2019-06-20 stsp view->search_next(view);
859 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
862 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
866 e5a0f69f 2018-08-18 stsp return err;
870 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
872 0cf4efb1 2018-09-29 stsp PANEL *panel;
873 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
875 669b5ffa 2018-10-07 stsp if (view->parent)
876 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
878 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
879 0cf4efb1 2018-09-29 stsp if (panel == NULL)
882 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
883 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
884 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
888 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
890 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
891 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
893 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
895 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
898 669b5ffa 2018-10-07 stsp return view->focussed;
901 bcbd79e2 2018-08-19 stsp static const struct got_error *
902 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
904 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
905 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
906 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
907 fd823528 2018-10-22 stsp int fast_refresh = 10;
908 1a76625f 2018-10-22 stsp int done = 0, errcode;
910 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
911 1a76625f 2018-10-22 stsp if (errcode)
912 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
914 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
915 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
917 a81bf10d 2018-09-29 stsp main_view = view;
918 1004088d 2018-09-29 stsp view->focussed = 1;
919 878940b7 2018-09-29 stsp err = view->show(view);
921 0cf4efb1 2018-09-29 stsp return err;
922 0cf4efb1 2018-09-29 stsp update_panels();
923 0cf4efb1 2018-09-29 stsp doupdate();
924 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
925 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
926 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
927 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
929 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
930 e4197bf9 2018-08-18 stsp view, &views);
933 e5a0f69f 2018-08-18 stsp if (dead_view) {
934 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
936 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
937 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
938 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
939 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
940 669b5ffa 2018-10-07 stsp prev = view->parent;
942 669b5ffa 2018-10-07 stsp if (dead_view->parent)
943 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
945 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
947 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
948 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
951 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
952 0cf4efb1 2018-09-29 stsp if (focus_view)
953 0cf4efb1 2018-09-29 stsp view = focus_view;
954 669b5ffa 2018-10-07 stsp else if (prev)
955 669b5ffa 2018-10-07 stsp view = prev;
956 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
957 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
958 0cf4efb1 2018-09-29 stsp tog_view_list_head);
960 0cf4efb1 2018-09-29 stsp view = NULL;
961 669b5ffa 2018-10-07 stsp if (view) {
962 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
963 669b5ffa 2018-10-07 stsp focus_view = view->child;
965 669b5ffa 2018-10-07 stsp focus_view = view;
969 bcbd79e2 2018-08-19 stsp if (new_view) {
970 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
971 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
972 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
973 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
975 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
976 86c66b02 2018-10-18 stsp err = view_close(v);
981 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
982 fed7eaa8 2018-10-24 stsp view = new_view;
983 878940b7 2018-09-29 stsp if (focus_view == NULL)
984 878940b7 2018-09-29 stsp focus_view = new_view;
986 0cf4efb1 2018-09-29 stsp if (focus_view) {
987 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
989 0cf4efb1 2018-09-29 stsp view->focussed = 0;
990 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
991 0cf4efb1 2018-09-29 stsp view = focus_view;
992 878940b7 2018-09-29 stsp if (new_view)
993 878940b7 2018-09-29 stsp show_panel(new_view->panel);
994 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
995 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
997 669b5ffa 2018-10-07 stsp if (view) {
998 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
999 758194b5 2018-10-24 stsp view->focussed = 1;
1000 758194b5 2018-10-24 stsp show_panel(view->panel);
1001 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
1002 758194b5 2018-10-24 stsp show_panel(view->child->panel);
1003 1a76625f 2018-10-22 stsp focus_view = view;
1005 669b5ffa 2018-10-07 stsp if (view->parent) {
1006 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1008 1a76625f 2018-10-22 stsp goto done;
1010 669b5ffa 2018-10-07 stsp err = view->show(view);
1012 1a76625f 2018-10-22 stsp goto done;
1013 669b5ffa 2018-10-07 stsp if (view->child) {
1014 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1016 1a76625f 2018-10-22 stsp goto done;
1018 1a76625f 2018-10-22 stsp update_panels();
1019 1a76625f 2018-10-22 stsp doupdate();
1023 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1024 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1025 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1026 e5a0f69f 2018-08-18 stsp view_close(view);
1029 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1030 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1031 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1033 e5a0f69f 2018-08-18 stsp return err;
1036 4ed7e80c 2018-05-20 stsp __dead static void
1037 9f7d7167 2018-04-29 stsp usage_log(void)
1040 c70c5802 2018-08-01 stsp fprintf(stderr,
1041 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1042 9f7d7167 2018-04-29 stsp getprogname());
1046 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1047 80ddbec8 2018-04-29 stsp static const struct got_error *
1048 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1050 00dfcb92 2018-06-11 stsp char *vis = NULL;
1051 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1053 963b370f 2018-05-20 stsp *ws = NULL;
1054 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1055 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1056 00dfcb92 2018-06-11 stsp int vislen;
1057 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1058 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1060 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1061 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1063 00dfcb92 2018-06-11 stsp return err;
1064 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1065 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1066 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1067 a7f50699 2018-06-11 stsp goto done;
1071 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1072 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1073 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1074 a7f50699 2018-06-11 stsp goto done;
1077 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1078 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1080 00dfcb92 2018-06-11 stsp free(vis);
1081 963b370f 2018-05-20 stsp if (err) {
1082 963b370f 2018-05-20 stsp free(*ws);
1083 963b370f 2018-05-20 stsp *ws = NULL;
1084 963b370f 2018-05-20 stsp *wlen = 0;
1086 963b370f 2018-05-20 stsp return err;
1089 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1090 963b370f 2018-05-20 stsp static const struct got_error *
1091 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1092 27a741e5 2019-09-11 stsp int col_tab_align)
1094 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1095 963b370f 2018-05-20 stsp int cols = 0;
1096 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1097 963b370f 2018-05-20 stsp size_t wlen;
1100 963b370f 2018-05-20 stsp *wlinep = NULL;
1101 b700b5d6 2018-07-10 stsp *widthp = 0;
1103 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1105 963b370f 2018-05-20 stsp return err;
1108 27a741e5 2019-09-11 stsp while (i < wlen) {
1109 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1111 27a741e5 2019-09-11 stsp if (width == 0) {
1116 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1117 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1119 27a741e5 2019-09-11 stsp cols += width;
1121 27a741e5 2019-09-11 stsp } else if (width == -1) {
1122 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1123 27a741e5 2019-09-11 stsp width = TABSIZE -
1124 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1125 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1127 b700b5d6 2018-07-10 stsp cols += width;
1131 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1132 963b370f 2018-05-20 stsp goto done;
1135 963b370f 2018-05-20 stsp wline[i] = L'\0';
1136 b700b5d6 2018-07-10 stsp if (widthp)
1137 b700b5d6 2018-07-10 stsp *widthp = cols;
1140 963b370f 2018-05-20 stsp free(wline);
1142 963b370f 2018-05-20 stsp *wlinep = wline;
1143 963b370f 2018-05-20 stsp return err;
1146 8b473291 2019-02-21 stsp static const struct got_error*
1147 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1148 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1150 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1151 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1153 8b473291 2019-02-21 stsp const char *name;
1155 8b473291 2019-02-21 stsp *refs_str = NULL;
1157 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1158 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1161 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1162 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1164 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1165 8b473291 2019-02-21 stsp name += 5;
1166 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1168 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1169 8b473291 2019-02-21 stsp name += 6;
1170 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
1171 8b473291 2019-02-21 stsp name += 8;
1172 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1173 52b5abe1 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1174 5d844a1e 2019-08-13 stsp if (err) {
1175 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1177 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1178 5d844a1e 2019-08-13 stsp err = NULL;
1179 5d844a1e 2019-08-13 stsp tag = NULL;
1182 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1183 52b5abe1 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1185 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1186 52b5abe1 2019-08-13 stsp if (cmp != 0)
1188 8b473291 2019-02-21 stsp s = *refs_str;
1189 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1190 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1191 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1193 8b473291 2019-02-21 stsp *refs_str = NULL;
1199 8b473291 2019-02-21 stsp return err;
1202 963b370f 2018-05-20 stsp static const struct got_error *
1203 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1204 27a741e5 2019-09-11 stsp int col_tab_align)
1206 5813d178 2019-03-09 stsp char *smallerthan, *at;
1208 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1209 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1210 5813d178 2019-03-09 stsp author = smallerthan + 1;
1211 5813d178 2019-03-09 stsp at = strchr(author, '@');
1213 5813d178 2019-03-09 stsp *at = '\0';
1214 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1217 5813d178 2019-03-09 stsp static const struct got_error *
1218 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1219 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1220 11b20872 2019-11-08 stsp const size_t date_display_cols, int author_display_cols,
1221 11b20872 2019-11-08 stsp struct tog_colors *colors)
1223 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1224 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1225 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1226 5813d178 2019-03-09 stsp char *author = NULL;
1227 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1228 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1229 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1230 bb737323 2018-05-20 stsp int col, limit;
1231 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1232 ccb26ccd 2018-11-05 stsp struct tm tm;
1233 45d799e2 2018-12-23 stsp time_t committer_time;
1234 11b20872 2019-11-08 stsp struct tog_color *tc;
1236 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1237 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1238 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1239 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1240 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1241 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1243 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1244 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1246 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1247 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_DATE);
1249 11b20872 2019-11-08 stsp wattr_on(view->window,
1250 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1251 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1253 11b20872 2019-11-08 stsp wattr_off(view->window,
1254 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1255 27a741e5 2019-09-11 stsp col = limit;
1256 b39d25c7 2018-07-10 stsp if (col > avail)
1257 b39d25c7 2018-07-10 stsp goto done;
1259 6570a66d 2019-11-08 stsp if (avail >= 120) {
1260 6570a66d 2019-11-08 stsp char *id_str;
1261 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1263 6570a66d 2019-11-08 stsp goto done;
1264 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1266 11b20872 2019-11-08 stsp wattr_on(view->window,
1267 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1268 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1270 11b20872 2019-11-08 stsp wattr_off(view->window,
1271 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1272 6570a66d 2019-11-08 stsp free(id_str);
1274 6570a66d 2019-11-08 stsp if (col > avail)
1275 6570a66d 2019-11-08 stsp goto done;
1278 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1279 5813d178 2019-03-09 stsp if (author == NULL) {
1280 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1281 80ddbec8 2018-04-29 stsp goto done;
1283 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1285 bb737323 2018-05-20 stsp goto done;
1286 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_AUTHOR);
1288 11b20872 2019-11-08 stsp wattr_on(view->window,
1289 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1290 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1292 11b20872 2019-11-08 stsp wattr_off(view->window,
1293 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1294 bb737323 2018-05-20 stsp col += author_width;
1295 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1296 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1298 bb737323 2018-05-20 stsp author_width++;
1300 9c2eaf34 2018-05-20 stsp if (col > avail)
1301 9c2eaf34 2018-05-20 stsp goto done;
1303 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1305 6d9fbc00 2018-04-29 stsp goto done;
1306 bb737323 2018-05-20 stsp logmsg = logmsg0;
1307 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1309 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1310 bb737323 2018-05-20 stsp if (newline)
1311 bb737323 2018-05-20 stsp *newline = '\0';
1312 bb737323 2018-05-20 stsp limit = avail - col;
1313 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1315 bb737323 2018-05-20 stsp goto done;
1316 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1317 bb737323 2018-05-20 stsp col += logmsg_width;
1318 27a741e5 2019-09-11 stsp while (col < avail) {
1319 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1323 80ddbec8 2018-04-29 stsp free(logmsg0);
1324 bb737323 2018-05-20 stsp free(wlogmsg);
1325 5813d178 2019-03-09 stsp free(author);
1326 bb737323 2018-05-20 stsp free(wauthor);
1327 80ddbec8 2018-04-29 stsp free(line);
1328 80ddbec8 2018-04-29 stsp return err;
1331 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1332 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1333 899d86c2 2018-05-10 stsp struct got_object_id *id)
1335 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1337 99db9666 2018-05-07 stsp entry = calloc(1, sizeof(*entry));
1338 99db9666 2018-05-07 stsp if (entry == NULL)
1339 899d86c2 2018-05-10 stsp return NULL;
1341 899d86c2 2018-05-10 stsp entry->id = id;
1342 99db9666 2018-05-07 stsp entry->commit = commit;
1343 899d86c2 2018-05-10 stsp return entry;
1346 99db9666 2018-05-07 stsp static void
1347 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1349 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1351 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1352 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1353 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1354 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1355 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1356 99db9666 2018-05-07 stsp free(entry);
1359 99db9666 2018-05-07 stsp static void
1360 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1362 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1363 99db9666 2018-05-07 stsp pop_commit(commits);
1366 99db9666 2018-05-07 stsp static const struct got_error *
1367 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1368 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1370 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1371 13add988 2019-10-15 stsp regmatch_t regmatch;
1372 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1374 13add988 2019-10-15 stsp *have_match = 0;
1376 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1378 13add988 2019-10-15 stsp return err;
1380 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1382 13add988 2019-10-15 stsp goto done;
1384 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1385 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1386 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1387 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1388 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1389 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1390 13add988 2019-10-15 stsp *have_match = 1;
1392 13add988 2019-10-15 stsp free(id_str);
1393 13add988 2019-10-15 stsp free(logmsg);
1394 13add988 2019-10-15 stsp return err;
1397 13add988 2019-10-15 stsp static const struct got_error *
1398 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1399 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1400 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1402 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1403 13add988 2019-10-15 stsp int nqueued = 0, have_match = 0;
1406 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1407 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1408 1a76625f 2018-10-22 stsp * while updating the display.
1410 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1411 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1412 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1413 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1414 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1415 1a76625f 2018-10-22 stsp int errcode;
1417 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1418 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1421 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1424 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1425 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1426 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1430 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1431 1a76625f 2018-10-22 stsp if (errcode) {
1432 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1433 13add988 2019-10-15 stsp "pthread_mutex_lock");
1437 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1438 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1439 ecb28ae0 2018-07-16 stsp nqueued++;
1440 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1442 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1443 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1446 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1447 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1448 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1449 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1451 a93813fa 2020-01-20 mpi if (err || have_match)
1455 9ba79e04 2018-06-11 stsp return err;
1458 9ba79e04 2018-06-11 stsp static const struct got_error *
1459 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1460 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1461 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1462 11b20872 2019-11-08 stsp struct got_reflist_head *refs, const char *path, int commits_needed,
1463 11b20872 2019-11-08 stsp struct tog_colors *colors)
1465 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1466 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1467 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry;
1468 60493ae3 2019-06-20 stsp int width;
1469 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1470 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1471 8b473291 2019-02-21 stsp char *refs_str = NULL;
1472 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1473 11b20872 2019-11-08 stsp struct tog_color *tc;
1474 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1476 e0d42f60 2018-07-22 stsp entry = first;
1477 e0d42f60 2018-07-22 stsp ncommits = 0;
1478 e0d42f60 2018-07-22 stsp while (entry) {
1479 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1480 e0d42f60 2018-07-22 stsp *selected = entry;
1483 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1484 e0d42f60 2018-07-22 stsp ncommits++;
1487 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1488 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1490 1a76625f 2018-10-22 stsp return err;
1491 8b473291 2019-02-21 stsp if (refs) {
1492 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1495 8b473291 2019-02-21 stsp goto done;
1499 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1500 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1502 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1503 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1504 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1505 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1506 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1507 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1508 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1509 8b473291 2019-02-21 stsp goto done;
1512 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
1513 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1514 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1515 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1516 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1517 1a76625f 2018-10-22 stsp header = NULL;
1518 1a76625f 2018-10-22 stsp goto done;
1520 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1521 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1522 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1523 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1524 1a76625f 2018-10-22 stsp header = NULL;
1525 1a76625f 2018-10-22 stsp goto done;
1527 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1529 1a76625f 2018-10-22 stsp goto done;
1531 2814baeb 2018-08-01 stsp werase(view->window);
1533 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1534 a3404814 2018-09-02 stsp wstandout(view->window);
1535 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1537 11b20872 2019-11-08 stsp wattr_on(view->window,
1538 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1539 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1541 11b20872 2019-11-08 stsp wattr_off(view->window,
1542 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1543 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1544 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1547 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1548 a3404814 2018-09-02 stsp wstandend(view->window);
1549 ecb28ae0 2018-07-16 stsp free(wline);
1550 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1551 1a76625f 2018-10-22 stsp goto done;
1553 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1554 899d86c2 2018-05-10 stsp entry = first;
1555 5813d178 2019-03-09 stsp ncommits = 0;
1556 5813d178 2019-03-09 stsp while (entry) {
1557 5813d178 2019-03-09 stsp char *author;
1558 5813d178 2019-03-09 stsp wchar_t *wauthor;
1559 5813d178 2019-03-09 stsp int width;
1560 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1562 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1563 5813d178 2019-03-09 stsp if (author == NULL) {
1564 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1565 5813d178 2019-03-09 stsp goto done;
1567 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1568 27a741e5 2019-09-11 stsp date_display_cols);
1569 5813d178 2019-03-09 stsp if (author_cols < width)
1570 5813d178 2019-03-09 stsp author_cols = width;
1571 5813d178 2019-03-09 stsp free(wauthor);
1572 5813d178 2019-03-09 stsp free(author);
1573 7ca04879 2019-10-19 stsp ncommits++;
1574 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1577 5813d178 2019-03-09 stsp entry = first;
1578 899d86c2 2018-05-10 stsp *last = first;
1579 867c6645 2018-07-10 stsp ncommits = 0;
1580 899d86c2 2018-05-10 stsp while (entry) {
1581 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1583 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1584 2814baeb 2018-08-01 stsp wstandout(view->window);
1585 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1586 11b20872 2019-11-08 stsp date_display_cols, author_cols, colors);
1587 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1588 2814baeb 2018-08-01 stsp wstandend(view->window);
1590 60493ae3 2019-06-20 stsp goto done;
1591 899d86c2 2018-05-10 stsp ncommits++;
1592 899d86c2 2018-05-10 stsp *last = entry;
1593 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1596 1a57306a 2018-09-02 stsp view_vborder(view);
1598 1a76625f 2018-10-22 stsp free(id_str);
1599 8b473291 2019-02-21 stsp free(refs_str);
1600 1a76625f 2018-10-22 stsp free(ncommits_str);
1601 1a76625f 2018-10-22 stsp free(header);
1602 899d86c2 2018-05-10 stsp return err;
1605 07b55e75 2018-05-10 stsp static void
1606 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1607 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1608 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1610 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1611 07b55e75 2018-05-10 stsp int nscrolled = 0;
1613 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1614 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1617 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1618 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1619 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1620 07b55e75 2018-05-10 stsp if (entry) {
1621 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1622 07b55e75 2018-05-10 stsp nscrolled++;
1627 899d86c2 2018-05-10 stsp static const struct got_error *
1628 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1629 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1631 5e224a3e 2019-02-22 stsp int errcode;
1632 8a42fca8 2019-02-22 stsp int max_wait = 20;
1634 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1636 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1637 5e224a3e 2019-02-22 stsp if (*log_complete)
1640 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1641 5e224a3e 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1642 5e224a3e 2019-02-22 stsp if (errcode)
1643 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1644 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1645 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1646 82954512 2020-02-03 stsp if (errcode)
1647 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1648 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1649 82954512 2020-02-03 stsp pthread_yield();
1650 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1651 82954512 2020-02-03 stsp if (errcode)
1652 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1653 82954512 2020-02-03 stsp "pthread_mutex_lock");
1655 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1657 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1658 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1659 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1660 5e224a3e 2019-02-22 stsp * with few commits in history.
1662 5e224a3e 2019-02-22 stsp return NULL;
1666 5e224a3e 2019-02-22 stsp return NULL;
1669 5e224a3e 2019-02-22 stsp static const struct got_error *
1670 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1671 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1672 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
1673 1a76625f 2018-10-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1674 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1676 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1677 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1678 aa075928 2018-05-10 stsp int nscrolled = 0;
1680 1a76625f 2018-10-22 stsp if (*last_displayed_entry == NULL)
1681 1a76625f 2018-10-22 stsp return NULL;
1683 7aafa0d1 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1684 7aafa0d1 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1686 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1687 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1689 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1690 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1691 5e224a3e 2019-02-22 stsp need_commits);
1693 5e224a3e 2019-02-22 stsp return err;
1697 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1698 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1701 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1703 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1704 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1706 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1707 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1709 dd0a52c1 2018-05-20 stsp return err;
1712 aa075928 2018-05-10 stsp static const struct got_error *
1713 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1714 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1715 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1716 8b473291 2019-02-21 stsp struct got_repository *repo)
1718 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1719 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1720 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1722 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1723 15a94983 2018-12-23 stsp if (diff_view == NULL)
1724 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1726 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1727 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1728 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1729 e5a0f69f 2018-08-18 stsp if (err == NULL)
1730 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1731 cd0acaa7 2018-05-20 stsp return err;
1734 cd0acaa7 2018-05-20 stsp static const struct got_error *
1735 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1736 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1738 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1740 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1741 941e9f74 2019-05-21 stsp if (parent == NULL)
1742 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1744 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1745 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1746 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1747 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1748 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1749 941e9f74 2019-05-21 stsp s->tree = subtree;
1750 941e9f74 2019-05-21 stsp s->selected = 0;
1751 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1752 941e9f74 2019-05-21 stsp return NULL;
1756 941e9f74 2019-05-21 stsp static const struct got_error *
1757 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1758 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1759 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1761 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1762 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1763 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1764 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1765 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1766 941e9f74 2019-05-21 stsp const char *p;
1768 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1769 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1771 9343a5fb 2018-06-23 stsp return err;
1773 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1774 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1775 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1777 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1778 941e9f74 2019-05-21 stsp if (err) {
1779 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1780 941e9f74 2019-05-21 stsp return err;
1782 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1784 941e9f74 2019-05-21 stsp *new_view = tree_view;
1786 33cbf02b 2020-01-12 stsp if (got_path_is_root_dir(path))
1787 33cbf02b 2020-01-12 stsp return NULL;
1789 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1791 941e9f74 2019-05-21 stsp while (*p) {
1792 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1793 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1794 56e0773d 2019-11-28 stsp char *te_name;
1796 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1799 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1800 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1801 941e9f74 2019-05-21 stsp if (slash == NULL)
1802 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1804 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1805 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1806 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1809 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1810 56e0773d 2019-11-28 stsp if (te == NULL) {
1811 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1812 56e0773d 2019-11-28 stsp free(te_name);
1815 56e0773d 2019-11-28 stsp free(te_name);
1816 56e0773d 2019-11-28 stsp s->selected_entry = te;
1817 56e0773d 2019-11-28 stsp s->selected = got_tree_entry_get_index(te);
1818 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1819 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1821 56e0773d 2019-11-28 stsp if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1822 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1823 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1824 67409a31 2019-05-24 stsp s->selected = 0;
1828 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1829 941e9f74 2019-05-21 stsp if (slash)
1830 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1832 941e9f74 2019-05-21 stsp subpath = strdup(path);
1833 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1834 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1838 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1843 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1844 941e9f74 2019-05-21 stsp free(tree_id);
1848 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1849 941e9f74 2019-05-21 stsp if (err) {
1850 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1853 941e9f74 2019-05-21 stsp if (slash == NULL)
1855 941e9f74 2019-05-21 stsp free(subpath);
1856 941e9f74 2019-05-21 stsp subpath = NULL;
1857 941e9f74 2019-05-21 stsp p = slash;
1860 941e9f74 2019-05-21 stsp free(subpath);
1861 9343a5fb 2018-06-23 stsp return err;
1864 61266923 2020-01-14 stsp static const struct got_error *
1865 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1867 61266923 2020-01-14 stsp sigset_t sigset;
1868 61266923 2020-01-14 stsp int errcode;
1870 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1871 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1873 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1874 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1875 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1876 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1877 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1879 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1880 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1881 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1883 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1884 61266923 2020-01-14 stsp if (errcode)
1885 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1887 61266923 2020-01-14 stsp return NULL;
1890 1a76625f 2018-10-22 stsp static void *
1891 1a76625f 2018-10-22 stsp log_thread(void *arg)
1893 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1894 1a76625f 2018-10-22 stsp int errcode = 0;
1895 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1896 1a76625f 2018-10-22 stsp int done = 0;
1898 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1900 61266923 2020-01-14 stsp return (void *)err;
1902 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1903 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1904 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1905 13add988 2019-10-15 stsp a->regex);
1906 1a76625f 2018-10-22 stsp if (err) {
1907 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1908 1a76625f 2018-10-22 stsp return (void *)err;
1909 1a76625f 2018-10-22 stsp err = NULL;
1911 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1912 1a76625f 2018-10-22 stsp a->commits_needed--;
1914 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1915 3abe8080 2019-04-10 stsp if (errcode) {
1916 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1917 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1919 3abe8080 2019-04-10 stsp } else if (*a->quit)
1921 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1922 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1923 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1924 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1928 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1929 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1930 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1931 1a76625f 2018-10-22 stsp &tog_mutex);
1932 1a76625f 2018-10-22 stsp if (errcode)
1933 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1934 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1937 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1938 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1939 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1940 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1942 3abe8080 2019-04-10 stsp a->log_complete = 1;
1943 1a76625f 2018-10-22 stsp return (void *)err;
1946 9343a5fb 2018-06-23 stsp static const struct got_error *
1947 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1949 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1950 1a76625f 2018-10-22 stsp int errcode;
1952 1a76625f 2018-10-22 stsp if (s->thread) {
1953 1a76625f 2018-10-22 stsp s->quit = 1;
1954 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1955 1a76625f 2018-10-22 stsp if (errcode)
1956 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1957 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1958 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1959 1a76625f 2018-10-22 stsp if (errcode)
1960 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1961 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1962 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1963 1a76625f 2018-10-22 stsp if (errcode)
1964 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1965 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1966 1a76625f 2018-10-22 stsp if (errcode)
1967 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1968 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1969 1a76625f 2018-10-22 stsp s->thread = NULL;
1972 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1973 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1974 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1976 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1977 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1978 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1981 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1982 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1983 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1986 1a76625f 2018-10-22 stsp return err;
1989 1a76625f 2018-10-22 stsp static const struct got_error *
1990 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1992 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1993 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1995 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1996 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1997 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1998 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1999 1a76625f 2018-10-22 stsp free(s->start_id);
2000 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2001 1a76625f 2018-10-22 stsp return err;
2004 1a76625f 2018-10-22 stsp static const struct got_error *
2005 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2007 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2009 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2010 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2011 60493ae3 2019-06-20 stsp return NULL;
2014 60493ae3 2019-06-20 stsp static const struct got_error *
2015 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2017 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2018 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2019 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2021 60493ae3 2019-06-20 stsp if (!view->searching) {
2022 60493ae3 2019-06-20 stsp view->search_next_done = 1;
2023 60493ae3 2019-06-20 stsp return NULL;
2026 96e2b566 2019-07-08 stsp if (s->search_entry) {
2027 13add988 2019-10-15 stsp int errcode, ch;
2028 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2029 13add988 2019-10-15 stsp if (errcode)
2030 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2031 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2032 13add988 2019-10-15 stsp ch = wgetch(view->window);
2033 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2034 13add988 2019-10-15 stsp if (errcode)
2035 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2036 13add988 2019-10-15 stsp "pthread_mutex_lock");
2037 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2038 678cbce5 2019-07-28 stsp view->search_next_done = 1;
2039 678cbce5 2019-07-28 stsp return NULL;
2041 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2042 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2044 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2045 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2046 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2047 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2048 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2050 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
2051 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2053 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2054 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2056 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2059 60493ae3 2019-06-20 stsp while (1) {
2060 13add988 2019-10-15 stsp int have_match = 0;
2062 60493ae3 2019-06-20 stsp if (entry == NULL) {
2063 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2064 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2065 f801134a 2019-06-25 stsp view->search_next_done = 1;
2066 f801134a 2019-06-25 stsp return NULL;
2069 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2070 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2071 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2073 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2074 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
2075 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
2076 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
2077 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
2080 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2081 13add988 2019-10-15 stsp &view->regex);
2084 13add988 2019-10-15 stsp if (have_match) {
2085 60493ae3 2019-06-20 stsp view->search_next_done = 1;
2086 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2090 96e2b566 2019-07-08 stsp s->search_entry = entry;
2091 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2092 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2094 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2097 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2098 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2099 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2100 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2102 60493ae3 2019-06-20 stsp return err;
2105 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2106 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2108 60493ae3 2019-06-20 stsp return err;
2113 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2115 60493ae3 2019-06-20 stsp return NULL;
2118 60493ae3 2019-06-20 stsp static const struct got_error *
2119 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2120 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
2121 b672a97a 2020-01-27 stsp const char *head_ref_name, const char *path, int check_disk,
2122 b672a97a 2020-01-27 stsp int log_branches)
2124 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2125 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2126 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2127 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2128 1a76625f 2018-10-22 stsp int errcode;
2130 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2131 ecb28ae0 2018-07-16 stsp if (err != NULL)
2132 ecb28ae0 2018-07-16 stsp goto done;
2134 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2135 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2136 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2138 8b473291 2019-02-21 stsp s->refs = refs;
2139 fb2756b9 2018-08-04 stsp s->repo = repo;
2140 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2141 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2142 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2143 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2144 5036bf37 2018-09-24 stsp goto done;
2146 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2148 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2149 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2150 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2151 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2153 11b20872 2019-11-08 stsp goto done;
2154 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2155 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2156 11b20872 2019-11-08 stsp if (err) {
2157 11b20872 2019-11-08 stsp free_colors(&s->colors);
2158 11b20872 2019-11-08 stsp goto done;
2160 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2161 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2162 11b20872 2019-11-08 stsp if (err) {
2163 11b20872 2019-11-08 stsp free_colors(&s->colors);
2164 11b20872 2019-11-08 stsp goto done;
2168 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2169 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2170 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2171 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2172 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2174 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2176 1a76625f 2018-10-22 stsp goto done;
2177 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2178 b672a97a 2020-01-27 stsp !s->log_branches);
2180 1a76625f 2018-10-22 stsp goto done;
2181 c5b78334 2020-01-12 stsp err = got_commit_graph_iter_start(thread_graph,
2182 c5b78334 2020-01-12 stsp s->start_id, s->repo, NULL, NULL);
2184 c5b78334 2020-01-12 stsp goto done;
2186 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2187 1a76625f 2018-10-22 stsp if (errcode) {
2188 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2189 1a76625f 2018-10-22 stsp goto done;
2192 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2193 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2194 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2195 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2196 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2197 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2198 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2199 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2200 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2201 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2202 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2203 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2204 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2207 1a76625f 2018-10-22 stsp close_log_view(view);
2208 ba4f502b 2018-08-04 stsp return err;
2211 e5a0f69f 2018-08-18 stsp static const struct got_error *
2212 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2214 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2216 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2217 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2218 2b380cc8 2018-10-24 stsp &s->thread_args);
2219 2b380cc8 2018-10-24 stsp if (errcode)
2220 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2223 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2224 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2225 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2226 11b20872 2019-11-08 stsp s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2229 0cf4efb1 2018-09-29 stsp static const struct got_error *
2230 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2231 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2233 ba4f502b 2018-08-04 stsp const struct got_error *err = NULL;
2234 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2235 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2236 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2237 669b5ffa 2018-10-07 stsp int begin_x = 0;
2238 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2240 e5a0f69f 2018-08-18 stsp switch (ch) {
2242 1e37a5c2 2019-05-12 jcs s->quit = 1;
2245 1e37a5c2 2019-05-12 jcs case KEY_UP:
2248 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2250 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2251 1e37a5c2 2019-05-12 jcs s->selected--;
2253 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2254 1144d21a 2019-06-21 stsp &s->commits);
2256 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2257 a4292ac5 2019-05-12 jcs case CTRL('b'):
2258 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2260 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2261 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2262 1e37a5c2 2019-05-12 jcs s->selected = 0;
2265 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2266 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
2269 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2272 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2274 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2275 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2276 1e37a5c2 2019-05-12 jcs s->selected++;
2279 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2280 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2281 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2282 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2283 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2285 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2286 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2287 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2288 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2289 1e37a5c2 2019-05-12 jcs if (first == NULL)
2291 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2292 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
2293 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2294 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2295 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2298 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2299 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2300 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2301 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2302 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2303 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2305 1e37a5c2 2019-05-12 jcs err = NULL;
2308 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2309 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2310 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2311 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2312 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2314 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2317 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2319 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2320 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2321 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2322 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2323 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2326 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2327 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2329 1e37a5c2 2019-05-12 jcs return err;
2330 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2332 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2335 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2336 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2338 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2341 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2343 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2344 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2345 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2346 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2349 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2350 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2352 1e37a5c2 2019-05-12 jcs return err;
2353 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2355 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2358 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2359 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2361 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2363 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2364 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2366 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2367 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2368 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2370 1e37a5c2 2019-05-12 jcs return err;
2371 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2372 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2373 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2374 638f9024 2019-05-13 stsp return got_error_from_errno(
2375 1e37a5c2 2019-05-12 jcs "view_open");
2376 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2377 b672a97a 2020-01-27 stsp s->repo, s->head_ref_name, parent_path, 0,
2378 b672a97a 2020-01-27 stsp s->log_branches);
2380 1e37a5c2 2019-05-12 jcs return err;;
2381 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2382 1e37a5c2 2019-05-12 jcs *new_view = lv;
2384 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2385 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2387 1e37a5c2 2019-05-12 jcs return NULL;
2390 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2391 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2393 d01904d4 2019-06-25 stsp return err;
2394 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2395 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2396 d01904d4 2019-06-25 stsp if (lv == NULL)
2397 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2398 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2399 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2400 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2401 ef129c5e 2019-08-03 stsp if (err) {
2402 ef129c5e 2019-08-03 stsp view_close(lv);
2403 d01904d4 2019-06-25 stsp return err;
2405 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2406 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2407 d01904d4 2019-06-25 stsp free(start_id);
2408 ef129c5e 2019-08-03 stsp view_close(lv);
2409 d01904d4 2019-06-25 stsp return got_error_from_errno("strdup");
2411 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2412 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2413 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2414 e6b23735 2019-10-04 stsp if (err) {
2415 e6b23735 2019-10-04 stsp free(start_id);
2416 e6b23735 2019-10-04 stsp view_close(lv);
2417 50284065 2019-10-04 stsp return err;
2419 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2420 b672a97a 2020-01-27 stsp s->head_ref_name, in_repo_path, 0, s->log_branches);
2421 ef129c5e 2019-08-03 stsp if (err) {
2422 ef129c5e 2019-08-03 stsp free(start_id);
2423 ef129c5e 2019-08-03 stsp view_close(lv);
2424 d01904d4 2019-06-25 stsp return err;;
2426 d01904d4 2019-06-25 stsp *dead_view = view;
2427 d01904d4 2019-06-25 stsp *new_view = lv;
2430 b672a97a 2020-01-27 stsp s->log_branches = !s->log_branches;
2431 b672a97a 2020-01-27 stsp err = stop_log_thread(s);
2433 b672a97a 2020-01-27 stsp return err;
2434 b672a97a 2020-01-27 stsp lv = view_open(view->nlines, view->ncols,
2435 b672a97a 2020-01-27 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2436 b672a97a 2020-01-27 stsp if (lv == NULL)
2437 b672a97a 2020-01-27 stsp return got_error_from_errno("view_open");
2438 b672a97a 2020-01-27 stsp err = open_log_view(lv, s->start_id, s->refs, s->repo,
2439 b672a97a 2020-01-27 stsp s->head_ref_name, s->in_repo_path, 0, s->log_branches);
2440 b672a97a 2020-01-27 stsp if (err) {
2441 b672a97a 2020-01-27 stsp view_close(lv);
2442 b672a97a 2020-01-27 stsp return err;;
2444 b672a97a 2020-01-27 stsp *dead_view = view;
2445 b672a97a 2020-01-27 stsp *new_view = lv;
2451 80ddbec8 2018-04-29 stsp return err;
2454 4ed7e80c 2018-05-20 stsp static const struct got_error *
2455 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2457 c2db6724 2019-01-04 stsp const struct got_error *error;
2459 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2460 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2461 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2463 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2464 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2466 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2467 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2469 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2470 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2472 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2473 c2db6724 2019-01-04 stsp if (error != NULL)
2474 c2db6724 2019-01-04 stsp return error;
2476 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2477 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2479 c2db6724 2019-01-04 stsp return NULL;
2482 a915003a 2019-02-05 stsp static void
2483 a915003a 2019-02-05 stsp init_curses(void)
2485 a915003a 2019-02-05 stsp initscr();
2487 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2490 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2491 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2492 a915003a 2019-02-05 stsp curs_set(0);
2493 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2494 6d17833f 2019-11-08 stsp start_color();
2495 6d17833f 2019-11-08 stsp use_default_colors();
2497 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2498 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2499 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2502 c2db6724 2019-01-04 stsp static const struct got_error *
2503 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2505 80ddbec8 2018-04-29 stsp const struct got_error *error;
2506 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2507 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2508 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2509 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2510 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2511 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2512 f43793a4 2020-01-27 stsp int ch, log_branches = 0, check_disk = 1;
2513 04cc582a 2018-08-01 stsp struct tog_view *view;
2515 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2517 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2518 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2519 c2db6724 2019-01-04 stsp NULL) == -1)
2520 80ddbec8 2018-04-29 stsp err(1, "pledge");
2523 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2524 80ddbec8 2018-04-29 stsp switch (ch) {
2526 b672a97a 2020-01-27 stsp log_branches = 1;
2529 80ddbec8 2018-04-29 stsp start_commit = optarg;
2532 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2533 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2534 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2538 17020d27 2019-03-07 stsp usage_log();
2539 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2543 80ddbec8 2018-04-29 stsp argc -= optind;
2544 80ddbec8 2018-04-29 stsp argv += optind;
2546 963f97a1 2019-03-18 stsp cwd = getcwd(NULL, 0);
2547 963f97a1 2019-03-18 stsp if (cwd == NULL) {
2548 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2549 963f97a1 2019-03-18 stsp goto done;
2551 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2552 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2553 963f97a1 2019-03-18 stsp goto done;
2554 963f97a1 2019-03-18 stsp error = NULL;
2556 963f97a1 2019-03-18 stsp if (argc == 0) {
2557 ecb28ae0 2018-07-16 stsp path = strdup("");
2558 963f97a1 2019-03-18 stsp if (path == NULL) {
2559 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2560 963f97a1 2019-03-18 stsp goto done;
2562 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2563 963f97a1 2019-03-18 stsp if (worktree) {
2564 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2566 963f97a1 2019-03-18 stsp if (error)
2567 963f97a1 2019-03-18 stsp goto done;
2569 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2570 963f97a1 2019-03-18 stsp if (path == NULL) {
2571 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2572 963f97a1 2019-03-18 stsp goto done;
2576 80ddbec8 2018-04-29 stsp usage_log();
2578 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2579 a1fbf39a 2019-08-11 stsp if (worktree)
2580 b9d7675a 2019-08-11 stsp repo_path = strdup(
2581 b9d7675a 2019-08-11 stsp got_worktree_get_repo_path(worktree));
2583 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2585 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2586 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2587 ecb28ae0 2018-07-16 stsp goto done;
2590 a915003a 2019-02-05 stsp init_curses();
2592 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2593 c02c541e 2019-03-29 stsp if (error != NULL)
2594 c02c541e 2019-03-29 stsp goto done;
2596 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2597 ec142235 2019-03-07 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2598 c2db6724 2019-01-04 stsp if (error)
2599 c2db6724 2019-01-04 stsp goto done;
2601 15a94983 2018-12-23 stsp if (start_commit == NULL)
2602 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2603 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2604 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2606 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2607 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2608 80ddbec8 2018-04-29 stsp if (error != NULL)
2609 ecb28ae0 2018-07-16 stsp goto done;
2611 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2612 8b473291 2019-02-21 stsp if (error)
2613 8b473291 2019-02-21 stsp goto done;
2615 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2616 04cc582a 2018-08-01 stsp if (view == NULL) {
2617 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2618 04cc582a 2018-08-01 stsp goto done;
2620 2fc00ff4 2019-08-31 stsp if (worktree) {
2621 f43793a4 2020-01-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2623 f43793a4 2020-01-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2624 f43793a4 2020-01-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2625 f43793a4 2020-01-27 stsp error = got_error_from_errno("asprintf");
2626 f43793a4 2020-01-27 stsp goto done;
2628 f43793a4 2020-01-27 stsp free(path);
2630 f43793a4 2020-01-27 stsp check_disk = 0;
2632 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2633 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2634 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2635 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2636 2fc00ff4 2019-08-31 stsp goto done;
2639 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2640 f43793a4 2020-01-27 stsp path, check_disk, log_branches);
2641 ba4f502b 2018-08-04 stsp if (error)
2642 ba4f502b 2018-08-04 stsp goto done;
2643 2fc00ff4 2019-08-31 stsp if (worktree) {
2644 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2645 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2646 2fc00ff4 2019-08-31 stsp worktree = NULL;
2648 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2650 ecb28ae0 2018-07-16 stsp free(repo_path);
2651 ecb28ae0 2018-07-16 stsp free(cwd);
2652 ecb28ae0 2018-07-16 stsp free(path);
2653 899d86c2 2018-05-10 stsp free(start_id);
2654 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2656 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2657 ec142235 2019-03-07 stsp if (worktree)
2658 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2659 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2660 80ddbec8 2018-04-29 stsp return error;
2663 4ed7e80c 2018-05-20 stsp __dead static void
2664 9f7d7167 2018-04-29 stsp usage_diff(void)
2667 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2668 9f7d7167 2018-04-29 stsp getprogname());
2672 b304db33 2018-05-20 stsp static char *
2673 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2675 b304db33 2018-05-20 stsp char *line;
2676 b304db33 2018-05-20 stsp size_t linelen;
2677 b304db33 2018-05-20 stsp size_t lineno;
2678 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2680 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2682 b304db33 2018-05-20 stsp *len = linelen;
2683 b304db33 2018-05-20 stsp return line;
2686 6d17833f 2019-11-08 stsp static int
2687 6d17833f 2019-11-08 stsp match_line(const char *line, regex_t *regex)
2689 6d17833f 2019-11-08 stsp regmatch_t regmatch;
2691 6d17833f 2019-11-08 stsp return regexec(regex, line, 1, ®match, 0) == 0;
2694 f26dddb7 2019-11-08 stsp struct tog_color *
2695 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2697 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2699 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2700 6d17833f 2019-11-08 stsp if (match_line(line, &tc->regex))
2701 6d17833f 2019-11-08 stsp return tc;
2704 6d17833f 2019-11-08 stsp return NULL;
2707 4ed7e80c 2018-05-20 stsp static const struct got_error *
2708 f44b1f58 2020-02-02 tracey draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2709 f44b1f58 2020-02-02 tracey int selected_line, int max_lines, int *last_displayed_line, int *eof,
2710 f44b1f58 2020-02-02 tracey char *header, struct tog_colors *colors)
2712 61e69b96 2018-05-20 stsp const struct got_error *err;
2713 f44b1f58 2020-02-02 tracey int lineno = 0, nprinted = 0;
2714 b304db33 2018-05-20 stsp char *line;
2715 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2716 b304db33 2018-05-20 stsp size_t len;
2717 61e69b96 2018-05-20 stsp wchar_t *wline;
2718 e0b650dd 2018-05-20 stsp int width;
2720 26ed57b2 2018-05-19 stsp rewind(f);
2721 f7d12f7e 2018-08-01 stsp werase(view->window);
2723 a3404814 2018-09-02 stsp if (header) {
2724 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
2725 a3404814 2018-09-02 stsp if (err) {
2726 a3404814 2018-09-02 stsp return err;
2729 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2730 a3404814 2018-09-02 stsp wstandout(view->window);
2731 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2732 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2733 a3404814 2018-09-02 stsp wstandend(view->window);
2734 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2735 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2737 a3404814 2018-09-02 stsp if (max_lines <= 1)
2738 a3404814 2018-09-02 stsp return NULL;
2739 a3404814 2018-09-02 stsp max_lines--;
2743 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2744 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2745 26ed57b2 2018-05-19 stsp if (line == NULL) {
2749 f44b1f58 2020-02-02 tracey if (++lineno < *first_displayed_line) {
2750 26ed57b2 2018-05-19 stsp free(line);
2754 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2755 61e69b96 2018-05-20 stsp if (err) {
2756 61e69b96 2018-05-20 stsp free(line);
2757 61e69b96 2018-05-20 stsp return err;
2760 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2762 6d17833f 2019-11-08 stsp wattr_on(view->window,
2763 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2764 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2766 6d17833f 2019-11-08 stsp wattr_off(view->window,
2767 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2768 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2769 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2770 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2771 f44b1f58 2020-02-02 tracey *first_displayed_line = lineno;
2772 26ed57b2 2018-05-19 stsp free(line);
2773 2550e4c3 2018-07-13 stsp free(wline);
2774 2550e4c3 2018-07-13 stsp wline = NULL;
2776 f44b1f58 2020-02-02 tracey *last_displayed_line = lineno;
2778 1a57306a 2018-09-02 stsp view_vborder(view);
2780 c3e9aa98 2019-05-13 jcs if (*eof) {
2781 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2782 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2783 c3e9aa98 2019-05-13 jcs nprinted++;
2786 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2788 c3e9aa98 2019-05-13 jcs return err;
2791 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2792 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2793 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2796 26ed57b2 2018-05-19 stsp return NULL;
2799 abd2672a 2018-12-23 stsp static char *
2800 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2802 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2803 09867e48 2019-08-13 stsp char *p, *s;
2805 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2806 09867e48 2019-08-13 stsp if (tm == NULL)
2807 09867e48 2019-08-13 stsp return NULL;
2808 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2809 09867e48 2019-08-13 stsp if (s == NULL)
2810 09867e48 2019-08-13 stsp return NULL;
2811 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2813 abd2672a 2018-12-23 stsp *p = '\0';
2817 4ed7e80c 2018-05-20 stsp static const struct got_error *
2818 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2819 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2821 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2822 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2823 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2824 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2825 45d799e2 2018-12-23 stsp time_t committer_time;
2826 45d799e2 2018-12-23 stsp const char *author, *committer;
2827 8b473291 2019-02-21 stsp char *refs_str = NULL;
2829 8b473291 2019-02-21 stsp if (refs) {
2830 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2832 8b473291 2019-02-21 stsp return err;
2835 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2837 abd2672a 2018-12-23 stsp return err;
2839 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2840 15a94983 2018-12-23 stsp if (err) {
2841 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2842 15a94983 2018-12-23 stsp goto done;
2845 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2846 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2847 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2848 abd2672a 2018-12-23 stsp goto done;
2850 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2851 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2852 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2853 abd2672a 2018-12-23 stsp goto done;
2855 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2856 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2857 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2858 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2859 abd2672a 2018-12-23 stsp goto done;
2861 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2862 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2863 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2864 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2865 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2866 abd2672a 2018-12-23 stsp goto done;
2868 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2870 5943eee2 2019-08-13 stsp goto done;
2871 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2872 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2873 abd2672a 2018-12-23 stsp goto done;
2876 abd2672a 2018-12-23 stsp free(id_str);
2877 5943eee2 2019-08-13 stsp free(logmsg);
2878 8b473291 2019-02-21 stsp free(refs_str);
2879 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2880 abd2672a 2018-12-23 stsp return err;
2883 f44b1f58 2020-02-02 tracey const struct got_error *
2884 f44b1f58 2020-02-02 tracey get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2885 f44b1f58 2020-02-02 tracey FILE *infile)
2887 f44b1f58 2020-02-02 tracey size_t len;
2888 f44b1f58 2020-02-02 tracey char *buf = NULL;
2890 f44b1f58 2020-02-02 tracey size_t noffsets = 0;
2891 f44b1f58 2020-02-02 tracey off_t off = 0;
2893 f44b1f58 2020-02-02 tracey if (line_offsets)
2894 f44b1f58 2020-02-02 tracey *line_offsets = NULL;
2895 f44b1f58 2020-02-02 tracey if (filesize)
2896 f44b1f58 2020-02-02 tracey *filesize = 0;
2897 f44b1f58 2020-02-02 tracey if (nlines)
2898 f44b1f58 2020-02-02 tracey *nlines = 0;
2900 f44b1f58 2020-02-02 tracey if (fseek(infile, 0, SEEK_END) == -1)
2901 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseek");
2902 f44b1f58 2020-02-02 tracey len = ftell(infile) + 1;
2903 f44b1f58 2020-02-02 tracey if (ferror(infile))
2904 f44b1f58 2020-02-02 tracey return got_error_from_errno("ftell");
2905 f44b1f58 2020-02-02 tracey if (fseek(infile, 0, SEEK_SET) == -1)
2906 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseek");
2908 f44b1f58 2020-02-02 tracey if (len == 0)
2909 f44b1f58 2020-02-02 tracey return NULL;
2910 f44b1f58 2020-02-02 tracey if ((buf = calloc(len, sizeof(char *))) == NULL)
2911 f44b1f58 2020-02-02 tracey return got_error_from_errno("calloc");
2913 f44b1f58 2020-02-02 tracey fread(buf, 1, len, infile);
2914 f44b1f58 2020-02-02 tracey if (ferror(infile))
2915 f44b1f58 2020-02-02 tracey return got_error_from_errno("fread");
2918 f44b1f58 2020-02-02 tracey if (line_offsets && nlines) {
2919 f44b1f58 2020-02-02 tracey if (*line_offsets == NULL) {
2920 f44b1f58 2020-02-02 tracey /* Have some data but perhaps no '\n'. */
2921 f44b1f58 2020-02-02 tracey noffsets = 1;
2922 f44b1f58 2020-02-02 tracey *nlines = 1;
2923 f44b1f58 2020-02-02 tracey *line_offsets = calloc(1, sizeof(**line_offsets));
2924 f44b1f58 2020-02-02 tracey if (*line_offsets == NULL)
2925 f44b1f58 2020-02-02 tracey return got_error_from_errno("calloc");
2926 f44b1f58 2020-02-02 tracey /* Skip forward over end of first line. */
2927 f44b1f58 2020-02-02 tracey while (i < len) {
2928 f44b1f58 2020-02-02 tracey if (buf[i] == '\n')
2933 f44b1f58 2020-02-02 tracey /* Scan '\n' offsets in remaining chunk of data. */
2934 f44b1f58 2020-02-02 tracey while (i < len) {
2935 f44b1f58 2020-02-02 tracey if (buf[i] != '\n') {
2937 f44b1f58 2020-02-02 tracey continue;
2939 f44b1f58 2020-02-02 tracey (*nlines)++;
2940 f44b1f58 2020-02-02 tracey if (noffsets < *nlines) {
2941 f44b1f58 2020-02-02 tracey off_t *o = recallocarray(*line_offsets,
2942 f44b1f58 2020-02-02 tracey noffsets, *nlines,
2943 f44b1f58 2020-02-02 tracey sizeof(**line_offsets));
2944 f44b1f58 2020-02-02 tracey if (o == NULL) {
2945 f44b1f58 2020-02-02 tracey free(*line_offsets);
2946 f44b1f58 2020-02-02 tracey *line_offsets = NULL;
2947 f44b1f58 2020-02-02 tracey return got_error_from_errno(
2948 f44b1f58 2020-02-02 tracey "recallocarray");
2950 f44b1f58 2020-02-02 tracey *line_offsets = o;
2951 f44b1f58 2020-02-02 tracey noffsets = *nlines;
2953 f44b1f58 2020-02-02 tracey off = i + 1;
2954 f44b1f58 2020-02-02 tracey (*line_offsets)[*nlines - 1] = off;
2959 f44b1f58 2020-02-02 tracey if (fflush(infile) != 0)
2960 f44b1f58 2020-02-02 tracey return got_error_from_errno("fflush");
2961 f44b1f58 2020-02-02 tracey rewind(infile);
2963 f44b1f58 2020-02-02 tracey if (filesize)
2964 f44b1f58 2020-02-02 tracey *filesize = len;
2966 f44b1f58 2020-02-02 tracey return NULL;
2969 abd2672a 2018-12-23 stsp static const struct got_error *
2970 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2972 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2973 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2974 15a94983 2018-12-23 stsp int obj_type;
2976 511a516b 2018-05-19 stsp f = got_opentemp();
2977 48ae06ee 2018-10-18 stsp if (f == NULL) {
2978 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2979 48ae06ee 2018-10-18 stsp goto done;
2981 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2982 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2983 fb43ecf1 2019-02-11 stsp goto done;
2987 15a94983 2018-12-23 stsp if (s->id1)
2988 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2990 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2992 15a94983 2018-12-23 stsp goto done;
2994 15a94983 2018-12-23 stsp switch (obj_type) {
2995 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2996 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2997 f44b1f58 2020-02-02 tracey s->diff_context, 0, s->repo, s->f);
2999 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3000 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3001 f44b1f58 2020-02-02 tracey s->diff_context, 0, s->repo, s->f);
3003 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3004 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3005 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3006 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3008 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3010 3ffacbe1 2020-02-02 tracey goto done;
3011 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3012 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3013 f44b1f58 2020-02-02 tracey err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3014 f44b1f58 2020-02-02 tracey if (err)
3015 f44b1f58 2020-02-02 tracey goto done;
3016 f44b1f58 2020-02-02 tracey } else {
3017 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3018 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3019 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3020 f44b1f58 2020-02-02 tracey err = write_commit_info(s->id2, s->refs,
3021 f44b1f58 2020-02-02 tracey s->repo, s->f);
3022 f44b1f58 2020-02-02 tracey if (err)
3023 f44b1f58 2020-02-02 tracey goto done;
3028 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3030 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
3031 f44b1f58 2020-02-02 tracey s->diff_context, 0, s->repo, s->f);
3035 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3038 f44b1f58 2020-02-02 tracey if (err)
3039 f44b1f58 2020-02-02 tracey goto done;
3040 f44b1f58 2020-02-02 tracey err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3043 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3044 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3045 48ae06ee 2018-10-18 stsp return err;
3048 f5215bb9 2019-02-22 stsp static void
3049 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3051 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3052 f5215bb9 2019-02-22 stsp update_panels();
3053 f5215bb9 2019-02-22 stsp doupdate();
3056 48ae06ee 2018-10-18 stsp static const struct got_error *
3057 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3059 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3061 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3062 f44b1f58 2020-02-02 tracey return NULL;
3065 f44b1f58 2020-02-02 tracey static const struct got_error *
3066 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3068 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3069 f44b1f58 2020-02-02 tracey int lineno;
3071 f44b1f58 2020-02-02 tracey if (!view->searching) {
3072 f44b1f58 2020-02-02 tracey view->search_next_done = 1;
3073 f44b1f58 2020-02-02 tracey return NULL;
3076 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3077 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3078 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3080 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3081 f44b1f58 2020-02-02 tracey } else {
3082 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3083 f44b1f58 2020-02-02 tracey lineno = 1;
3085 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3088 f44b1f58 2020-02-02 tracey while (1) {
3089 f44b1f58 2020-02-02 tracey char *line = NULL;
3090 f44b1f58 2020-02-02 tracey off_t offset;
3091 f44b1f58 2020-02-02 tracey size_t len;
3093 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3094 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3095 f44b1f58 2020-02-02 tracey view->search_next_done = 1;
3096 f44b1f58 2020-02-02 tracey free(line);
3100 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3101 f44b1f58 2020-02-02 tracey lineno = 1;
3103 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3106 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3107 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3108 f44b1f58 2020-02-02 tracey free(line);
3109 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3111 f44b1f58 2020-02-02 tracey free(line);
3112 f44b1f58 2020-02-02 tracey line = parse_next_line(s->f, &len);
3113 f44b1f58 2020-02-02 tracey if (line && match_line(line, &view->regex)) {
3114 f44b1f58 2020-02-02 tracey view->search_next_done = 1;
3115 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3116 f44b1f58 2020-02-02 tracey free(line);
3119 f44b1f58 2020-02-02 tracey free(line);
3120 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3121 f44b1f58 2020-02-02 tracey lineno++;
3123 f44b1f58 2020-02-02 tracey lineno--;
3126 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3127 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3128 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3131 f44b1f58 2020-02-02 tracey return NULL;
3134 f44b1f58 2020-02-02 tracey static const struct got_error *
3135 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3136 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
3137 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
3139 48ae06ee 2018-10-18 stsp const struct got_error *err;
3140 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3142 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3143 15a94983 2018-12-23 stsp int type1, type2;
3144 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3146 15a94983 2018-12-23 stsp return err;
3147 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3149 15a94983 2018-12-23 stsp return err;
3151 15a94983 2018-12-23 stsp if (type1 != type2)
3152 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
3154 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3155 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3156 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3157 f44b1f58 2020-02-02 tracey s->repo = repo;
3158 f44b1f58 2020-02-02 tracey s->refs = refs;
3159 f44b1f58 2020-02-02 tracey s->id1 = id1;
3160 f44b1f58 2020-02-02 tracey s->id2 = id2;
3162 15a94983 2018-12-23 stsp if (id1) {
3163 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3164 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3165 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3167 5465d566 2020-02-01 tracey s->id1 = NULL;
3169 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3170 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3171 5465d566 2020-02-01 tracey free(s->id1);
3172 5465d566 2020-02-01 tracey s->id1 = NULL;
3173 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3175 5465d566 2020-02-01 tracey s->f = NULL;
3176 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3177 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3178 5465d566 2020-02-01 tracey s->diff_context = 3;
3179 5465d566 2020-02-01 tracey s->log_view = log_view;
3180 5465d566 2020-02-01 tracey s->repo = repo;
3181 5465d566 2020-02-01 tracey s->refs = refs;
3183 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3184 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3185 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3186 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3187 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3189 6d17833f 2019-11-08 stsp return err;
3190 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3191 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3192 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3193 6d17833f 2019-11-08 stsp if (err) {
3194 5465d566 2020-02-01 tracey free_colors(&s->colors);
3195 6d17833f 2019-11-08 stsp return err;
3197 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3198 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3199 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3200 6d17833f 2019-11-08 stsp if (err) {
3201 5465d566 2020-02-01 tracey free_colors(&s->colors);
3202 6d17833f 2019-11-08 stsp return err;
3205 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3206 11b20872 2019-11-08 stsp "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3207 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3208 6d17833f 2019-11-08 stsp if (err) {
3209 5465d566 2020-02-01 tracey free_colors(&s->colors);
3210 6d17833f 2019-11-08 stsp return err;
3213 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3214 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3215 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3216 11b20872 2019-11-08 stsp if (err) {
3217 5465d566 2020-02-01 tracey free_colors(&s->colors);
3218 11b20872 2019-11-08 stsp return err;
3221 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3222 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3223 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3224 11b20872 2019-11-08 stsp if (err) {
3225 5465d566 2020-02-01 tracey free_colors(&s->colors);
3226 11b20872 2019-11-08 stsp return err;
3230 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3231 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3232 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3234 5465d566 2020-02-01 tracey err = create_diff(s);
3235 48ae06ee 2018-10-18 stsp if (err) {
3236 5465d566 2020-02-01 tracey free(s->id1);
3237 5465d566 2020-02-01 tracey s->id1 = NULL;
3238 5465d566 2020-02-01 tracey free(s->id2);
3239 5465d566 2020-02-01 tracey s->id2 = NULL;
3240 48ae06ee 2018-10-18 stsp return err;
3243 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3244 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3245 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3246 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3247 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3249 5dc9f4bc 2018-08-04 stsp return NULL;
3252 e5a0f69f 2018-08-18 stsp static const struct got_error *
3253 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3255 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3256 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3258 5465d566 2020-02-01 tracey free(s->id1);
3259 5465d566 2020-02-01 tracey s->id1 = NULL;
3260 5465d566 2020-02-01 tracey free(s->id2);
3261 5465d566 2020-02-01 tracey s->id2 = NULL;
3262 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3263 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3264 5465d566 2020-02-01 tracey free_colors(&s->colors);
3265 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3266 e5a0f69f 2018-08-18 stsp return err;
3269 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3270 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3272 a3404814 2018-09-02 stsp const struct got_error *err;
3273 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3274 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3276 a3404814 2018-09-02 stsp if (s->id1) {
3277 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3279 a3404814 2018-09-02 stsp return err;
3281 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3283 a3404814 2018-09-02 stsp return err;
3285 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
3286 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3287 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3288 a3404814 2018-09-02 stsp free(id_str1);
3289 a3404814 2018-09-02 stsp free(id_str2);
3290 a3404814 2018-09-02 stsp return err;
3292 a3404814 2018-09-02 stsp free(id_str1);
3293 a3404814 2018-09-02 stsp free(id_str2);
3295 f44b1f58 2020-02-02 tracey return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3296 f44b1f58 2020-02-02 tracey s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3297 bddb1296 2019-11-08 stsp header, &s->colors);
3300 e5a0f69f 2018-08-18 stsp static const struct got_error *
3301 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3302 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3304 d7a04538 2019-02-21 stsp const struct got_error *err;
3305 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3306 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3307 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3309 15a087fe 2019-02-21 stsp free(s->id2);
3310 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3311 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3312 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3314 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3316 d7a04538 2019-02-21 stsp return err;
3317 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3318 15a087fe 2019-02-21 stsp free(s->id1);
3319 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3320 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3321 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3322 15a087fe 2019-02-21 stsp return NULL;
3325 15a087fe 2019-02-21 stsp static const struct got_error *
3326 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3327 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3329 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3330 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3331 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3332 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3335 e5a0f69f 2018-08-18 stsp switch (ch) {
3337 1e37a5c2 2019-05-12 jcs case KEY_UP:
3338 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3339 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3341 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3342 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3343 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3346 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3347 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3348 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3351 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3352 1e37a5c2 2019-05-12 jcs if (!s->eof)
3353 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3355 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3356 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3358 00ba99a7 2019-05-12 jcs if (s->eof)
3361 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3362 1e37a5c2 2019-05-12 jcs char *line;
3363 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3364 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3365 1e37a5c2 2019-05-12 jcs if (line == NULL)
3370 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3371 1e37a5c2 2019-05-12 jcs s->diff_context--;
3372 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3373 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3377 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3378 1e37a5c2 2019-05-12 jcs s->diff_context++;
3379 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3380 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3385 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3387 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3388 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3389 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3390 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3393 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3398 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3402 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3403 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3405 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3406 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3410 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3412 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3414 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3415 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3417 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
3418 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
3419 1e37a5c2 2019-05-12 jcs update_panels();
3420 1e37a5c2 2019-05-12 jcs doupdate();
3422 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
3423 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
3424 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
3425 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
3429 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3434 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3435 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3438 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3442 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3443 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3445 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3446 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3452 bcbd79e2 2018-08-19 stsp return err;
3455 4ed7e80c 2018-05-20 stsp static const struct got_error *
3456 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3458 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3459 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3460 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3461 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3462 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
3463 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3465 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3467 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3469 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3470 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3471 eb6600df 2019-01-04 stsp NULL) == -1)
3472 26ed57b2 2018-05-19 stsp err(1, "pledge");
3475 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3476 26ed57b2 2018-05-19 stsp switch (ch) {
3478 17020d27 2019-03-07 stsp usage_diff();
3479 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3483 26ed57b2 2018-05-19 stsp argc -= optind;
3484 26ed57b2 2018-05-19 stsp argv += optind;
3486 26ed57b2 2018-05-19 stsp if (argc == 0) {
3487 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3488 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3489 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
3490 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3491 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
3492 15a94983 2018-12-23 stsp id_str1 = argv[0];
3493 15a94983 2018-12-23 stsp id_str2 = argv[1];
3494 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
3495 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
3496 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3497 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
3498 15a94983 2018-12-23 stsp id_str1 = argv[1];
3499 15a94983 2018-12-23 stsp id_str2 = argv[2];
3501 26ed57b2 2018-05-19 stsp usage_diff();
3503 a915003a 2019-02-05 stsp init_curses();
3505 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3506 26ed57b2 2018-05-19 stsp if (error)
3507 26ed57b2 2018-05-19 stsp goto done;
3509 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3510 c02c541e 2019-03-29 stsp if (error)
3511 c02c541e 2019-03-29 stsp goto done;
3513 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
3514 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3515 26ed57b2 2018-05-19 stsp if (error)
3516 26ed57b2 2018-05-19 stsp goto done;
3518 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
3519 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3520 26ed57b2 2018-05-19 stsp if (error)
3521 26ed57b2 2018-05-19 stsp goto done;
3523 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3524 8b473291 2019-02-21 stsp if (error)
3525 8b473291 2019-02-21 stsp goto done;
3527 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3528 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3529 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3530 ea5e7bb5 2018-08-01 stsp goto done;
3532 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3533 5dc9f4bc 2018-08-04 stsp if (error)
3534 5dc9f4bc 2018-08-04 stsp goto done;
3535 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3537 c02c541e 2019-03-29 stsp free(repo_path);
3539 921be706 2019-06-28 stsp got_repo_close(repo);
3540 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3541 26ed57b2 2018-05-19 stsp return error;
3544 4ed7e80c 2018-05-20 stsp __dead static void
3545 9f7d7167 2018-04-29 stsp usage_blame(void)
3548 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3549 9f7d7167 2018-04-29 stsp getprogname());
3553 84451b3e 2018-07-10 stsp struct tog_blame_line {
3554 84451b3e 2018-07-10 stsp int annotated;
3555 84451b3e 2018-07-10 stsp struct got_object_id *id;
3558 4ed7e80c 2018-05-20 stsp static const struct got_error *
3559 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3560 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3561 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3562 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3563 11b20872 2019-11-08 stsp struct tog_colors *colors)
3565 84451b3e 2018-07-10 stsp const struct got_error *err;
3566 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3567 84451b3e 2018-07-10 stsp char *line;
3568 84451b3e 2018-07-10 stsp size_t len;
3569 84451b3e 2018-07-10 stsp wchar_t *wline;
3570 27a741e5 2019-09-11 stsp int width;
3571 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3572 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3573 ab089a2a 2018-07-12 stsp char *id_str;
3574 11b20872 2019-11-08 stsp struct tog_color *tc;
3576 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3578 ab089a2a 2018-07-12 stsp return err;
3580 84451b3e 2018-07-10 stsp rewind(f);
3581 f7d12f7e 2018-08-01 stsp werase(view->window);
3583 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3584 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3585 ab089a2a 2018-07-12 stsp free(id_str);
3586 ab089a2a 2018-07-12 stsp return err;
3589 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3590 ab089a2a 2018-07-12 stsp free(line);
3591 2550e4c3 2018-07-13 stsp line = NULL;
3593 1cae65b4 2019-09-22 stsp return err;
3594 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3595 a3404814 2018-09-02 stsp wstandout(view->window);
3596 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3598 11b20872 2019-11-08 stsp wattr_on(view->window,
3599 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3600 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3602 11b20872 2019-11-08 stsp wattr_off(view->window,
3603 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3604 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3605 a3404814 2018-09-02 stsp wstandend(view->window);
3606 2550e4c3 2018-07-13 stsp free(wline);
3607 2550e4c3 2018-07-13 stsp wline = NULL;
3608 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3609 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3611 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3612 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3613 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3614 ab089a2a 2018-07-12 stsp free(id_str);
3615 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3617 ab089a2a 2018-07-12 stsp free(id_str);
3618 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3619 3f60a8ef 2018-07-10 stsp free(line);
3620 2550e4c3 2018-07-13 stsp line = NULL;
3622 3f60a8ef 2018-07-10 stsp return err;
3623 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3624 2550e4c3 2018-07-13 stsp free(wline);
3625 2550e4c3 2018-07-13 stsp wline = NULL;
3626 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3627 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3630 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3631 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3632 84451b3e 2018-07-10 stsp if (line == NULL) {
3636 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3637 84451b3e 2018-07-10 stsp free(line);
3641 27a741e5 2019-09-11 stsp if (view->ncols <= 9) {
3642 27a741e5 2019-09-11 stsp width = 9;
3643 27a741e5 2019-09-11 stsp wline = wcsdup(L"");
3644 27a741e5 2019-09-11 stsp if (wline == NULL)
3645 27a741e5 2019-09-11 stsp err = got_error_from_errno("wcsdup");
3647 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line,
3648 27a741e5 2019-09-11 stsp view->ncols - 9, 9);
3649 27a741e5 2019-09-11 stsp width += 9;
3651 84451b3e 2018-07-10 stsp if (err) {
3652 84451b3e 2018-07-10 stsp free(line);
3653 84451b3e 2018-07-10 stsp return err;
3656 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3657 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3659 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3660 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3661 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3662 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3663 27a741e5 2019-09-11 stsp !(view->focussed &&
3664 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3665 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3666 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3667 8d0fe45a 2019-08-12 stsp char *id_str;
3668 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3669 8d0fe45a 2019-08-12 stsp if (err) {
3670 8d0fe45a 2019-08-12 stsp free(line);
3671 8d0fe45a 2019-08-12 stsp free(wline);
3672 8d0fe45a 2019-08-12 stsp return err;
3674 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3676 11b20872 2019-11-08 stsp wattr_on(view->window,
3677 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3678 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3680 11b20872 2019-11-08 stsp wattr_off(view->window,
3681 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3682 8d0fe45a 2019-08-12 stsp free(id_str);
3683 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3685 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3686 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3689 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3690 ee41ec32 2018-07-10 stsp prev_id = NULL;
3693 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3694 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3695 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3697 27a741e5 2019-09-11 stsp waddwstr(view->window, wline);
3698 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3699 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3700 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3701 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3702 84451b3e 2018-07-10 stsp free(line);
3703 2550e4c3 2018-07-13 stsp free(wline);
3704 2550e4c3 2018-07-13 stsp wline = NULL;
3706 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3708 1a57306a 2018-09-02 stsp view_vborder(view);
3710 84451b3e 2018-07-10 stsp return NULL;
3713 84451b3e 2018-07-10 stsp static const struct got_error *
3714 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3716 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3717 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3718 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3719 1a76625f 2018-10-22 stsp int errcode;
3721 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3722 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3723 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3725 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3726 1a76625f 2018-10-22 stsp if (errcode)
3727 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3729 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3730 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3731 d68a0a7d 2018-07-10 stsp goto done;
3734 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3735 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3737 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3738 d68a0a7d 2018-07-10 stsp if (line->annotated)
3739 d68a0a7d 2018-07-10 stsp goto done;
3741 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3742 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3743 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3744 84451b3e 2018-07-10 stsp goto done;
3746 84451b3e 2018-07-10 stsp line->annotated = 1;
3748 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3749 1a76625f 2018-10-22 stsp if (errcode)
3750 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3751 84451b3e 2018-07-10 stsp return err;
3754 84451b3e 2018-07-10 stsp static void *
3755 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3757 18430de3 2018-07-10 stsp const struct got_error *err;
3758 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3759 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3760 1a76625f 2018-10-22 stsp int errcode;
3762 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
3764 61266923 2020-01-14 stsp return (void *)err;
3766 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3767 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3768 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3769 fc06ba56 2019-08-22 stsp err = NULL;
3771 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3772 1a76625f 2018-10-22 stsp if (errcode)
3773 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3774 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3776 9328d2ed 2018-07-13 stsp got_repo_close(ta->repo);
3777 9328d2ed 2018-07-13 stsp ta->repo = NULL;
3778 18430de3 2018-07-10 stsp *ta->complete = 1;
3780 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3781 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3782 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3784 18430de3 2018-07-10 stsp return (void *)err;
3787 245d91c1 2018-07-12 stsp static struct got_object_id *
3788 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3789 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3791 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3793 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3794 8d0fe45a 2019-08-12 stsp return NULL;
3796 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3797 245d91c1 2018-07-12 stsp if (!line->annotated)
3798 245d91c1 2018-07-12 stsp return NULL;
3800 245d91c1 2018-07-12 stsp return line->id;
3803 84451b3e 2018-07-10 stsp static const struct got_error *
3804 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3806 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3809 245d91c1 2018-07-12 stsp if (blame->thread) {
3810 1a76625f 2018-10-22 stsp int errcode;
3811 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3812 1a76625f 2018-10-22 stsp if (errcode)
3813 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3814 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3815 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3816 1a76625f 2018-10-22 stsp if (errcode)
3817 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3818 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3819 1a76625f 2018-10-22 stsp if (errcode)
3820 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3821 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3822 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3823 245d91c1 2018-07-12 stsp err = NULL;
3824 245d91c1 2018-07-12 stsp blame->thread = NULL;
3826 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3827 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3828 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3830 245d91c1 2018-07-12 stsp if (blame->f) {
3831 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3832 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3833 245d91c1 2018-07-12 stsp blame->f = NULL;
3835 57670559 2018-12-23 stsp if (blame->lines) {
3836 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3837 57670559 2018-12-23 stsp free(blame->lines[i].id);
3838 57670559 2018-12-23 stsp free(blame->lines);
3839 57670559 2018-12-23 stsp blame->lines = NULL;
3841 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3842 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3844 245d91c1 2018-07-12 stsp return err;
3847 245d91c1 2018-07-12 stsp static const struct got_error *
3848 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
3850 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
3851 fc06ba56 2019-08-22 stsp int *done = arg;
3852 fc06ba56 2019-08-22 stsp int errcode;
3854 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3855 fc06ba56 2019-08-22 stsp if (errcode)
3856 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3857 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
3859 fc06ba56 2019-08-22 stsp if (*done)
3860 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
3862 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3863 fc06ba56 2019-08-22 stsp if (errcode)
3864 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3865 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
3867 fc06ba56 2019-08-22 stsp return err;
3870 fc06ba56 2019-08-22 stsp static const struct got_error *
3871 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3872 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3873 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3874 a70480e0 2018-06-23 stsp struct got_repository *repo)
3876 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3877 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3878 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3879 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3880 15a94983 2018-12-23 stsp int obj_type;
3882 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3884 15a94983 2018-12-23 stsp return err;
3886 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3888 27d434c2 2018-09-15 stsp goto done;
3890 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3891 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3892 84451b3e 2018-07-10 stsp goto done;
3895 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3897 84451b3e 2018-07-10 stsp goto done;
3898 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3899 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3900 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3901 84451b3e 2018-07-10 stsp goto done;
3903 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3904 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3905 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
3906 a70480e0 2018-06-23 stsp goto done;
3908 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3909 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3910 b02560ec 2019-08-19 stsp blame->nlines--;
3912 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3913 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3914 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3915 84451b3e 2018-07-10 stsp goto done;
3918 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3920 bd24772e 2018-07-11 stsp goto done;
3922 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3923 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3924 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3925 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3926 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3927 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3928 245d91c1 2018-07-12 stsp goto done;
3930 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3932 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3933 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3934 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3935 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3936 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
3937 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
3938 245d91c1 2018-07-12 stsp *blame_complete = 0;
3942 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3943 27d434c2 2018-09-15 stsp free(obj_id);
3945 245d91c1 2018-07-12 stsp stop_blame(blame);
3946 245d91c1 2018-07-12 stsp return err;
3949 245d91c1 2018-07-12 stsp static const struct got_error *
3950 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3951 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3952 8b473291 2019-02-21 stsp struct got_repository *repo)
3954 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3955 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3957 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3959 c4843652 2019-08-12 stsp s->path = strdup(path);
3960 c4843652 2019-08-12 stsp if (s->path == NULL)
3961 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
3963 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3964 c4843652 2019-08-12 stsp if (err) {
3965 c4843652 2019-08-12 stsp free(s->path);
3966 7cbe629d 2018-08-04 stsp return err;
3969 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3970 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3971 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3972 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3973 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3974 fb2756b9 2018-08-04 stsp s->repo = repo;
3975 8b473291 2019-02-21 stsp s->refs = refs;
3976 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3977 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3979 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
3980 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3981 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3982 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3984 11b20872 2019-11-08 stsp return err;
3987 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3988 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3989 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3990 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3991 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3993 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3994 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3995 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3996 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3999 e5a0f69f 2018-08-18 stsp static const struct got_error *
4000 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4002 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4003 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4005 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4006 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4008 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4009 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4010 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4011 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4012 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4015 e5a0f69f 2018-08-18 stsp free(s->path);
4016 11b20872 2019-11-08 stsp free_colors(&s->colors);
4018 e5a0f69f 2018-08-18 stsp return err;
4021 7cbe629d 2018-08-04 stsp static const struct got_error *
4022 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4024 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4026 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4027 6c4c42e0 2019-06-24 stsp return NULL;
4030 6c4c42e0 2019-06-24 stsp static const struct got_error *
4031 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4033 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4034 6c4c42e0 2019-06-24 stsp int lineno;
4036 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4037 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
4038 6c4c42e0 2019-06-24 stsp return NULL;
4041 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4042 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4043 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4045 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4047 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4048 6c4c42e0 2019-06-24 stsp lineno = 1;
4050 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4053 6c4c42e0 2019-06-24 stsp while (1) {
4054 6c4c42e0 2019-06-24 stsp char *line = NULL;
4055 6c4c42e0 2019-06-24 stsp off_t offset;
4056 6c4c42e0 2019-06-24 stsp size_t len;
4058 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4059 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4060 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
4061 6c4c42e0 2019-06-24 stsp free(line);
4065 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4066 6c4c42e0 2019-06-24 stsp lineno = 1;
4068 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4071 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4072 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4073 6c4c42e0 2019-06-24 stsp free(line);
4074 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4076 6c4c42e0 2019-06-24 stsp free(line);
4077 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
4078 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
4079 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
4080 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4081 6c4c42e0 2019-06-24 stsp free(line);
4084 6c4c42e0 2019-06-24 stsp free(line);
4085 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4091 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4092 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4093 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4096 6c4c42e0 2019-06-24 stsp return NULL;
4099 6c4c42e0 2019-06-24 stsp static const struct got_error *
4100 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4102 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4103 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4104 2b380cc8 2018-10-24 stsp int errcode;
4106 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4107 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4108 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4109 2b380cc8 2018-10-24 stsp if (errcode)
4110 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4112 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4115 51fe7530 2019-08-19 stsp if (s->blame_complete)
4116 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4118 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4119 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4120 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
4121 11b20872 2019-11-08 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4123 669b5ffa 2018-10-07 stsp view_vborder(view);
4124 e5a0f69f 2018-08-18 stsp return err;
4127 e5a0f69f 2018-08-18 stsp static const struct got_error *
4128 e5a0f69f 2018-08-18 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4129 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4131 245d91c1 2018-07-12 stsp const struct got_error *err = NULL, *thread_err = NULL;
4132 e1cd8fed 2018-08-01 stsp struct tog_view *diff_view;
4133 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4134 669b5ffa 2018-10-07 stsp int begin_x = 0;
4136 e5a0f69f 2018-08-18 stsp switch (ch) {
4138 1e37a5c2 2019-05-12 jcs s->done = 1;
4141 1e37a5c2 2019-05-12 jcs case KEY_UP:
4142 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4143 1e37a5c2 2019-05-12 jcs s->selected_line--;
4144 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4145 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4146 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4148 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4149 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4150 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4153 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4154 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4155 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4157 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4160 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4161 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4162 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4163 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4164 1e37a5c2 2019-05-12 jcs s->selected_line++;
4165 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4166 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4167 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4170 1e37a5c2 2019-05-12 jcs case 'p': {
4171 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4172 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4173 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4174 1e37a5c2 2019-05-12 jcs if (id == NULL)
4176 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4177 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4178 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4179 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4180 1e37a5c2 2019-05-12 jcs int obj_type;
4181 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4182 1e37a5c2 2019-05-12 jcs s->repo, id);
4185 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4186 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4187 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4188 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4191 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4192 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4193 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4194 e5a0f69f 2018-08-18 stsp if (err) {
4195 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4196 1e37a5c2 2019-05-12 jcs err = NULL;
4197 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4200 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4202 1e37a5c2 2019-05-12 jcs free(blob_id);
4203 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4204 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4205 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4208 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4210 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4212 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4213 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4215 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4220 1e37a5c2 2019-05-12 jcs s->done = 1;
4221 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4222 1e37a5c2 2019-05-12 jcs s->done = 0;
4223 1e37a5c2 2019-05-12 jcs if (thread_err)
4225 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4226 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4227 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4228 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4229 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
4230 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
4235 1e37a5c2 2019-05-12 jcs case 'B': {
4236 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4237 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4238 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4240 1e37a5c2 2019-05-12 jcs s->done = 1;
4241 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4242 1e37a5c2 2019-05-12 jcs s->done = 0;
4243 1e37a5c2 2019-05-12 jcs if (thread_err)
4245 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4246 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4247 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4248 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4249 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4250 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4251 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
4252 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
4257 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4258 1e37a5c2 2019-05-12 jcs case '\r': {
4259 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4260 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4261 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4262 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4263 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4264 1e37a5c2 2019-05-12 jcs if (id == NULL)
4266 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4269 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4270 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4271 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4272 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4273 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4274 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4275 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4276 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4279 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4280 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
4281 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4283 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4286 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4287 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4290 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4292 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4295 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4296 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4298 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4303 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4305 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4306 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4307 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4310 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4311 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4312 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4313 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4316 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4317 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4318 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4319 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4321 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4322 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4323 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4325 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4326 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4327 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4328 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4334 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4337 a70480e0 2018-06-23 stsp static const struct got_error *
4338 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4340 a70480e0 2018-06-23 stsp const struct got_error *error;
4341 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4342 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4343 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4344 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4345 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4346 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4348 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4350 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4352 a70480e0 2018-06-23 stsp #ifndef PROFILE
4353 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4354 8e94dd5b 2019-01-04 stsp NULL) == -1)
4355 a70480e0 2018-06-23 stsp err(1, "pledge");
4358 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4359 a70480e0 2018-06-23 stsp switch (ch) {
4361 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4364 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4365 69069811 2018-08-02 stsp if (repo_path == NULL)
4366 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4370 17020d27 2019-03-07 stsp usage_blame();
4371 a70480e0 2018-06-23 stsp /* NOTREACHED */
4375 a70480e0 2018-06-23 stsp argc -= optind;
4376 a70480e0 2018-06-23 stsp argv += optind;
4378 69069811 2018-08-02 stsp if (argc == 1)
4379 a70480e0 2018-06-23 stsp path = argv[0];
4381 a70480e0 2018-06-23 stsp usage_blame();
4383 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4384 69069811 2018-08-02 stsp if (cwd == NULL) {
4385 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4386 69069811 2018-08-02 stsp goto done;
4388 69069811 2018-08-02 stsp if (repo_path == NULL) {
4389 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4390 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4391 69069811 2018-08-02 stsp goto done;
4393 eb41ed75 2019-02-05 stsp error = NULL;
4394 eb41ed75 2019-02-05 stsp if (worktree) {
4395 eb41ed75 2019-02-05 stsp repo_path =
4396 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4397 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
4398 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4399 eb41ed75 2019-02-05 stsp if (error)
4400 eb41ed75 2019-02-05 stsp goto done;
4402 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4403 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
4404 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4405 eb41ed75 2019-02-05 stsp goto done;
4410 a915003a 2019-02-05 stsp init_curses();
4412 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4413 a70480e0 2018-06-23 stsp if (error != NULL)
4414 92205607 2019-01-04 stsp goto done;
4416 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4417 c02c541e 2019-03-29 stsp if (error)
4418 c02c541e 2019-03-29 stsp goto done;
4420 eb41ed75 2019-02-05 stsp if (worktree) {
4421 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4422 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4423 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4424 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4425 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4426 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4427 eb41ed75 2019-02-05 stsp path) == -1) {
4428 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4429 eb41ed75 2019-02-05 stsp goto done;
4431 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4434 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4436 eb41ed75 2019-02-05 stsp if (error)
4437 69069811 2018-08-02 stsp goto done;
4439 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4440 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4441 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4442 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4443 a70480e0 2018-06-23 stsp if (error != NULL)
4444 66b4983c 2018-06-23 stsp goto done;
4445 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4446 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4448 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4449 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4451 a19e88aa 2018-06-23 stsp if (error != NULL)
4452 a19e88aa 2018-06-23 stsp goto done;
4454 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4455 8b473291 2019-02-21 stsp if (error)
4456 8b473291 2019-02-21 stsp goto done;
4458 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4459 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4460 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4461 e1cd8fed 2018-08-01 stsp goto done;
4463 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4464 7cbe629d 2018-08-04 stsp if (error)
4465 7cbe629d 2018-08-04 stsp goto done;
4466 12314ad4 2019-08-31 stsp if (worktree) {
4467 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4468 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4469 12314ad4 2019-08-31 stsp worktree = NULL;
4471 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4473 69069811 2018-08-02 stsp free(repo_path);
4474 69069811 2018-08-02 stsp free(cwd);
4475 a70480e0 2018-06-23 stsp free(commit_id);
4476 eb41ed75 2019-02-05 stsp if (worktree)
4477 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4479 a70480e0 2018-06-23 stsp got_repo_close(repo);
4480 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4481 a70480e0 2018-06-23 stsp return error;
4484 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4485 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4486 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4487 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4488 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4489 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4490 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4491 f26dddb7 2019-11-08 stsp int isroot, struct tog_colors *colors)
4493 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4494 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4495 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4496 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4497 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4499 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4501 f7d12f7e 2018-08-01 stsp werase(view->window);
4503 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4504 ffd1d5e5 2018-06-23 stsp return NULL;
4506 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4508 ffd1d5e5 2018-06-23 stsp return err;
4509 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4510 a3404814 2018-09-02 stsp wstandout(view->window);
4511 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4513 11b20872 2019-11-08 stsp wattr_on(view->window,
4514 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4515 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4517 11b20872 2019-11-08 stsp wattr_off(view->window,
4518 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4519 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4520 a3404814 2018-09-02 stsp wstandend(view->window);
4521 2550e4c3 2018-07-13 stsp free(wline);
4522 2550e4c3 2018-07-13 stsp wline = NULL;
4523 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4524 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4525 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4526 ffd1d5e5 2018-06-23 stsp return NULL;
4527 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4529 ce52c690 2018-06-23 stsp return err;
4530 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4531 2550e4c3 2018-07-13 stsp free(wline);
4532 2550e4c3 2018-07-13 stsp wline = NULL;
4533 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4534 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4535 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4536 ffd1d5e5 2018-06-23 stsp return NULL;
4537 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4538 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4539 a1eca9bb 2018-06-23 stsp return NULL;
4541 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4542 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4543 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4544 0cf4efb1 2018-09-29 stsp if (view->focussed)
4545 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4546 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4548 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4549 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4550 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4551 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4552 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4553 ffd1d5e5 2018-06-23 stsp return NULL;
4557 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4560 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4561 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4562 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
4563 848d6979 2019-08-12 stsp const char *modestr = "";
4564 56e0773d 2019-11-28 stsp mode_t mode;
4566 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4567 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4569 1d13200f 2018-07-12 stsp if (show_ids) {
4570 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4571 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4573 638f9024 2019-05-13 stsp return got_error_from_errno(
4574 230a42bd 2019-05-11 jcs "got_object_id_str");
4576 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4577 63c5ca5d 2019-08-24 stsp modestr = "$";
4578 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
4579 848d6979 2019-08-12 stsp modestr = "@";
4580 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4581 848d6979 2019-08-12 stsp modestr = "/";
4582 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4583 848d6979 2019-08-12 stsp modestr = "*";
4584 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4585 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te), modestr) == -1) {
4586 1d13200f 2018-07-12 stsp free(id_str);
4587 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4589 1d13200f 2018-07-12 stsp free(id_str);
4590 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4591 ffd1d5e5 2018-06-23 stsp if (err) {
4592 ffd1d5e5 2018-06-23 stsp free(line);
4595 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4596 0cf4efb1 2018-09-29 stsp if (view->focussed)
4597 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4598 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4600 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4602 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4603 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4604 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4606 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4607 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4608 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4609 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4610 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4611 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4612 ffd1d5e5 2018-06-23 stsp free(line);
4613 2550e4c3 2018-07-13 stsp free(wline);
4614 2550e4c3 2018-07-13 stsp wline = NULL;
4616 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4617 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4618 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4622 ffd1d5e5 2018-06-23 stsp return err;
4625 5c5136c5 2018-05-20 stsp static void
4626 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4627 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4628 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int isroot)
4630 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4633 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4636 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, 0);
4637 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4638 ffd1d5e5 2018-06-23 stsp if (!isroot)
4639 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4644 56e0773d 2019-11-28 stsp while (*first_displayed_entry && i < maxscroll) {
4645 56e0773d 2019-11-28 stsp *first_displayed_entry = got_tree_entry_get_prev(tree,
4646 56e0773d 2019-11-28 stsp *first_displayed_entry);
4649 56e0773d 2019-11-28 stsp if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4650 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4653 768394f3 2019-01-24 stsp static int
4654 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4655 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4656 56e0773d 2019-11-28 stsp struct got_tree_object *tree)
4658 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4659 ffd1d5e5 2018-06-23 stsp int n = 0;
4661 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4662 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, *first_displayed_entry);
4664 56e0773d 2019-11-28 stsp next = got_object_tree_get_first_entry(tree);
4666 768394f3 2019-01-24 stsp last = last_displayed_entry;
4667 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4668 56e0773d 2019-11-28 stsp last = got_tree_entry_get_next(tree, last);
4669 768394f3 2019-01-24 stsp if (last) {
4670 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4671 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, next);
4677 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4678 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4679 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4681 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4682 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4683 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4685 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4686 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4687 56e0773d 2019-11-28 stsp + 1 /* slash */;
4689 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4691 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4692 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4693 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4695 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4696 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4697 d9765a41 2018-06-23 stsp while (pt) {
4698 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4699 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4700 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4701 cb2ebc8a 2018-06-23 stsp goto done;
4703 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4704 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4705 cb2ebc8a 2018-06-23 stsp goto done;
4707 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4710 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4711 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4712 ce52c690 2018-06-23 stsp goto done;
4716 ce52c690 2018-06-23 stsp if (err) {
4717 ce52c690 2018-06-23 stsp free(*path);
4718 ce52c690 2018-06-23 stsp *path = NULL;
4720 ce52c690 2018-06-23 stsp return err;
4723 ce52c690 2018-06-23 stsp static const struct got_error *
4724 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4725 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4726 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4727 8b473291 2019-02-21 stsp struct got_repository *repo)
4729 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4730 ce52c690 2018-06-23 stsp char *path;
4731 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4733 a0de39f3 2019-08-09 stsp *new_view = NULL;
4735 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4737 ce52c690 2018-06-23 stsp return err;
4739 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4740 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4741 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4742 83ce39e3 2019-08-12 stsp goto done;
4745 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4746 e5a0f69f 2018-08-18 stsp if (err) {
4747 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4748 fc06ba56 2019-08-22 stsp err = NULL;
4749 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4751 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4753 83ce39e3 2019-08-12 stsp free(path);
4754 cb2ebc8a 2018-06-23 stsp return err;
4757 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4758 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4759 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4760 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4761 8b473291 2019-02-21 stsp struct got_repository *repo)
4763 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4764 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4765 69efd4c4 2018-07-18 stsp char *path;
4767 a0de39f3 2019-08-09 stsp *new_view = NULL;
4769 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4770 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4771 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4773 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4775 69efd4c4 2018-07-18 stsp return err;
4777 b672a97a 2020-01-27 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0, 0);
4779 e5a0f69f 2018-08-18 stsp view_close(log_view);
4781 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4782 69efd4c4 2018-07-18 stsp free(path);
4783 69efd4c4 2018-07-18 stsp return err;
4786 69efd4c4 2018-07-18 stsp static const struct got_error *
4787 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4788 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4789 8b473291 2019-02-21 stsp struct got_repository *repo)
4791 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4792 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4793 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4795 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4797 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4798 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4799 ffd1d5e5 2018-06-23 stsp goto done;
4801 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4802 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4803 ffd1d5e5 2018-06-23 stsp goto done;
4806 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4807 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4808 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4809 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4810 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4811 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4812 6484ec90 2018-09-29 stsp goto done;
4814 8b473291 2019-02-21 stsp s->refs = refs;
4815 fb2756b9 2018-08-04 stsp s->repo = repo;
4817 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4819 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4820 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
4821 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
4822 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4824 c0b01bdb 2019-11-08 stsp goto done;
4825 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4826 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
4827 c0b01bdb 2019-11-08 stsp if (err) {
4828 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4829 c0b01bdb 2019-11-08 stsp goto done;
4831 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
4832 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
4833 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4834 c0b01bdb 2019-11-08 stsp if (err) {
4835 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4836 c0b01bdb 2019-11-08 stsp goto done;
4839 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
4840 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
4841 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4842 c0b01bdb 2019-11-08 stsp if (err) {
4843 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4844 c0b01bdb 2019-11-08 stsp goto done;
4847 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4848 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4849 11b20872 2019-11-08 stsp if (err) {
4850 11b20872 2019-11-08 stsp free_colors(&s->colors);
4851 11b20872 2019-11-08 stsp goto done;
4855 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4856 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4857 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4858 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4859 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4861 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4862 6484ec90 2018-09-29 stsp if (err) {
4863 fb2756b9 2018-08-04 stsp free(s->tree_label);
4864 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4866 ad80ab7b 2018-08-04 stsp return err;
4869 e5a0f69f 2018-08-18 stsp static const struct got_error *
4870 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4872 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4874 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4875 fb2756b9 2018-08-04 stsp free(s->tree_label);
4876 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4877 6484ec90 2018-09-29 stsp free(s->commit_id);
4878 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4879 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4880 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4881 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4882 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4883 ad80ab7b 2018-08-04 stsp free(parent);
4886 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4887 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4888 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4890 e5a0f69f 2018-08-18 stsp return NULL;
4893 ad80ab7b 2018-08-04 stsp static const struct got_error *
4894 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4896 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4898 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4899 7c32bd05 2019-06-22 stsp return NULL;
4902 7c32bd05 2019-06-22 stsp static int
4903 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4905 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4907 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4911 7c32bd05 2019-06-22 stsp static const struct got_error *
4912 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4914 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4915 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
4917 7c32bd05 2019-06-22 stsp if (!view->searching) {
4918 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4919 7c32bd05 2019-06-22 stsp return NULL;
4922 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4923 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4924 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4925 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
4926 56e0773d 2019-11-28 stsp s->selected_entry);
4928 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4930 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
4931 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4933 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
4934 56e0773d 2019-11-28 stsp s->selected_entry);
4937 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4938 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4940 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4943 7c32bd05 2019-06-22 stsp while (1) {
4944 56e0773d 2019-11-28 stsp if (te == NULL) {
4945 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4946 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4947 ac66afb8 2019-06-24 stsp return NULL;
4949 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4950 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4952 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4955 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
4956 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4957 56e0773d 2019-11-28 stsp s->matched_entry = te;
4961 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4962 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
4964 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
4967 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4968 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4969 7c32bd05 2019-06-22 stsp s->selected = 0;
4972 7c32bd05 2019-06-22 stsp return NULL;
4975 7c32bd05 2019-06-22 stsp static const struct got_error *
4976 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4978 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4979 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4980 e5a0f69f 2018-08-18 stsp char *parent_path;
4982 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4984 e5a0f69f 2018-08-18 stsp return err;
4986 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4987 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4988 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4989 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
4990 bddb1296 2019-11-08 stsp &s->colors);
4991 e5a0f69f 2018-08-18 stsp free(parent_path);
4993 669b5ffa 2018-10-07 stsp view_vborder(view);
4994 e5a0f69f 2018-08-18 stsp return err;
4997 e5a0f69f 2018-08-18 stsp static const struct got_error *
4998 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4999 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
5001 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5002 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5003 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
5004 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
5006 e5a0f69f 2018-08-18 stsp switch (ch) {
5008 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5011 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5013 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5014 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5015 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
5016 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
5017 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
5018 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5019 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5021 1e37a5c2 2019-05-12 jcs return err;
5022 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
5024 1e37a5c2 2019-05-12 jcs view_close(log_view);
5027 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
5028 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
5030 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5033 1e37a5c2 2019-05-12 jcs case KEY_UP:
5034 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5035 1e37a5c2 2019-05-12 jcs s->selected--;
5036 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
5039 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
5041 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
5042 56e0773d 2019-11-28 stsp s->tree, s->tree == s->root);
5044 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5045 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
5046 56e0773d 2019-11-28 stsp MAX(0, view->nlines - 4 - s->selected), s->tree,
5047 1e37a5c2 2019-05-12 jcs s->tree == s->root);
5048 1e37a5c2 2019-05-12 jcs s->selected = 0;
5049 56e0773d 2019-11-28 stsp if (got_object_tree_get_first_entry(s->tree) ==
5050 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
5051 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
5054 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5055 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5056 1e37a5c2 2019-05-12 jcs s->selected++;
5059 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5061 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5063 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
5064 56e0773d 2019-11-28 stsp s->last_displayed_entry, s->tree);
5066 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5067 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5069 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5070 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5071 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5074 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
5075 56e0773d 2019-11-28 stsp view->nlines, s->last_displayed_entry, s->tree);
5076 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
5077 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
5078 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
5079 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
5081 1e37a5c2 2019-05-12 jcs ndisplayed++;
5082 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5083 1e37a5c2 2019-05-12 jcs } while (te);
5084 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
5087 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5089 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5090 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5091 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5092 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5093 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5095 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5096 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5098 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5099 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5100 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5101 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5102 1e37a5c2 2019-05-12 jcs s->selected_entry =
5103 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5104 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5105 1e37a5c2 2019-05-12 jcs free(parent);
5106 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5107 56e0773d 2019-11-28 stsp s->selected_entry))) {
5108 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5109 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5110 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5113 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
5114 941e9f74 2019-05-21 stsp if (err) {
5115 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5118 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5119 56e0773d 2019-11-28 stsp s->selected_entry))) {
5120 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5121 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5122 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5124 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5125 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5126 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
5129 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5130 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5132 669b5ffa 2018-10-07 stsp return err;
5133 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
5134 669b5ffa 2018-10-07 stsp if (err) {
5135 1e37a5c2 2019-05-12 jcs view_close(blame_view);
5138 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
5139 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
5141 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5144 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5145 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
5146 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5152 ffd1d5e5 2018-06-23 stsp return err;
5155 ffd1d5e5 2018-06-23 stsp __dead static void
5156 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5159 74283ab8 2020-02-07 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path]\n",
5160 ffd1d5e5 2018-06-23 stsp getprogname());
5164 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5165 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5167 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5168 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5169 8b473291 2019-02-21 stsp struct got_reflist_head refs;
5170 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
5171 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5172 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
5173 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5174 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5176 5221c383 2018-08-01 stsp struct tog_view *view;
5178 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
5180 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
5181 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5182 d188b9a6 2019-01-04 stsp NULL) == -1)
5183 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
5186 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5187 ffd1d5e5 2018-06-23 stsp switch (ch) {
5189 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5192 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5193 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5194 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5198 17020d27 2019-03-07 stsp usage_tree();
5199 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5203 ffd1d5e5 2018-06-23 stsp argc -= optind;
5204 ffd1d5e5 2018-06-23 stsp argv += optind;
5206 74283ab8 2020-02-07 stsp if (argc != 0)
5207 74283ab8 2020-02-07 stsp usage_tree();
5209 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5210 52185f70 2019-02-05 stsp struct got_worktree *worktree;
5211 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
5212 52185f70 2019-02-05 stsp if (cwd == NULL)
5213 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
5214 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5215 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5216 52185f70 2019-02-05 stsp goto done;
5218 74283ab8 2020-02-07 stsp error = NULL;
5219 52185f70 2019-02-05 stsp if (worktree) {
5220 52185f70 2019-02-05 stsp free(cwd);
5221 52185f70 2019-02-05 stsp repo_path =
5222 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5223 52185f70 2019-02-05 stsp got_worktree_close(worktree);
5225 52185f70 2019-02-05 stsp repo_path = cwd;
5226 52185f70 2019-02-05 stsp if (repo_path == NULL) {
5227 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5228 52185f70 2019-02-05 stsp goto done;
5232 a915003a 2019-02-05 stsp init_curses();
5234 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5235 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5236 52185f70 2019-02-05 stsp goto done;
5238 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5239 c02c541e 2019-03-29 stsp if (error)
5240 c02c541e 2019-03-29 stsp goto done;
5242 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5243 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5244 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5245 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5246 ffd1d5e5 2018-06-23 stsp goto done;
5248 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5249 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5250 ffd1d5e5 2018-06-23 stsp goto done;
5252 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5253 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5254 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5255 ffd1d5e5 2018-06-23 stsp goto done;
5257 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5258 8b473291 2019-02-21 stsp if (error)
5259 8b473291 2019-02-21 stsp goto done;
5261 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5262 5221c383 2018-08-01 stsp if (view == NULL) {
5263 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5264 5221c383 2018-08-01 stsp goto done;
5266 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
5267 ad80ab7b 2018-08-04 stsp if (error)
5268 ad80ab7b 2018-08-04 stsp goto done;
5269 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5271 52185f70 2019-02-05 stsp free(repo_path);
5272 ffd1d5e5 2018-06-23 stsp free(commit_id);
5273 ffd1d5e5 2018-06-23 stsp if (commit)
5274 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5276 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5278 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5279 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5280 ffd1d5e5 2018-06-23 stsp return error;
5283 ce5b7c56 2019-07-09 stsp static void
5284 ce5b7c56 2019-07-09 stsp list_commands(void)
5288 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
5289 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
5290 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
5291 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
5293 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
5296 4ed7e80c 2018-05-20 stsp __dead static void
5297 ce5b7c56 2019-07-09 stsp usage(int hflag)
5299 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5300 53ccebc2 2019-07-30 stsp getprogname());
5301 ce5b7c56 2019-07-09 stsp if (hflag)
5302 ce5b7c56 2019-07-09 stsp list_commands();
5306 c2301be8 2018-04-30 stsp static char **
5307 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
5309 c2301be8 2018-04-30 stsp char **argv;
5310 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
5312 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
5313 c2301be8 2018-04-30 stsp if (argv == NULL)
5314 c2301be8 2018-04-30 stsp err(1, "calloc");
5315 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
5316 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
5317 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5318 c2301be8 2018-04-30 stsp if (arg1) {
5319 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
5320 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
5321 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5324 c2301be8 2018-04-30 stsp return argv;
5328 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
5330 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
5331 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
5332 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
5333 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
5334 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
5335 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
5336 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
5339 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
5341 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5342 9f7d7167 2018-04-29 stsp switch (ch) {
5344 9f7d7167 2018-04-29 stsp hflag = 1;
5347 53ccebc2 2019-07-30 stsp Vflag = 1;
5350 ce5b7c56 2019-07-09 stsp usage(hflag);
5351 9f7d7167 2018-04-29 stsp /* NOTREACHED */
5355 9f7d7167 2018-04-29 stsp argc -= optind;
5356 9f7d7167 2018-04-29 stsp argv += optind;
5357 9f7d7167 2018-04-29 stsp optind = 0;
5358 c2301be8 2018-04-30 stsp optreset = 1;
5360 53ccebc2 2019-07-30 stsp if (Vflag) {
5361 53ccebc2 2019-07-30 stsp got_version_print_str();
5365 c2301be8 2018-04-30 stsp if (argc == 0) {
5366 f29d3e89 2018-06-23 stsp if (hflag)
5367 ce5b7c56 2019-07-09 stsp usage(hflag);
5368 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
5369 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
5370 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
5375 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
5376 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
5377 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
5378 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
5379 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
5384 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
5385 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
5386 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
5387 ce5b7c56 2019-07-09 stsp list_commands();
5392 3642c4c6 2019-07-09 stsp if (hflag)
5393 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
5395 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5398 c2301be8 2018-04-30 stsp free(cmd_argv);
5399 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
5400 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);