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 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
26 9f7d7167 2018-04-29 stsp #include <panel.h>
27 9f7d7167 2018-04-29 stsp #include <locale.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <util.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
44 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
85 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
88 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
91 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
92 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
93 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
94 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 d6b05b5b 2018-08-04 stsp enum tog_view_type {
98 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
99 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
100 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
101 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
104 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
106 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
107 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
108 ba4f502b 2018-08-04 stsp struct got_object_id *id;
109 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
112 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
113 ba4f502b 2018-08-04 stsp struct commit_queue {
114 ba4f502b 2018-08-04 stsp int ncommits;
115 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
118 f26dddb7 2019-11-08 stsp struct tog_color {
119 f26dddb7 2019-11-08 stsp SIMPLEQ_ENTRY(tog_color) entry;
120 6d17833f 2019-11-08 stsp regex_t regex;
121 6d17833f 2019-11-08 stsp short colorpair;
123 f26dddb7 2019-11-08 stsp SIMPLEQ_HEAD(tog_colors, tog_color);
125 11b20872 2019-11-08 stsp static const struct got_error *
126 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
127 11b20872 2019-11-08 stsp int idx, short color)
129 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
130 11b20872 2019-11-08 stsp struct tog_color *tc;
131 11b20872 2019-11-08 stsp int regerr = 0;
133 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
134 11b20872 2019-11-08 stsp return NULL;
136 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
138 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
139 11b20872 2019-11-08 stsp if (tc == NULL)
140 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
141 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
142 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
143 11b20872 2019-11-08 stsp if (regerr) {
144 11b20872 2019-11-08 stsp static char regerr_msg[512];
145 11b20872 2019-11-08 stsp static char err_msg[512];
146 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
147 11b20872 2019-11-08 stsp sizeof(regerr_msg));
148 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
149 11b20872 2019-11-08 stsp regerr_msg);
150 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
152 11b20872 2019-11-08 stsp return err;
154 11b20872 2019-11-08 stsp tc->colorpair = idx;
155 11b20872 2019-11-08 stsp SIMPLEQ_INSERT_HEAD(colors, tc, entry);
156 11b20872 2019-11-08 stsp return NULL;
159 11b20872 2019-11-08 stsp static void
160 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
162 11b20872 2019-11-08 stsp struct tog_color *tc;
164 11b20872 2019-11-08 stsp while (!SIMPLEQ_EMPTY(colors)) {
165 11b20872 2019-11-08 stsp tc = SIMPLEQ_FIRST(colors);
166 11b20872 2019-11-08 stsp SIMPLEQ_REMOVE_HEAD(colors, entry);
167 11b20872 2019-11-08 stsp regfree(&tc->regex);
172 11b20872 2019-11-08 stsp struct tog_color *
173 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
175 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
177 11b20872 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
178 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
182 11b20872 2019-11-08 stsp return NULL;
186 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
188 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
189 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
190 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
191 11b20872 2019-11-08 stsp return COLOR_CYAN;
192 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
193 11b20872 2019-11-08 stsp return COLOR_YELLOW;
194 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
195 11b20872 2019-11-08 stsp return COLOR_GREEN;
196 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
197 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
198 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
199 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
200 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
201 91b8c405 2020-01-25 stsp return COLOR_CYAN;
202 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
203 11b20872 2019-11-08 stsp return COLOR_GREEN;
204 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
205 11b20872 2019-11-08 stsp return COLOR_GREEN;
206 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
207 11b20872 2019-11-08 stsp return COLOR_CYAN;
208 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
209 11b20872 2019-11-08 stsp return COLOR_YELLOW;
215 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
217 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
219 11b20872 2019-11-08 stsp if (val == NULL)
220 11b20872 2019-11-08 stsp return default_color_value(envvar);
222 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
223 11b20872 2019-11-08 stsp return COLOR_BLACK;
224 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
225 11b20872 2019-11-08 stsp return COLOR_RED;
226 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
227 11b20872 2019-11-08 stsp return COLOR_GREEN;
228 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
229 11b20872 2019-11-08 stsp return COLOR_YELLOW;
230 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
231 11b20872 2019-11-08 stsp return COLOR_BLUE;
232 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
233 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
234 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
235 11b20872 2019-11-08 stsp return COLOR_CYAN;
236 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
237 11b20872 2019-11-08 stsp return COLOR_WHITE;
238 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
241 11b20872 2019-11-08 stsp return default_color_value(envvar);
245 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
246 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
248 15a087fe 2019-02-21 stsp int first_displayed_line;
249 15a087fe 2019-02-21 stsp int last_displayed_line;
251 15a087fe 2019-02-21 stsp int diff_context;
252 15a087fe 2019-02-21 stsp struct got_repository *repo;
253 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
254 bddb1296 2019-11-08 stsp struct tog_colors colors;
255 fe621944 2020-11-10 stsp size_t nlines;
256 f44b1f58 2020-02-02 tracey off_t *line_offsets;
257 f44b1f58 2020-02-02 tracey int matched_line;
258 f44b1f58 2020-02-02 tracey int selected_line;
260 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
261 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
264 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
266 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
267 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
268 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
269 1a76625f 2018-10-22 stsp int commits_needed;
270 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
271 1a76625f 2018-10-22 stsp struct commit_queue *commits;
272 1a76625f 2018-10-22 stsp const char *in_repo_path;
273 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
274 1a76625f 2018-10-22 stsp struct got_repository *repo;
275 1a76625f 2018-10-22 stsp int log_complete;
276 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
277 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
278 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
279 13add988 2019-10-15 stsp int *searching;
280 13add988 2019-10-15 stsp int *search_next_done;
281 13add988 2019-10-15 stsp regex_t *regex;
284 1a76625f 2018-10-22 stsp struct tog_log_view_state {
285 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
286 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
287 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
288 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
289 b01e7d3b 2018-08-04 stsp int selected;
290 b01e7d3b 2018-08-04 stsp char *in_repo_path;
291 d01904d4 2019-06-25 stsp const char *head_ref_name;
292 b672a97a 2020-01-27 stsp int log_branches;
293 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
294 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
295 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
296 1a76625f 2018-10-22 stsp sig_atomic_t quit;
297 1a76625f 2018-10-22 stsp pthread_t thread;
298 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
299 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
300 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
301 11b20872 2019-11-08 stsp struct tog_colors colors;
304 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
305 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
306 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
307 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
308 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
309 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
310 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
311 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
312 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
313 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
314 11b20872 2019-11-08 stsp #define TOG_COLOR_DATE 11
316 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
317 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
318 e9424729 2018-08-04 stsp int nlines;
320 e9424729 2018-08-04 stsp struct tog_view *view;
321 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
325 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
326 e9424729 2018-08-04 stsp const char *path;
327 e9424729 2018-08-04 stsp struct got_repository *repo;
328 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
329 e9424729 2018-08-04 stsp int *complete;
330 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
331 fc06ba56 2019-08-22 stsp void *cancel_arg;
334 e9424729 2018-08-04 stsp struct tog_blame {
336 e9424729 2018-08-04 stsp size_t filesize;
337 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
338 6fcac457 2018-11-19 stsp int nlines;
339 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
340 e9424729 2018-08-04 stsp pthread_t thread;
341 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
342 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
343 e9424729 2018-08-04 stsp const char *path;
346 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
347 7cbe629d 2018-08-04 stsp int first_displayed_line;
348 7cbe629d 2018-08-04 stsp int last_displayed_line;
349 7cbe629d 2018-08-04 stsp int selected_line;
350 7cbe629d 2018-08-04 stsp int blame_complete;
353 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
354 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
355 e5a0f69f 2018-08-18 stsp char *path;
356 7cbe629d 2018-08-04 stsp struct got_repository *repo;
357 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
358 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
359 e9424729 2018-08-04 stsp struct tog_blame blame;
360 6c4c42e0 2019-06-24 stsp int matched_line;
361 11b20872 2019-11-08 stsp struct tog_colors colors;
364 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
365 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
366 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
367 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
368 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
369 ad80ab7b 2018-08-04 stsp int selected;
372 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
374 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
375 ad80ab7b 2018-08-04 stsp char *tree_label;
376 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
377 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
378 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
379 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
380 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
381 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
382 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
383 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
384 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
385 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
386 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
387 bddb1296 2019-11-08 stsp struct tog_colors colors;
391 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
393 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
394 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
395 669b5ffa 2018-10-07 stsp * there is enough screen estate.
397 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
398 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
400 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
401 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
402 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
404 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
405 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
407 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
409 cc3c9aac 2018-08-01 stsp struct tog_view {
410 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
411 26ed57b2 2018-05-19 stsp WINDOW *window;
412 26ed57b2 2018-05-19 stsp PANEL *panel;
413 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
414 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
415 1004088d 2018-09-29 stsp int focussed;
416 669b5ffa 2018-10-07 stsp struct tog_view *parent;
417 669b5ffa 2018-10-07 stsp struct tog_view *child;
418 669b5ffa 2018-10-07 stsp int child_focussed;
420 5dc9f4bc 2018-08-04 stsp /* type-specific state */
421 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
423 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
424 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
425 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
426 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
429 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
430 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
431 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
432 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
434 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
435 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
436 60493ae3 2019-06-20 stsp int searching;
437 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
438 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
439 60493ae3 2019-06-20 stsp int search_next_done;
440 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
441 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
442 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
443 1803e47f 2019-06-22 stsp regex_t regex;
444 41605754 2020-11-12 stsp regmatch_t regmatch;
447 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
448 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
449 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
450 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
451 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
452 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
453 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
454 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
455 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
457 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
458 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
459 f135c941 2020-02-20 stsp struct got_repository *, const char *, const char *, int);
460 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
461 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
462 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
463 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
464 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
465 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
467 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
468 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
469 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
470 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
471 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
472 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
473 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
474 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
476 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
477 55cccc34 2020-02-20 stsp struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
478 55cccc34 2020-02-20 stsp struct got_repository *);
479 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
480 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
481 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
482 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
483 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
484 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
486 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
487 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
488 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
490 25791caa 2018-10-24 stsp static void
491 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
493 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
496 83baff54 2019-08-12 stsp static void
497 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
499 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
502 61266923 2020-01-14 stsp static void
503 61266923 2020-01-14 stsp tog_sigcont(int signo)
505 61266923 2020-01-14 stsp tog_sigcont_received = 1;
508 e5a0f69f 2018-08-18 stsp static const struct got_error *
509 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
511 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
513 669b5ffa 2018-10-07 stsp if (view->child) {
514 669b5ffa 2018-10-07 stsp view_close(view->child);
515 669b5ffa 2018-10-07 stsp view->child = NULL;
517 e5a0f69f 2018-08-18 stsp if (view->close)
518 e5a0f69f 2018-08-18 stsp err = view->close(view);
519 ea5e7bb5 2018-08-01 stsp if (view->panel)
520 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
521 ea5e7bb5 2018-08-01 stsp if (view->window)
522 ea5e7bb5 2018-08-01 stsp delwin(view->window);
523 ea5e7bb5 2018-08-01 stsp free(view);
524 e5a0f69f 2018-08-18 stsp return err;
527 ea5e7bb5 2018-08-01 stsp static struct tog_view *
528 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
529 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
531 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
533 ea5e7bb5 2018-08-01 stsp if (view == NULL)
534 ea5e7bb5 2018-08-01 stsp return NULL;
536 d6b05b5b 2018-08-04 stsp view->type = type;
537 f7d12f7e 2018-08-01 stsp view->lines = LINES;
538 f7d12f7e 2018-08-01 stsp view->cols = COLS;
539 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
540 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
541 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
542 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
543 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
544 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
545 96a765a8 2018-08-04 stsp view_close(view);
546 ea5e7bb5 2018-08-01 stsp return NULL;
548 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
549 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
550 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
551 96a765a8 2018-08-04 stsp view_close(view);
552 ea5e7bb5 2018-08-01 stsp return NULL;
555 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
556 ea5e7bb5 2018-08-01 stsp return view;
560 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
562 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
564 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
567 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
569 5c60c32a 2018-10-18 stsp static const struct got_error *
570 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
572 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
574 5c60c32a 2018-10-18 stsp view->begin_y = 0;
575 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
576 5c60c32a 2018-10-18 stsp view->nlines = LINES;
577 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
578 5c60c32a 2018-10-18 stsp view->lines = LINES;
579 5c60c32a 2018-10-18 stsp view->cols = COLS;
580 5c60c32a 2018-10-18 stsp err = view_resize(view);
582 5c60c32a 2018-10-18 stsp return err;
584 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
585 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
587 5c60c32a 2018-10-18 stsp return NULL;
590 5c60c32a 2018-10-18 stsp static const struct got_error *
591 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
593 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
595 5c60c32a 2018-10-18 stsp view->begin_x = 0;
596 5c60c32a 2018-10-18 stsp view->begin_y = 0;
597 5c60c32a 2018-10-18 stsp view->nlines = LINES;
598 5c60c32a 2018-10-18 stsp view->ncols = COLS;
599 5c60c32a 2018-10-18 stsp view->lines = LINES;
600 5c60c32a 2018-10-18 stsp view->cols = COLS;
601 5c60c32a 2018-10-18 stsp err = view_resize(view);
603 5c60c32a 2018-10-18 stsp return err;
605 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
606 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
608 5c60c32a 2018-10-18 stsp return NULL;
612 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
614 5c60c32a 2018-10-18 stsp return view->parent == NULL;
617 4d8c2215 2018-08-19 stsp static const struct got_error *
618 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
620 f7d12f7e 2018-08-01 stsp int nlines, ncols;
622 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
623 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
625 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
627 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
628 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
630 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
632 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
633 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
634 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
635 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
636 25791caa 2018-10-24 stsp wclear(view->window);
638 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
639 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
640 0cf4efb1 2018-09-29 stsp view->lines = LINES;
641 0cf4efb1 2018-09-29 stsp view->cols = COLS;
643 6e3e5d9c 2018-10-18 stsp if (view->child) {
644 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
645 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
646 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
647 5c60c32a 2018-10-18 stsp if (view->child->focussed)
648 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
650 5c60c32a 2018-10-18 stsp show_panel(view->panel);
652 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
653 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
657 5c60c32a 2018-10-18 stsp return NULL;
660 669b5ffa 2018-10-07 stsp static const struct got_error *
661 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
663 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
665 669b5ffa 2018-10-07 stsp if (view->child == NULL)
666 669b5ffa 2018-10-07 stsp return NULL;
668 669b5ffa 2018-10-07 stsp err = view_close(view->child);
669 669b5ffa 2018-10-07 stsp view->child = NULL;
670 669b5ffa 2018-10-07 stsp return err;
673 669b5ffa 2018-10-07 stsp static const struct got_error *
674 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
676 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
678 669b5ffa 2018-10-07 stsp view->child = child;
679 669b5ffa 2018-10-07 stsp child->parent = view;
680 669b5ffa 2018-10-07 stsp return err;
684 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
686 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
689 34bc9ec9 2019-02-22 stsp static void
690 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
692 25791caa 2018-10-24 stsp int cols, lines;
693 25791caa 2018-10-24 stsp struct winsize size;
695 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
696 25791caa 2018-10-24 stsp cols = 80; /* Default */
697 25791caa 2018-10-24 stsp lines = 24;
699 25791caa 2018-10-24 stsp cols = size.ws_col;
700 25791caa 2018-10-24 stsp lines = size.ws_row;
702 25791caa 2018-10-24 stsp resize_term(lines, cols);
705 2b49a8ae 2019-06-22 stsp static const struct got_error *
706 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
708 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
709 2b49a8ae 2019-06-22 stsp char pattern[1024];
712 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
713 2b49a8ae 2019-06-22 stsp return NULL;
715 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
716 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
718 2b49a8ae 2019-06-22 stsp nocbreak();
720 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
723 2b49a8ae 2019-06-22 stsp if (ret == ERR)
724 2b49a8ae 2019-06-22 stsp return NULL;
726 2b49a8ae 2019-06-22 stsp if (view->searching) {
727 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
728 2b49a8ae 2019-06-22 stsp view->searching = 0;
731 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
732 7c32bd05 2019-06-22 stsp err = view->search_start(view);
734 7c32bd05 2019-06-22 stsp regfree(&view->regex);
735 7c32bd05 2019-06-22 stsp return err;
737 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
738 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
739 2b49a8ae 2019-06-22 stsp view->search_next(view);
742 2b49a8ae 2019-06-22 stsp return NULL;
745 0cf4efb1 2018-09-29 stsp static const struct got_error *
746 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
747 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
748 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
750 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
751 669b5ffa 2018-10-07 stsp struct tog_view *v;
752 1a76625f 2018-10-22 stsp int ch, errcode;
754 e5a0f69f 2018-08-18 stsp *new = NULL;
755 e5a0f69f 2018-08-18 stsp *dead = NULL;
756 0cf4efb1 2018-09-29 stsp *focus = NULL;
758 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
759 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
760 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
761 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
763 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
764 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
765 82954512 2020-02-03 stsp if (errcode)
766 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
767 82954512 2020-02-03 stsp "pthread_mutex_unlock");
768 82954512 2020-02-03 stsp pthread_yield();
769 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
770 82954512 2020-02-03 stsp if (errcode)
771 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
772 82954512 2020-02-03 stsp "pthread_mutex_lock");
773 60493ae3 2019-06-20 stsp view->search_next(view);
774 60493ae3 2019-06-20 stsp return NULL;
777 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
778 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
779 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
780 1a76625f 2018-10-22 stsp if (errcode)
781 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
782 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
783 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
784 1a76625f 2018-10-22 stsp if (errcode)
785 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
786 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
788 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
789 25791caa 2018-10-24 stsp tog_resizeterm();
790 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
791 61266923 2020-01-14 stsp tog_sigcont_received = 0;
792 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
793 25791caa 2018-10-24 stsp err = view_resize(v);
795 25791caa 2018-10-24 stsp return err;
796 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
798 25791caa 2018-10-24 stsp return err;
802 e5a0f69f 2018-08-18 stsp switch (ch) {
806 1e37a5c2 2019-05-12 jcs if (view->child) {
807 1e37a5c2 2019-05-12 jcs *focus = view->child;
808 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
809 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
810 1e37a5c2 2019-05-12 jcs *focus = view->parent;
811 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
815 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
816 1e37a5c2 2019-05-12 jcs *dead = view;
822 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
823 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
825 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
826 669b5ffa 2018-10-07 stsp *focus = view->child;
827 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
828 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
830 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
833 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
834 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
836 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
837 1e37a5c2 2019-05-12 jcs *focus = view;
838 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
839 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
841 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
845 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
846 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
849 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
852 60493ae3 2019-06-20 stsp if (view->search_start)
853 2b49a8ae 2019-06-22 stsp view_search_start(view);
855 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
859 8f4ed634 2020-03-26 stsp if (view->search_next) {
860 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
861 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
862 60493ae3 2019-06-20 stsp view->search_next_done = 0;
863 60493ae3 2019-06-20 stsp view->search_next(view);
865 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
868 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
872 e5a0f69f 2018-08-18 stsp return err;
876 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
878 0cf4efb1 2018-09-29 stsp PANEL *panel;
879 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
881 669b5ffa 2018-10-07 stsp if (view->parent)
882 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
884 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
885 0cf4efb1 2018-09-29 stsp if (panel == NULL)
888 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
889 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
890 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
894 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
896 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
897 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
899 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
901 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
904 669b5ffa 2018-10-07 stsp return view->focussed;
907 bcbd79e2 2018-08-19 stsp static const struct got_error *
908 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
910 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
911 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
912 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
913 fd823528 2018-10-22 stsp int fast_refresh = 10;
914 1a76625f 2018-10-22 stsp int done = 0, errcode;
916 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
917 1a76625f 2018-10-22 stsp if (errcode)
918 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
920 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
921 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
923 a81bf10d 2018-09-29 stsp main_view = view;
924 1004088d 2018-09-29 stsp view->focussed = 1;
925 878940b7 2018-09-29 stsp err = view->show(view);
927 0cf4efb1 2018-09-29 stsp return err;
928 0cf4efb1 2018-09-29 stsp update_panels();
929 0cf4efb1 2018-09-29 stsp doupdate();
930 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
931 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
932 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
933 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
935 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
936 e4197bf9 2018-08-18 stsp view, &views);
939 e5a0f69f 2018-08-18 stsp if (dead_view) {
940 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
942 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
943 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
944 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
945 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
946 669b5ffa 2018-10-07 stsp prev = view->parent;
948 669b5ffa 2018-10-07 stsp if (dead_view->parent)
949 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
951 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
953 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
954 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
957 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
958 0cf4efb1 2018-09-29 stsp if (focus_view)
959 0cf4efb1 2018-09-29 stsp view = focus_view;
960 669b5ffa 2018-10-07 stsp else if (prev)
961 669b5ffa 2018-10-07 stsp view = prev;
962 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
963 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
964 0cf4efb1 2018-09-29 stsp tog_view_list_head);
966 0cf4efb1 2018-09-29 stsp view = NULL;
967 669b5ffa 2018-10-07 stsp if (view) {
968 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
969 669b5ffa 2018-10-07 stsp focus_view = view->child;
971 669b5ffa 2018-10-07 stsp focus_view = view;
975 bcbd79e2 2018-08-19 stsp if (new_view) {
976 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
977 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
978 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
979 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
981 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
982 86c66b02 2018-10-18 stsp err = view_close(v);
987 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
988 fed7eaa8 2018-10-24 stsp view = new_view;
989 878940b7 2018-09-29 stsp if (focus_view == NULL)
990 878940b7 2018-09-29 stsp focus_view = new_view;
992 0cf4efb1 2018-09-29 stsp if (focus_view) {
993 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
995 0cf4efb1 2018-09-29 stsp view->focussed = 0;
996 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
997 0cf4efb1 2018-09-29 stsp view = focus_view;
998 878940b7 2018-09-29 stsp if (new_view)
999 878940b7 2018-09-29 stsp show_panel(new_view->panel);
1000 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
1001 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
1003 669b5ffa 2018-10-07 stsp if (view) {
1004 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
1005 758194b5 2018-10-24 stsp view->focussed = 1;
1006 758194b5 2018-10-24 stsp show_panel(view->panel);
1007 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
1008 758194b5 2018-10-24 stsp show_panel(view->child->panel);
1009 1a76625f 2018-10-22 stsp focus_view = view;
1011 669b5ffa 2018-10-07 stsp if (view->parent) {
1012 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1014 1a76625f 2018-10-22 stsp goto done;
1016 669b5ffa 2018-10-07 stsp err = view->show(view);
1018 1a76625f 2018-10-22 stsp goto done;
1019 669b5ffa 2018-10-07 stsp if (view->child) {
1020 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1022 1a76625f 2018-10-22 stsp goto done;
1024 1a76625f 2018-10-22 stsp update_panels();
1025 1a76625f 2018-10-22 stsp doupdate();
1029 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1030 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1031 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1032 e5a0f69f 2018-08-18 stsp view_close(view);
1035 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1036 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1037 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1039 e5a0f69f 2018-08-18 stsp return err;
1042 4ed7e80c 2018-05-20 stsp __dead static void
1043 9f7d7167 2018-04-29 stsp usage_log(void)
1046 c70c5802 2018-08-01 stsp fprintf(stderr,
1047 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1048 9f7d7167 2018-04-29 stsp getprogname());
1052 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1053 80ddbec8 2018-04-29 stsp static const struct got_error *
1054 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1056 00dfcb92 2018-06-11 stsp char *vis = NULL;
1057 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1059 963b370f 2018-05-20 stsp *ws = NULL;
1060 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1061 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1062 00dfcb92 2018-06-11 stsp int vislen;
1063 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1064 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1066 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1067 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1069 00dfcb92 2018-06-11 stsp return err;
1070 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1071 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1072 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1073 a7f50699 2018-06-11 stsp goto done;
1077 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1078 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1079 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1080 a7f50699 2018-06-11 stsp goto done;
1083 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1084 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1086 00dfcb92 2018-06-11 stsp free(vis);
1087 963b370f 2018-05-20 stsp if (err) {
1088 963b370f 2018-05-20 stsp free(*ws);
1089 963b370f 2018-05-20 stsp *ws = NULL;
1090 963b370f 2018-05-20 stsp *wlen = 0;
1092 963b370f 2018-05-20 stsp return err;
1095 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1096 963b370f 2018-05-20 stsp static const struct got_error *
1097 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1098 27a741e5 2019-09-11 stsp int col_tab_align)
1100 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1101 963b370f 2018-05-20 stsp int cols = 0;
1102 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1103 963b370f 2018-05-20 stsp size_t wlen;
1106 963b370f 2018-05-20 stsp *wlinep = NULL;
1107 b700b5d6 2018-07-10 stsp *widthp = 0;
1109 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1111 963b370f 2018-05-20 stsp return err;
1114 27a741e5 2019-09-11 stsp while (i < wlen) {
1115 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1117 27a741e5 2019-09-11 stsp if (width == 0) {
1122 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1123 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1125 27a741e5 2019-09-11 stsp cols += width;
1127 27a741e5 2019-09-11 stsp } else if (width == -1) {
1128 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1129 27a741e5 2019-09-11 stsp width = TABSIZE -
1130 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1131 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1133 27a741e5 2019-09-11 stsp cols += width;
1137 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1138 963b370f 2018-05-20 stsp goto done;
1141 963b370f 2018-05-20 stsp wline[i] = L'\0';
1142 b700b5d6 2018-07-10 stsp if (widthp)
1143 b700b5d6 2018-07-10 stsp *widthp = cols;
1146 963b370f 2018-05-20 stsp free(wline);
1148 963b370f 2018-05-20 stsp *wlinep = wline;
1149 963b370f 2018-05-20 stsp return err;
1152 8b473291 2019-02-21 stsp static const struct got_error*
1153 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1154 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1156 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1157 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1159 8b473291 2019-02-21 stsp const char *name;
1161 8b473291 2019-02-21 stsp *refs_str = NULL;
1163 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1164 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1165 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1168 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1169 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1171 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1172 8b473291 2019-02-21 stsp name += 5;
1173 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1175 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1176 8b473291 2019-02-21 stsp name += 6;
1177 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1178 8b473291 2019-02-21 stsp name += 8;
1179 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1180 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1183 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1186 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1187 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1188 5d844a1e 2019-08-13 stsp if (err) {
1189 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1190 48cae60d 2020-09-22 stsp free(ref_id);
1193 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1194 5d844a1e 2019-08-13 stsp err = NULL;
1195 5d844a1e 2019-08-13 stsp tag = NULL;
1198 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1199 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1200 48cae60d 2020-09-22 stsp free(ref_id);
1202 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1203 52b5abe1 2019-08-13 stsp if (cmp != 0)
1205 8b473291 2019-02-21 stsp s = *refs_str;
1206 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1207 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1208 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1210 8b473291 2019-02-21 stsp *refs_str = NULL;
1216 8b473291 2019-02-21 stsp return err;
1219 963b370f 2018-05-20 stsp static const struct got_error *
1220 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1221 27a741e5 2019-09-11 stsp int col_tab_align)
1223 5813d178 2019-03-09 stsp char *smallerthan, *at;
1225 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1226 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1227 5813d178 2019-03-09 stsp author = smallerthan + 1;
1228 5813d178 2019-03-09 stsp at = strchr(author, '@');
1230 5813d178 2019-03-09 stsp *at = '\0';
1231 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1234 5813d178 2019-03-09 stsp static const struct got_error *
1235 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1236 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1237 11b20872 2019-11-08 stsp const size_t date_display_cols, int author_display_cols,
1238 11b20872 2019-11-08 stsp struct tog_colors *colors)
1240 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1241 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1242 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1243 5813d178 2019-03-09 stsp char *author = NULL;
1244 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1245 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1246 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1247 bb737323 2018-05-20 stsp int col, limit;
1248 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1249 ccb26ccd 2018-11-05 stsp struct tm tm;
1250 45d799e2 2018-12-23 stsp time_t committer_time;
1251 11b20872 2019-11-08 stsp struct tog_color *tc;
1253 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1254 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1255 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1256 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1257 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1258 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1260 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1261 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1263 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1264 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_DATE);
1266 11b20872 2019-11-08 stsp wattr_on(view->window,
1267 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1268 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1270 11b20872 2019-11-08 stsp wattr_off(view->window,
1271 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1272 27a741e5 2019-09-11 stsp col = limit;
1273 b39d25c7 2018-07-10 stsp if (col > avail)
1274 b39d25c7 2018-07-10 stsp goto done;
1276 6570a66d 2019-11-08 stsp if (avail >= 120) {
1277 6570a66d 2019-11-08 stsp char *id_str;
1278 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1280 6570a66d 2019-11-08 stsp goto done;
1281 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1283 11b20872 2019-11-08 stsp wattr_on(view->window,
1284 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1285 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1287 11b20872 2019-11-08 stsp wattr_off(view->window,
1288 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1289 6570a66d 2019-11-08 stsp free(id_str);
1291 6570a66d 2019-11-08 stsp if (col > avail)
1292 6570a66d 2019-11-08 stsp goto done;
1295 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1296 5813d178 2019-03-09 stsp if (author == NULL) {
1297 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1298 80ddbec8 2018-04-29 stsp goto done;
1300 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1302 bb737323 2018-05-20 stsp goto done;
1303 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_AUTHOR);
1305 11b20872 2019-11-08 stsp wattr_on(view->window,
1306 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1307 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1309 11b20872 2019-11-08 stsp wattr_off(view->window,
1310 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1311 bb737323 2018-05-20 stsp col += author_width;
1312 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1313 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1315 bb737323 2018-05-20 stsp author_width++;
1317 9c2eaf34 2018-05-20 stsp if (col > avail)
1318 9c2eaf34 2018-05-20 stsp goto done;
1320 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1322 6d9fbc00 2018-04-29 stsp goto done;
1323 bb737323 2018-05-20 stsp logmsg = logmsg0;
1324 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1326 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1327 bb737323 2018-05-20 stsp if (newline)
1328 bb737323 2018-05-20 stsp *newline = '\0';
1329 bb737323 2018-05-20 stsp limit = avail - col;
1330 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1332 bb737323 2018-05-20 stsp goto done;
1333 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1334 bb737323 2018-05-20 stsp col += logmsg_width;
1335 27a741e5 2019-09-11 stsp while (col < avail) {
1336 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1340 80ddbec8 2018-04-29 stsp free(logmsg0);
1341 bb737323 2018-05-20 stsp free(wlogmsg);
1342 5813d178 2019-03-09 stsp free(author);
1343 bb737323 2018-05-20 stsp free(wauthor);
1344 80ddbec8 2018-04-29 stsp free(line);
1345 80ddbec8 2018-04-29 stsp return err;
1348 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1349 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1350 899d86c2 2018-05-10 stsp struct got_object_id *id)
1352 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1354 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1355 80ddbec8 2018-04-29 stsp if (entry == NULL)
1356 899d86c2 2018-05-10 stsp return NULL;
1358 899d86c2 2018-05-10 stsp entry->id = id;
1359 99db9666 2018-05-07 stsp entry->commit = commit;
1360 899d86c2 2018-05-10 stsp return entry;
1363 99db9666 2018-05-07 stsp static void
1364 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1366 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1368 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1369 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1370 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1371 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1372 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1373 99db9666 2018-05-07 stsp free(entry);
1376 99db9666 2018-05-07 stsp static void
1377 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1379 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1380 99db9666 2018-05-07 stsp pop_commit(commits);
1383 c4972b91 2018-05-07 stsp static const struct got_error *
1384 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1385 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1387 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1388 13add988 2019-10-15 stsp regmatch_t regmatch;
1389 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1391 13add988 2019-10-15 stsp *have_match = 0;
1393 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1395 13add988 2019-10-15 stsp return err;
1397 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1399 13add988 2019-10-15 stsp goto done;
1401 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1402 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1403 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1404 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1405 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1406 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1407 13add988 2019-10-15 stsp *have_match = 1;
1409 13add988 2019-10-15 stsp free(id_str);
1410 13add988 2019-10-15 stsp free(logmsg);
1411 13add988 2019-10-15 stsp return err;
1414 13add988 2019-10-15 stsp static const struct got_error *
1415 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1416 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1417 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1419 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1420 7c1452c1 2020-03-26 stsp int nqueued = 0;
1423 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1424 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1425 1a76625f 2018-10-22 stsp * while updating the display.
1427 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1428 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1429 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1430 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1431 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1432 1a76625f 2018-10-22 stsp int errcode;
1434 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1435 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1438 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1441 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1442 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1443 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1447 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1448 1a76625f 2018-10-22 stsp if (errcode) {
1449 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1450 13add988 2019-10-15 stsp "pthread_mutex_lock");
1454 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1455 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1456 ecb28ae0 2018-07-16 stsp nqueued++;
1457 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1459 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1460 7c1452c1 2020-03-26 stsp int have_match;
1461 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1464 7c1452c1 2020-03-26 stsp if (have_match)
1465 8f4ed634 2020-03-26 stsp *search_next_done = TOG_SEARCH_HAVE_MORE;
1468 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1469 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1470 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1471 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1476 9ba79e04 2018-06-11 stsp return err;
1479 0553a4e3 2018-04-30 stsp static const struct got_error *
1480 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1481 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1482 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1483 11b20872 2019-11-08 stsp struct got_reflist_head *refs, const char *path, int commits_needed,
1484 11b20872 2019-11-08 stsp struct tog_colors *colors)
1486 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1487 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1488 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1489 60493ae3 2019-06-20 stsp int width;
1490 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1491 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1492 8b473291 2019-02-21 stsp char *refs_str = NULL;
1493 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1494 11b20872 2019-11-08 stsp struct tog_color *tc;
1495 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1497 e0d42f60 2018-07-22 stsp entry = first;
1498 e0d42f60 2018-07-22 stsp ncommits = 0;
1499 e0d42f60 2018-07-22 stsp while (entry) {
1500 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1501 e0d42f60 2018-07-22 stsp *selected = entry;
1504 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1505 e0d42f60 2018-07-22 stsp ncommits++;
1508 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1509 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1511 ecb28ae0 2018-07-16 stsp return err;
1512 8b473291 2019-02-21 stsp if (refs) {
1513 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1516 8b473291 2019-02-21 stsp goto done;
1520 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1521 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1523 8f4ed634 2020-03-26 stsp if (commits_needed > 0) {
1524 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1525 8f4ed634 2020-03-26 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1526 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1527 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1528 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1529 8f4ed634 2020-03-26 stsp goto done;
1532 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1534 f9686aa5 2020-03-27 stsp if (view->searching) {
1535 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1536 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1537 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1538 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1539 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1540 f9686aa5 2020-03-27 stsp search_str = "searching...";
1543 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1544 8f4ed634 2020-03-26 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1545 f9686aa5 2020-03-27 stsp search_str ? search_str :
1546 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1547 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1548 8f4ed634 2020-03-26 stsp goto done;
1552 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1553 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1554 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1555 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1556 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1557 1a76625f 2018-10-22 stsp header = NULL;
1558 1a76625f 2018-10-22 stsp goto done;
1560 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1561 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1562 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1563 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1564 1a76625f 2018-10-22 stsp header = NULL;
1565 1a76625f 2018-10-22 stsp goto done;
1567 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1569 1a76625f 2018-10-22 stsp goto done;
1571 2814baeb 2018-08-01 stsp werase(view->window);
1573 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1574 a3404814 2018-09-02 stsp wstandout(view->window);
1575 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1577 11b20872 2019-11-08 stsp wattr_on(view->window,
1578 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1579 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1581 11b20872 2019-11-08 stsp wattr_off(view->window,
1582 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1583 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1584 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1587 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1588 a3404814 2018-09-02 stsp wstandend(view->window);
1589 ecb28ae0 2018-07-16 stsp free(wline);
1590 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1591 1a76625f 2018-10-22 stsp goto done;
1593 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1594 899d86c2 2018-05-10 stsp entry = first;
1595 5813d178 2019-03-09 stsp ncommits = 0;
1596 5813d178 2019-03-09 stsp while (entry) {
1597 5813d178 2019-03-09 stsp char *author;
1598 5813d178 2019-03-09 stsp wchar_t *wauthor;
1599 5813d178 2019-03-09 stsp int width;
1600 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1602 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1603 5813d178 2019-03-09 stsp if (author == NULL) {
1604 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1605 5813d178 2019-03-09 stsp goto done;
1607 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1608 27a741e5 2019-09-11 stsp date_display_cols);
1609 5813d178 2019-03-09 stsp if (author_cols < width)
1610 5813d178 2019-03-09 stsp author_cols = width;
1611 5813d178 2019-03-09 stsp free(wauthor);
1612 5813d178 2019-03-09 stsp free(author);
1613 7ca04879 2019-10-19 stsp ncommits++;
1614 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1617 5813d178 2019-03-09 stsp entry = first;
1618 899d86c2 2018-05-10 stsp *last = first;
1619 867c6645 2018-07-10 stsp ncommits = 0;
1620 899d86c2 2018-05-10 stsp while (entry) {
1621 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1623 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1624 2814baeb 2018-08-01 stsp wstandout(view->window);
1625 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1626 11b20872 2019-11-08 stsp date_display_cols, author_cols, colors);
1627 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1628 2814baeb 2018-08-01 stsp wstandend(view->window);
1630 60493ae3 2019-06-20 stsp goto done;
1631 0553a4e3 2018-04-30 stsp ncommits++;
1632 899d86c2 2018-05-10 stsp *last = entry;
1633 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1636 1a57306a 2018-09-02 stsp view_vborder(view);
1638 1a76625f 2018-10-22 stsp free(id_str);
1639 8b473291 2019-02-21 stsp free(refs_str);
1640 1a76625f 2018-10-22 stsp free(ncommits_str);
1641 1a76625f 2018-10-22 stsp free(header);
1642 80ddbec8 2018-04-29 stsp return err;
1645 07b55e75 2018-05-10 stsp static void
1646 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1647 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1648 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1650 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1651 07b55e75 2018-05-10 stsp int nscrolled = 0;
1653 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1654 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1657 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1658 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1659 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1660 07b55e75 2018-05-10 stsp if (entry) {
1661 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1662 07b55e75 2018-05-10 stsp nscrolled++;
1667 aa075928 2018-05-10 stsp static const struct got_error *
1668 7c1452c1 2020-03-26 stsp trigger_log_thread(struct tog_view *log_view, int wait,
1669 7c1452c1 2020-03-26 stsp int *commits_needed, int *log_complete,
1670 7c1452c1 2020-03-26 stsp pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1672 5e224a3e 2019-02-22 stsp int errcode;
1674 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1676 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1677 5e224a3e 2019-02-22 stsp if (*log_complete)
1680 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1681 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1682 7aafa0d1 2019-02-22 stsp if (errcode)
1683 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1684 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1687 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1688 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1690 7c1452c1 2020-03-26 stsp if (!wait)
1693 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1694 7c1452c1 2020-03-26 stsp show_log_view(log_view);
1695 7c1452c1 2020-03-26 stsp update_panels();
1696 7c1452c1 2020-03-26 stsp doupdate();
1698 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1699 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1700 82954512 2020-02-03 stsp if (errcode)
1701 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1702 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1704 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1705 7c1452c1 2020-03-26 stsp show_log_view(log_view);
1706 7c1452c1 2020-03-26 stsp update_panels();
1707 7c1452c1 2020-03-26 stsp doupdate();
1710 5e224a3e 2019-02-22 stsp return NULL;
1713 5e224a3e 2019-02-22 stsp static const struct got_error *
1714 7c1452c1 2020-03-26 stsp scroll_down(struct tog_view *log_view,
1715 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1716 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1717 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1718 7c1452c1 2020-03-26 stsp pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1720 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1721 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1722 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1724 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1725 5e224a3e 2019-02-22 stsp return NULL;
1727 7c1452c1 2020-03-26 stsp ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1728 7c1452c1 2020-03-26 stsp if (commits->ncommits < ncommits_needed && !*log_complete) {
1730 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1732 7c1452c1 2020-03-26 stsp (*commits_needed) += maxscroll;
1733 7c1452c1 2020-03-26 stsp err = trigger_log_thread(log_view, 1, commits_needed,
1734 7c1452c1 2020-03-26 stsp log_complete, need_commits, commit_loaded);
1736 5e224a3e 2019-02-22 stsp return err;
1740 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1741 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1744 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1746 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1747 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1749 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1750 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1752 dd0a52c1 2018-05-20 stsp return err;
1755 cd0acaa7 2018-05-20 stsp static const struct got_error *
1756 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1757 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1758 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1759 8b473291 2019-02-21 stsp struct got_repository *repo)
1761 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1762 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1763 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1765 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1766 15a94983 2018-12-23 stsp if (diff_view == NULL)
1767 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1769 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1770 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1771 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1772 e5a0f69f 2018-08-18 stsp if (err == NULL)
1773 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1774 cd0acaa7 2018-05-20 stsp return err;
1777 80ddbec8 2018-04-29 stsp static const struct got_error *
1778 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1779 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1781 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1783 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1784 941e9f74 2019-05-21 stsp if (parent == NULL)
1785 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1787 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1788 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1789 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1790 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1791 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1792 941e9f74 2019-05-21 stsp s->tree = subtree;
1793 941e9f74 2019-05-21 stsp s->selected = 0;
1794 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1795 941e9f74 2019-05-21 stsp return NULL;
1798 941e9f74 2019-05-21 stsp static const struct got_error *
1799 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1800 55cccc34 2020-02-20 stsp struct got_object_id *commit_id,
1801 55cccc34 2020-02-20 stsp const char *path, struct got_repository *repo)
1803 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1804 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1805 941e9f74 2019-05-21 stsp const char *p;
1806 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1808 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1810 941e9f74 2019-05-21 stsp while (*p) {
1811 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1812 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1813 56e0773d 2019-11-28 stsp char *te_name;
1815 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1818 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1819 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1820 941e9f74 2019-05-21 stsp if (slash == NULL)
1821 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1823 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1824 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1825 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1828 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1829 56e0773d 2019-11-28 stsp if (te == NULL) {
1830 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1831 56e0773d 2019-11-28 stsp free(te_name);
1834 56e0773d 2019-11-28 stsp free(te_name);
1835 56e0773d 2019-11-28 stsp s->selected_entry = te;
1836 56e0773d 2019-11-28 stsp s->selected = got_tree_entry_get_index(te);
1837 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1838 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1840 56e0773d 2019-11-28 stsp if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1841 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1842 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1843 67409a31 2019-05-24 stsp s->selected = 0;
1847 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1848 941e9f74 2019-05-21 stsp if (slash)
1849 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1851 941e9f74 2019-05-21 stsp subpath = strdup(path);
1852 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1853 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1857 55cccc34 2020-02-20 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
1862 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1863 941e9f74 2019-05-21 stsp free(tree_id);
1867 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1868 941e9f74 2019-05-21 stsp if (err) {
1869 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1872 941e9f74 2019-05-21 stsp if (slash == NULL)
1874 941e9f74 2019-05-21 stsp free(subpath);
1875 941e9f74 2019-05-21 stsp subpath = NULL;
1876 941e9f74 2019-05-21 stsp p = slash;
1879 941e9f74 2019-05-21 stsp free(subpath);
1880 1a76625f 2018-10-22 stsp return err;
1883 61266923 2020-01-14 stsp static const struct got_error *
1884 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1885 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1886 55cccc34 2020-02-20 stsp struct got_reflist_head *refs, struct got_repository *repo)
1888 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1889 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1890 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1891 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1893 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1894 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1896 55cccc34 2020-02-20 stsp return err;
1898 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1899 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1900 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1902 55cccc34 2020-02-20 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1903 55cccc34 2020-02-20 stsp if (err) {
1904 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1905 55cccc34 2020-02-20 stsp return err;
1907 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1909 55cccc34 2020-02-20 stsp *new_view = tree_view;
1911 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1912 55cccc34 2020-02-20 stsp return NULL;
1914 55cccc34 2020-02-20 stsp return tree_view_walk_path(s, entry->id, path, repo);
1917 55cccc34 2020-02-20 stsp static const struct got_error *
1918 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1920 61266923 2020-01-14 stsp sigset_t sigset;
1921 61266923 2020-01-14 stsp int errcode;
1923 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1924 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1926 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1927 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1928 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1929 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1930 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1932 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1933 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1934 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1936 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1937 61266923 2020-01-14 stsp if (errcode)
1938 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1940 61266923 2020-01-14 stsp return NULL;
1943 1a76625f 2018-10-22 stsp static void *
1944 1a76625f 2018-10-22 stsp log_thread(void *arg)
1946 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1947 1a76625f 2018-10-22 stsp int errcode = 0;
1948 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1949 1a76625f 2018-10-22 stsp int done = 0;
1951 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1953 61266923 2020-01-14 stsp return (void *)err;
1955 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1956 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1957 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1958 13add988 2019-10-15 stsp a->regex);
1959 1a76625f 2018-10-22 stsp if (err) {
1960 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1961 1a76625f 2018-10-22 stsp return (void *)err;
1962 1a76625f 2018-10-22 stsp err = NULL;
1964 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1965 1a76625f 2018-10-22 stsp a->commits_needed--;
1967 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1968 3abe8080 2019-04-10 stsp if (errcode) {
1969 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1970 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1972 3abe8080 2019-04-10 stsp } else if (*a->quit)
1974 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1975 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1976 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1977 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1980 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
1981 7c1452c1 2020-03-26 stsp if (errcode) {
1982 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
1983 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
1984 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
1989 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1991 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
1992 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
1993 7c1452c1 2020-03-26 stsp &tog_mutex);
1994 7c1452c1 2020-03-26 stsp if (errcode)
1995 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
1996 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2000 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2001 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2002 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2003 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2005 3abe8080 2019-04-10 stsp a->log_complete = 1;
2006 1a76625f 2018-10-22 stsp return (void *)err;
2009 1a76625f 2018-10-22 stsp static const struct got_error *
2010 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2012 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2013 1a76625f 2018-10-22 stsp int errcode;
2015 1a76625f 2018-10-22 stsp if (s->thread) {
2016 1a76625f 2018-10-22 stsp s->quit = 1;
2017 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2018 1a76625f 2018-10-22 stsp if (errcode)
2019 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2020 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2021 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2022 1a76625f 2018-10-22 stsp if (errcode)
2023 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2024 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2025 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2026 1a76625f 2018-10-22 stsp if (errcode)
2027 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2028 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2029 1a76625f 2018-10-22 stsp if (errcode)
2030 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2031 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2032 1a76625f 2018-10-22 stsp s->thread = NULL;
2035 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2036 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2037 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
2039 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2040 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2041 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2044 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2045 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2046 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2049 9343a5fb 2018-06-23 stsp return err;
2052 9343a5fb 2018-06-23 stsp static const struct got_error *
2053 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2055 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2056 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2058 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2059 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2060 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2061 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2062 1a76625f 2018-10-22 stsp free(s->start_id);
2063 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2064 1a76625f 2018-10-22 stsp return err;
2067 1a76625f 2018-10-22 stsp static const struct got_error *
2068 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2070 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2072 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2073 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2074 60493ae3 2019-06-20 stsp return NULL;
2077 60493ae3 2019-06-20 stsp static const struct got_error *
2078 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2080 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2081 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2082 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2084 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2085 f9686aa5 2020-03-27 stsp show_log_view(view);
2086 f9686aa5 2020-03-27 stsp update_panels();
2087 f9686aa5 2020-03-27 stsp doupdate();
2089 96e2b566 2019-07-08 stsp if (s->search_entry) {
2090 13add988 2019-10-15 stsp int errcode, ch;
2091 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2092 13add988 2019-10-15 stsp if (errcode)
2093 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2094 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2095 13add988 2019-10-15 stsp ch = wgetch(view->window);
2096 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2097 13add988 2019-10-15 stsp if (errcode)
2098 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2099 13add988 2019-10-15 stsp "pthread_mutex_lock");
2100 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2101 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2102 678cbce5 2019-07-28 stsp return NULL;
2104 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2105 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2107 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2108 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2109 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2110 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2111 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2113 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2114 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2116 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2117 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2119 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2122 60493ae3 2019-06-20 stsp while (1) {
2123 13add988 2019-10-15 stsp int have_match = 0;
2125 60493ae3 2019-06-20 stsp if (entry == NULL) {
2126 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2127 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2128 f9967bca 2020-03-27 stsp view->search_next_done =
2129 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2130 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2131 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2132 f801134a 2019-06-25 stsp return NULL;
2135 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2136 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2137 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2139 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2140 7c1452c1 2020-03-26 stsp return trigger_log_thread(view, 0,
2141 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
2142 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
2143 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2144 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2147 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2148 13add988 2019-10-15 stsp &view->regex);
2151 13add988 2019-10-15 stsp if (have_match) {
2152 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2153 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2157 96e2b566 2019-07-08 stsp s->search_entry = entry;
2158 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2159 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2161 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2164 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2165 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2166 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2167 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2169 60493ae3 2019-06-20 stsp return err;
2172 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2173 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2175 60493ae3 2019-06-20 stsp return err;
2180 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2182 60493ae3 2019-06-20 stsp return NULL;
2185 60493ae3 2019-06-20 stsp static const struct got_error *
2186 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2187 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
2188 f135c941 2020-02-20 stsp const char *head_ref_name, const char *in_repo_path,
2189 b672a97a 2020-01-27 stsp int log_branches)
2191 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2192 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2193 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2194 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2195 1a76625f 2018-10-22 stsp int errcode;
2197 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2198 f135c941 2020-02-20 stsp free(s->in_repo_path);
2199 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2200 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2201 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2204 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2205 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2206 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2208 8b473291 2019-02-21 stsp s->refs = refs;
2209 fb2756b9 2018-08-04 stsp s->repo = repo;
2210 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2211 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2212 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2213 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2214 5036bf37 2018-09-24 stsp goto done;
2216 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2218 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2219 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2220 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2221 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2223 11b20872 2019-11-08 stsp goto done;
2224 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2225 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2226 11b20872 2019-11-08 stsp if (err) {
2227 11b20872 2019-11-08 stsp free_colors(&s->colors);
2228 11b20872 2019-11-08 stsp goto done;
2230 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2231 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2232 11b20872 2019-11-08 stsp if (err) {
2233 11b20872 2019-11-08 stsp free_colors(&s->colors);
2234 11b20872 2019-11-08 stsp goto done;
2238 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2239 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2240 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2241 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2242 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2244 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2246 1a76625f 2018-10-22 stsp goto done;
2247 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2248 b672a97a 2020-01-27 stsp !s->log_branches);
2250 1a76625f 2018-10-22 stsp goto done;
2251 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2252 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2254 c5b78334 2020-01-12 stsp goto done;
2256 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2257 1a76625f 2018-10-22 stsp if (errcode) {
2258 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2259 1a76625f 2018-10-22 stsp goto done;
2261 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2262 7c1452c1 2020-03-26 stsp if (errcode) {
2263 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2264 7c1452c1 2020-03-26 stsp goto done;
2267 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2268 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2269 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2270 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2271 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2272 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2273 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2274 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2275 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2276 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2277 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2278 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2279 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2282 1a76625f 2018-10-22 stsp close_log_view(view);
2283 ba4f502b 2018-08-04 stsp return err;
2286 e5a0f69f 2018-08-18 stsp static const struct got_error *
2287 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2289 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2291 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2292 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2293 2b380cc8 2018-10-24 stsp &s->thread_args);
2294 2b380cc8 2018-10-24 stsp if (errcode)
2295 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2298 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2299 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2300 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2301 11b20872 2019-11-08 stsp s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2304 e5a0f69f 2018-08-18 stsp static const struct got_error *
2305 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2306 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2308 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2309 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2310 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2311 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2312 669b5ffa 2018-10-07 stsp int begin_x = 0;
2313 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2315 e5a0f69f 2018-08-18 stsp switch (ch) {
2317 1e37a5c2 2019-05-12 jcs s->quit = 1;
2320 1e37a5c2 2019-05-12 jcs case KEY_UP:
2323 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2325 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2326 1e37a5c2 2019-05-12 jcs s->selected--;
2328 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2329 1144d21a 2019-06-21 stsp &s->commits);
2331 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2332 a4292ac5 2019-05-12 jcs case CTRL('b'):
2333 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2335 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2336 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2337 1e37a5c2 2019-05-12 jcs s->selected = 0;
2340 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2341 1831ac02 2020-03-23 naddy view->nlines - 1, &s->commits);
2344 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2347 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2349 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2350 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2351 1e37a5c2 2019-05-12 jcs s->selected++;
2354 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2355 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2356 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2357 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2358 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2359 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2361 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2362 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2363 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2364 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2365 1e37a5c2 2019-05-12 jcs if (first == NULL)
2367 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2368 1831ac02 2020-03-23 naddy view->nlines - 1, &s->last_displayed_entry,
2369 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2370 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2371 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2372 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2375 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2376 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2377 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2378 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2379 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2380 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2382 1e37a5c2 2019-05-12 jcs err = NULL;
2385 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2386 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2387 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2388 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2389 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2391 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2394 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2396 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2397 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2398 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2399 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2400 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2403 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2404 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2406 1e37a5c2 2019-05-12 jcs return err;
2407 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2409 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2412 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2413 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2415 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2418 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2420 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2421 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2422 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2423 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2426 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2427 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2429 1e37a5c2 2019-05-12 jcs return err;
2430 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2432 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2435 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2436 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2438 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2440 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2441 74cfe85e 2020-10-20 stsp if (got_path_cmp(s->in_repo_path, "/",
2442 74cfe85e 2020-10-20 stsp strlen(s->in_repo_path), 1) == 0)
2444 74cfe85e 2020-10-20 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2446 74cfe85e 2020-10-20 stsp return err;
2447 74cfe85e 2020-10-20 stsp err = stop_log_thread(s);
2448 74cfe85e 2020-10-20 stsp if (err) {
2449 74cfe85e 2020-10-20 stsp free(parent_path);
2450 74cfe85e 2020-10-20 stsp return err;
2452 74cfe85e 2020-10-20 stsp lv = view_open(view->nlines, view->ncols,
2453 74cfe85e 2020-10-20 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2454 74cfe85e 2020-10-20 stsp if (lv == NULL) {
2455 74cfe85e 2020-10-20 stsp free(parent_path);
2456 74cfe85e 2020-10-20 stsp return got_error_from_errno("view_open");
2458 74cfe85e 2020-10-20 stsp err = open_log_view(lv, s->start_id, s->refs,
2459 74cfe85e 2020-10-20 stsp s->repo, s->head_ref_name, parent_path,
2460 74cfe85e 2020-10-20 stsp s->log_branches);
2461 74cfe85e 2020-10-20 stsp free(parent_path);
2463 74cfe85e 2020-10-20 stsp return err;;
2464 74cfe85e 2020-10-20 stsp if (view_is_parent_view(view))
2465 74cfe85e 2020-10-20 stsp *new_view = lv;
2467 74cfe85e 2020-10-20 stsp view_set_child(view->parent, lv);
2468 74cfe85e 2020-10-20 stsp *focus_view = lv;
2471 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2472 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2474 d01904d4 2019-06-25 stsp return err;
2475 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2476 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2477 d01904d4 2019-06-25 stsp if (lv == NULL)
2478 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2479 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2480 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2481 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2482 ef129c5e 2019-08-03 stsp if (err) {
2483 ef129c5e 2019-08-03 stsp view_close(lv);
2484 d01904d4 2019-06-25 stsp return err;
2486 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2487 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2488 e6b23735 2019-10-04 stsp free(start_id);
2489 e6b23735 2019-10-04 stsp view_close(lv);
2490 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2492 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2493 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2494 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2495 e6b23735 2019-10-04 stsp if (err) {
2496 d01904d4 2019-06-25 stsp free(start_id);
2497 ef129c5e 2019-08-03 stsp view_close(lv);
2498 50284065 2019-10-04 stsp return err;
2500 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2501 f135c941 2020-02-20 stsp s->head_ref_name, in_repo_path, s->log_branches);
2502 ef129c5e 2019-08-03 stsp if (err) {
2503 ef129c5e 2019-08-03 stsp free(start_id);
2504 b672a97a 2020-01-27 stsp view_close(lv);
2505 b672a97a 2020-01-27 stsp return err;;
2507 b672a97a 2020-01-27 stsp *dead_view = view;
2508 b672a97a 2020-01-27 stsp *new_view = lv;
2511 b672a97a 2020-01-27 stsp s->log_branches = !s->log_branches;
2512 b672a97a 2020-01-27 stsp err = stop_log_thread(s);
2514 b672a97a 2020-01-27 stsp return err;
2515 b672a97a 2020-01-27 stsp lv = view_open(view->nlines, view->ncols,
2516 b672a97a 2020-01-27 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2517 b672a97a 2020-01-27 stsp if (lv == NULL)
2518 b672a97a 2020-01-27 stsp return got_error_from_errno("view_open");
2519 b672a97a 2020-01-27 stsp err = open_log_view(lv, s->start_id, s->refs, s->repo,
2520 f135c941 2020-02-20 stsp s->head_ref_name, s->in_repo_path, s->log_branches);
2521 b672a97a 2020-01-27 stsp if (err) {
2522 ef129c5e 2019-08-03 stsp view_close(lv);
2523 d01904d4 2019-06-25 stsp return err;;
2525 d01904d4 2019-06-25 stsp *dead_view = view;
2526 d01904d4 2019-06-25 stsp *new_view = lv;
2532 80ddbec8 2018-04-29 stsp return err;
2535 4ed7e80c 2018-05-20 stsp static const struct got_error *
2536 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2538 c2db6724 2019-01-04 stsp const struct got_error *error;
2540 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2541 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2542 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2544 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2545 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2547 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2548 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2550 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2551 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2553 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2554 c2db6724 2019-01-04 stsp if (error != NULL)
2555 c2db6724 2019-01-04 stsp return error;
2557 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2558 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2560 c2db6724 2019-01-04 stsp return NULL;
2563 a915003a 2019-02-05 stsp static void
2564 a915003a 2019-02-05 stsp init_curses(void)
2566 a915003a 2019-02-05 stsp initscr();
2568 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2571 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2572 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2573 a915003a 2019-02-05 stsp curs_set(0);
2574 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2575 6d17833f 2019-11-08 stsp start_color();
2576 6d17833f 2019-11-08 stsp use_default_colors();
2578 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2579 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2580 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2583 c2db6724 2019-01-04 stsp static const struct got_error *
2584 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2585 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2587 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2589 f135c941 2020-02-20 stsp if (argc == 0) {
2590 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2591 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2592 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2593 f135c941 2020-02-20 stsp return NULL;
2596 f135c941 2020-02-20 stsp if (worktree) {
2597 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2598 f135c941 2020-02-20 stsp char *wt_path, *p;
2600 f135c941 2020-02-20 stsp err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2602 f135c941 2020-02-20 stsp return err;
2604 f135c941 2020-02-20 stsp if (asprintf(&p, "%s%s%s", prefix,
2605 f135c941 2020-02-20 stsp (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2606 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2607 f135c941 2020-02-20 stsp free(wt_path);
2608 f135c941 2020-02-20 stsp return err;
2610 f135c941 2020-02-20 stsp err = got_repo_map_path(in_repo_path, repo, p, 0);
2612 f135c941 2020-02-20 stsp free(wt_path);
2614 f135c941 2020-02-20 stsp err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2616 f135c941 2020-02-20 stsp return err;
2619 f135c941 2020-02-20 stsp static const struct got_error *
2620 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2622 80ddbec8 2018-04-29 stsp const struct got_error *error;
2623 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2624 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2625 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2626 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2627 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2628 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2629 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2630 04cc582a 2018-08-01 stsp struct tog_view *view;
2632 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2634 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2635 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2636 c2db6724 2019-01-04 stsp NULL) == -1)
2637 80ddbec8 2018-04-29 stsp err(1, "pledge");
2640 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2641 80ddbec8 2018-04-29 stsp switch (ch) {
2643 b672a97a 2020-01-27 stsp log_branches = 1;
2646 80ddbec8 2018-04-29 stsp start_commit = optarg;
2649 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2650 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2651 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2655 17020d27 2019-03-07 stsp usage_log();
2656 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2660 80ddbec8 2018-04-29 stsp argc -= optind;
2661 80ddbec8 2018-04-29 stsp argv += optind;
2663 f135c941 2020-02-20 stsp if (argc > 1)
2664 f135c941 2020-02-20 stsp usage_log();
2666 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2667 6962eb72 2020-02-20 stsp if (cwd == NULL)
2668 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
2670 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2671 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2672 963f97a1 2019-03-18 stsp goto done;
2674 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2675 a1fbf39a 2019-08-11 stsp if (worktree)
2676 6962eb72 2020-02-20 stsp repo_path =
2677 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2679 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2681 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2682 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2683 963f97a1 2019-03-18 stsp goto done;
2686 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2687 80ddbec8 2018-04-29 stsp if (error != NULL)
2688 ecb28ae0 2018-07-16 stsp goto done;
2690 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2691 f135c941 2020-02-20 stsp repo, worktree);
2692 f135c941 2020-02-20 stsp if (error)
2693 f135c941 2020-02-20 stsp goto done;
2695 f135c941 2020-02-20 stsp init_curses();
2697 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2698 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2699 c02c541e 2019-03-29 stsp if (error)
2700 c02c541e 2019-03-29 stsp goto done;
2702 15a94983 2018-12-23 stsp if (start_commit == NULL)
2703 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2704 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2705 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2707 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2708 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2709 80ddbec8 2018-04-29 stsp if (error != NULL)
2710 8b473291 2019-02-21 stsp goto done;
2712 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2713 8b473291 2019-02-21 stsp if (error)
2714 ecb28ae0 2018-07-16 stsp goto done;
2716 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2717 04cc582a 2018-08-01 stsp if (view == NULL) {
2718 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2719 04cc582a 2018-08-01 stsp goto done;
2721 2fc00ff4 2019-08-31 stsp if (worktree) {
2722 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2723 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2724 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2725 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2726 2fc00ff4 2019-08-31 stsp goto done;
2729 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2730 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2731 ba4f502b 2018-08-04 stsp if (error)
2732 ba4f502b 2018-08-04 stsp goto done;
2733 2fc00ff4 2019-08-31 stsp if (worktree) {
2734 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2735 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2736 2fc00ff4 2019-08-31 stsp worktree = NULL;
2738 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2740 f135c941 2020-02-20 stsp free(in_repo_path);
2741 ecb28ae0 2018-07-16 stsp free(repo_path);
2742 ecb28ae0 2018-07-16 stsp free(cwd);
2743 899d86c2 2018-05-10 stsp free(start_id);
2744 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2746 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2747 ec142235 2019-03-07 stsp if (worktree)
2748 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2749 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2750 80ddbec8 2018-04-29 stsp return error;
2753 4ed7e80c 2018-05-20 stsp __dead static void
2754 9f7d7167 2018-04-29 stsp usage_diff(void)
2757 09b5bff8 2020-02-23 naddy fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2758 9f7d7167 2018-04-29 stsp getprogname());
2762 b304db33 2018-05-20 stsp static char *
2763 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2765 b304db33 2018-05-20 stsp char *line;
2766 b304db33 2018-05-20 stsp size_t linelen;
2767 b304db33 2018-05-20 stsp size_t lineno;
2768 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2770 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2772 b304db33 2018-05-20 stsp *len = linelen;
2773 b304db33 2018-05-20 stsp return line;
2776 6d17833f 2019-11-08 stsp static int
2777 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2778 41605754 2020-11-12 stsp regmatch_t *regmatch)
2780 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2783 f26dddb7 2019-11-08 stsp struct tog_color *
2784 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2786 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2788 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2789 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2790 6d17833f 2019-11-08 stsp return tc;
2793 6d17833f 2019-11-08 stsp return NULL;
2796 4ed7e80c 2018-05-20 stsp static const struct got_error *
2797 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2798 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2800 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2801 41605754 2020-11-12 stsp wchar_t *wline;
2802 41605754 2020-11-12 stsp int width;
2805 41605754 2020-11-12 stsp *wtotal = 0;
2807 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2808 41605754 2020-11-12 stsp if (s == NULL)
2809 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2811 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2812 41605754 2020-11-12 stsp if (err) {
2814 41605754 2020-11-12 stsp return err;
2816 41605754 2020-11-12 stsp waddwstr(window, wline);
2817 41605754 2020-11-12 stsp free(wline);
2819 41605754 2020-11-12 stsp wlimit -= width;
2820 41605754 2020-11-12 stsp *wtotal += width;
2822 41605754 2020-11-12 stsp if (wlimit > 0) {
2823 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2824 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2825 41605754 2020-11-12 stsp if (s == NULL) {
2826 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2828 41605754 2020-11-12 stsp return err;
2830 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2831 41605754 2020-11-12 stsp if (err) {
2833 41605754 2020-11-12 stsp return err;
2835 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2836 41605754 2020-11-12 stsp waddwstr(window, wline);
2837 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2838 41605754 2020-11-12 stsp free(wline);
2840 41605754 2020-11-12 stsp wlimit -= width;
2841 41605754 2020-11-12 stsp *wtotal += width;
2844 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2845 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2846 41605754 2020-11-12 stsp line + regmatch->rm_eo, wlimit, col_tab_align);
2848 41605754 2020-11-12 stsp return err;
2849 41605754 2020-11-12 stsp waddwstr(window, wline);
2850 41605754 2020-11-12 stsp free(wline);
2851 41605754 2020-11-12 stsp *wtotal += width;
2854 41605754 2020-11-12 stsp return NULL;
2857 41605754 2020-11-12 stsp static const struct got_error *
2858 fe621944 2020-11-10 stsp draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2859 fe621944 2020-11-10 stsp off_t *line_offsets, int selected_line, int max_lines,
2860 41605754 2020-11-12 stsp int *last_displayed_line, int *eof, char *header,
2861 41605754 2020-11-12 stsp struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2863 61e69b96 2018-05-20 stsp const struct got_error *err;
2864 fe621944 2020-11-10 stsp int nprinted = 0;
2865 b304db33 2018-05-20 stsp char *line;
2866 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2867 b304db33 2018-05-20 stsp size_t len;
2868 61e69b96 2018-05-20 stsp wchar_t *wline;
2869 e0b650dd 2018-05-20 stsp int width;
2870 fe621944 2020-11-10 stsp off_t line_offset;
2872 fe621944 2020-11-10 stsp line_offset = line_offsets[first_displayed_line - 1];
2873 fe621944 2020-11-10 stsp if (fseeko(f, line_offset, SEEK_SET) == -1)
2874 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2876 f7d12f7e 2018-08-01 stsp werase(view->window);
2878 a3404814 2018-09-02 stsp if (header) {
2879 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2880 135a2da0 2020-11-11 stsp first_displayed_line - 1 + selected_line, nlines,
2881 135a2da0 2020-11-11 stsp header) == -1)
2882 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2883 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2884 135a2da0 2020-11-11 stsp free(line);
2886 a3404814 2018-09-02 stsp return err;
2888 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2889 a3404814 2018-09-02 stsp wstandout(view->window);
2890 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2891 e54cc94a 2020-11-11 stsp free(wline);
2892 e54cc94a 2020-11-11 stsp wline = NULL;
2893 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2894 a3404814 2018-09-02 stsp wstandend(view->window);
2895 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2896 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2898 a3404814 2018-09-02 stsp if (max_lines <= 1)
2899 a3404814 2018-09-02 stsp return NULL;
2900 a3404814 2018-09-02 stsp max_lines--;
2904 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2905 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2906 26ed57b2 2018-05-19 stsp if (line == NULL) {
2911 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2913 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2914 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2915 41605754 2020-11-12 stsp if (first_displayed_line + nprinted == matched_line &&
2916 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2917 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
2918 41605754 2020-11-12 stsp view->window, regmatch);
2919 41605754 2020-11-12 stsp if (err) {
2920 41605754 2020-11-12 stsp free(line);
2921 41605754 2020-11-12 stsp return err;
2924 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2925 41605754 2020-11-12 stsp if (err) {
2926 41605754 2020-11-12 stsp free(line);
2927 41605754 2020-11-12 stsp return err;
2929 41605754 2020-11-12 stsp waddwstr(view->window, wline);
2930 41605754 2020-11-12 stsp free(wline);
2931 41605754 2020-11-12 stsp wline = NULL;
2934 6d17833f 2019-11-08 stsp wattr_off(view->window,
2935 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2936 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2937 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2938 fe621944 2020-11-10 stsp nprinted++;
2939 26ed57b2 2018-05-19 stsp free(line);
2941 fe621944 2020-11-10 stsp if (nprinted >= 1)
2942 fe621944 2020-11-10 stsp *last_displayed_line = first_displayed_line + (nprinted - 1);
2944 fe621944 2020-11-10 stsp *last_displayed_line = first_displayed_line;
2946 1a57306a 2018-09-02 stsp view_vborder(view);
2948 c3e9aa98 2019-05-13 jcs if (*eof) {
2949 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2950 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2951 c3e9aa98 2019-05-13 jcs nprinted++;
2954 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2956 c3e9aa98 2019-05-13 jcs return err;
2959 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2960 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2961 e54cc94a 2020-11-11 stsp free(wline);
2962 e54cc94a 2020-11-11 stsp wline = NULL;
2963 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2966 26ed57b2 2018-05-19 stsp return NULL;
2969 abd2672a 2018-12-23 stsp static char *
2970 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2972 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2973 09867e48 2019-08-13 stsp char *p, *s;
2975 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2976 09867e48 2019-08-13 stsp if (tm == NULL)
2977 09867e48 2019-08-13 stsp return NULL;
2978 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2979 09867e48 2019-08-13 stsp if (s == NULL)
2980 09867e48 2019-08-13 stsp return NULL;
2981 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2983 abd2672a 2018-12-23 stsp *p = '\0';
2987 4ed7e80c 2018-05-20 stsp static const struct got_error *
2988 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
2989 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
2991 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
2992 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2993 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2994 0208f208 2020-05-05 stsp struct got_object_qid *qid;
2996 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2997 0208f208 2020-05-05 stsp if (qid != NULL) {
2998 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
2999 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3002 0208f208 2020-05-05 stsp return err;
3004 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3005 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3009 0208f208 2020-05-05 stsp if (tree_id1) {
3010 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3012 0208f208 2020-05-05 stsp goto done;
3015 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3016 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3018 0208f208 2020-05-05 stsp goto done;
3020 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3021 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3023 0208f208 2020-05-05 stsp if (tree1)
3024 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3025 0208f208 2020-05-05 stsp if (tree2)
3026 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3027 0208f208 2020-05-05 stsp return err;
3030 0208f208 2020-05-05 stsp static const struct got_error *
3031 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3035 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3036 fe621944 2020-11-10 stsp if (p == NULL)
3037 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3038 fe621944 2020-11-10 stsp *line_offsets = p;
3039 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3040 fe621944 2020-11-10 stsp (*nlines)++;
3041 fe621944 2020-11-10 stsp return NULL;
3044 fe621944 2020-11-10 stsp static const struct got_error *
3045 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3046 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3047 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3049 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3050 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3051 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3052 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3053 45d799e2 2018-12-23 stsp time_t committer_time;
3054 45d799e2 2018-12-23 stsp const char *author, *committer;
3055 8b473291 2019-02-21 stsp char *refs_str = NULL;
3056 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3057 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3058 fe621944 2020-11-10 stsp off_t outoff = 0;
3061 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3063 8b473291 2019-02-21 stsp if (refs) {
3064 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3066 8b473291 2019-02-21 stsp return err;
3069 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3071 abd2672a 2018-12-23 stsp return err;
3073 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3074 15a94983 2018-12-23 stsp if (err) {
3075 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3076 15a94983 2018-12-23 stsp goto done;
3079 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3081 fe621944 2020-11-10 stsp goto done;
3083 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3084 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3085 fe621944 2020-11-10 stsp if (n < 0) {
3086 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3087 abd2672a 2018-12-23 stsp goto done;
3089 fe621944 2020-11-10 stsp outoff += n;
3090 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3092 fe621944 2020-11-10 stsp goto done;
3094 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3095 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3096 fe621944 2020-11-10 stsp if (n < 0) {
3097 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3098 abd2672a 2018-12-23 stsp goto done;
3100 fe621944 2020-11-10 stsp outoff += n;
3101 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3103 fe621944 2020-11-10 stsp goto done;
3105 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3106 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3107 fe621944 2020-11-10 stsp if (datestr) {
3108 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3109 fe621944 2020-11-10 stsp if (n < 0) {
3110 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3111 fe621944 2020-11-10 stsp goto done;
3113 fe621944 2020-11-10 stsp outoff += n;
3114 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3116 fe621944 2020-11-10 stsp goto done;
3118 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3119 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3120 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3121 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3122 fe621944 2020-11-10 stsp if (n < 0) {
3123 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3124 fe621944 2020-11-10 stsp goto done;
3126 fe621944 2020-11-10 stsp outoff += n;
3127 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3129 fe621944 2020-11-10 stsp goto done;
3131 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3133 5943eee2 2019-08-13 stsp goto done;
3134 fe621944 2020-11-10 stsp s = logmsg;
3135 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3136 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3137 fe621944 2020-11-10 stsp if (n < 0) {
3138 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3139 fe621944 2020-11-10 stsp goto done;
3141 fe621944 2020-11-10 stsp outoff += n;
3142 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3144 fe621944 2020-11-10 stsp goto done;
3147 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3149 0208f208 2020-05-05 stsp goto done;
3150 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3151 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3152 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3153 fe621944 2020-11-10 stsp if (n < 0) {
3154 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3155 fe621944 2020-11-10 stsp goto done;
3157 fe621944 2020-11-10 stsp outoff += n;
3158 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3160 fe621944 2020-11-10 stsp goto done;
3161 0208f208 2020-05-05 stsp free((char *)pe->path);
3162 0208f208 2020-05-05 stsp free(pe->data);
3165 0208f208 2020-05-05 stsp fputc('\n', outfile);
3167 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3169 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3170 abd2672a 2018-12-23 stsp free(id_str);
3171 5943eee2 2019-08-13 stsp free(logmsg);
3172 8b473291 2019-02-21 stsp free(refs_str);
3173 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3174 7510f233 2020-08-09 stsp if (err) {
3175 ae6a6978 2020-08-09 stsp free(*line_offsets);
3176 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3177 ae6a6978 2020-08-09 stsp *nlines = 0;
3179 fe621944 2020-11-10 stsp return err;
3182 abd2672a 2018-12-23 stsp static const struct got_error *
3183 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3185 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3186 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3187 15a94983 2018-12-23 stsp int obj_type;
3189 fe621944 2020-11-10 stsp free(s->line_offsets);
3190 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3191 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3192 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3193 fe621944 2020-11-10 stsp s->nlines = 0;
3195 511a516b 2018-05-19 stsp f = got_opentemp();
3196 48ae06ee 2018-10-18 stsp if (f == NULL) {
3197 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3198 48ae06ee 2018-10-18 stsp goto done;
3200 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
3201 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3202 fb43ecf1 2019-02-11 stsp goto done;
3206 15a94983 2018-12-23 stsp if (s->id1)
3207 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3209 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3211 15a94983 2018-12-23 stsp goto done;
3213 15a94983 2018-12-23 stsp switch (obj_type) {
3214 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3215 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3216 fe621944 2020-11-10 stsp s->id1, s->id2, NULL, NULL, s->diff_context, 0,
3217 fe621944 2020-11-10 stsp s->repo, s->f);
3219 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3220 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3221 fe621944 2020-11-10 stsp s->id1, s->id2, "", "", s->diff_context, 0, s->repo, s->f);
3223 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3224 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3225 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3226 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3228 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3230 3ffacbe1 2020-02-02 tracey goto done;
3231 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3232 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3233 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3234 fe621944 2020-11-10 stsp s->id2, s->refs, s->repo, s->f);
3235 f44b1f58 2020-02-02 tracey if (err)
3236 f44b1f58 2020-02-02 tracey goto done;
3237 f44b1f58 2020-02-02 tracey } else {
3238 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3239 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3240 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3241 fe621944 2020-11-10 stsp err = write_commit_info(
3242 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3243 fe621944 2020-11-10 stsp s->id2, s->refs, s->repo, s->f);
3244 f44b1f58 2020-02-02 tracey if (err)
3245 f44b1f58 2020-02-02 tracey goto done;
3250 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3252 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3253 fe621944 2020-11-10 stsp s->id1, s->id2, s->diff_context, 0, s->repo, s->f);
3257 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3260 f44b1f58 2020-02-02 tracey if (err)
3261 f44b1f58 2020-02-02 tracey goto done;
3263 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3264 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3265 48ae06ee 2018-10-18 stsp return err;
3268 f5215bb9 2019-02-22 stsp static void
3269 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3271 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3272 f5215bb9 2019-02-22 stsp update_panels();
3273 f5215bb9 2019-02-22 stsp doupdate();
3276 f44b1f58 2020-02-02 tracey static const struct got_error *
3277 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3279 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3281 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3282 f44b1f58 2020-02-02 tracey return NULL;
3285 f44b1f58 2020-02-02 tracey static const struct got_error *
3286 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3288 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3289 f44b1f58 2020-02-02 tracey int lineno;
3291 f44b1f58 2020-02-02 tracey if (!view->searching) {
3292 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3293 f44b1f58 2020-02-02 tracey return NULL;
3296 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3297 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3298 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3300 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3301 f44b1f58 2020-02-02 tracey } else {
3302 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3303 f44b1f58 2020-02-02 tracey lineno = 1;
3305 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3308 f44b1f58 2020-02-02 tracey while (1) {
3309 f44b1f58 2020-02-02 tracey char *line = NULL;
3310 f44b1f58 2020-02-02 tracey off_t offset;
3311 f44b1f58 2020-02-02 tracey size_t len;
3313 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3314 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3315 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3316 f44b1f58 2020-02-02 tracey free(line);
3320 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3321 f44b1f58 2020-02-02 tracey lineno = 1;
3323 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3326 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3327 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3328 f44b1f58 2020-02-02 tracey free(line);
3329 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3331 f44b1f58 2020-02-02 tracey free(line);
3332 f44b1f58 2020-02-02 tracey line = parse_next_line(s->f, &len);
3333 41605754 2020-11-12 stsp if (line &&
3334 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3335 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3336 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3337 f44b1f58 2020-02-02 tracey free(line);
3340 f44b1f58 2020-02-02 tracey free(line);
3341 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3342 f44b1f58 2020-02-02 tracey lineno++;
3344 f44b1f58 2020-02-02 tracey lineno--;
3347 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3348 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3349 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3352 f44b1f58 2020-02-02 tracey return NULL;
3355 48ae06ee 2018-10-18 stsp static const struct got_error *
3356 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3357 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
3358 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
3360 48ae06ee 2018-10-18 stsp const struct got_error *err;
3361 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3363 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3364 15a94983 2018-12-23 stsp int type1, type2;
3365 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3367 15a94983 2018-12-23 stsp return err;
3368 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3370 15a94983 2018-12-23 stsp return err;
3372 15a94983 2018-12-23 stsp if (type1 != type2)
3373 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3375 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3376 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3377 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3378 f44b1f58 2020-02-02 tracey s->repo = repo;
3379 f44b1f58 2020-02-02 tracey s->refs = refs;
3380 f44b1f58 2020-02-02 tracey s->id1 = id1;
3381 f44b1f58 2020-02-02 tracey s->id2 = id2;
3383 15a94983 2018-12-23 stsp if (id1) {
3384 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3385 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3386 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3388 5465d566 2020-02-01 tracey s->id1 = NULL;
3390 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3391 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3392 5465d566 2020-02-01 tracey free(s->id1);
3393 5465d566 2020-02-01 tracey s->id1 = NULL;
3394 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3396 5465d566 2020-02-01 tracey s->f = NULL;
3397 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3398 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3399 5465d566 2020-02-01 tracey s->diff_context = 3;
3400 5465d566 2020-02-01 tracey s->log_view = log_view;
3401 5465d566 2020-02-01 tracey s->repo = repo;
3402 5465d566 2020-02-01 tracey s->refs = refs;
3404 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3405 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3406 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3407 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3408 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3410 6d17833f 2019-11-08 stsp return err;
3411 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3412 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3413 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3414 6d17833f 2019-11-08 stsp if (err) {
3415 5465d566 2020-02-01 tracey free_colors(&s->colors);
3416 6d17833f 2019-11-08 stsp return err;
3418 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3419 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3420 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3421 6d17833f 2019-11-08 stsp if (err) {
3422 5465d566 2020-02-01 tracey free_colors(&s->colors);
3423 6d17833f 2019-11-08 stsp return err;
3426 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3427 528c17dd 2020-07-31 stsp "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3428 0208f208 2020-05-05 stsp TOG_COLOR_DIFF_META,
3429 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3430 6d17833f 2019-11-08 stsp if (err) {
3431 5465d566 2020-02-01 tracey free_colors(&s->colors);
3432 6d17833f 2019-11-08 stsp return err;
3435 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3436 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3437 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3438 11b20872 2019-11-08 stsp if (err) {
3439 5465d566 2020-02-01 tracey free_colors(&s->colors);
3440 11b20872 2019-11-08 stsp return err;
3443 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3444 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3445 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3446 11b20872 2019-11-08 stsp if (err) {
3447 5465d566 2020-02-01 tracey free_colors(&s->colors);
3448 11b20872 2019-11-08 stsp return err;
3452 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3453 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3454 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3456 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3457 fe621944 2020-11-10 stsp s->nlines = 0;
3458 5465d566 2020-02-01 tracey err = create_diff(s);
3459 48ae06ee 2018-10-18 stsp if (err) {
3460 5465d566 2020-02-01 tracey free(s->id1);
3461 5465d566 2020-02-01 tracey s->id1 = NULL;
3462 5465d566 2020-02-01 tracey free(s->id2);
3463 5465d566 2020-02-01 tracey s->id2 = NULL;
3464 48ae06ee 2018-10-18 stsp return err;
3467 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3468 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3469 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3470 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3471 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3473 5dc9f4bc 2018-08-04 stsp return NULL;
3476 e5a0f69f 2018-08-18 stsp static const struct got_error *
3477 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3479 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3480 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3482 5465d566 2020-02-01 tracey free(s->id1);
3483 5465d566 2020-02-01 tracey s->id1 = NULL;
3484 5465d566 2020-02-01 tracey free(s->id2);
3485 5465d566 2020-02-01 tracey s->id2 = NULL;
3486 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3487 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3488 5465d566 2020-02-01 tracey free_colors(&s->colors);
3489 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3490 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3491 fe621944 2020-11-10 stsp s->nlines = 0;
3492 e5a0f69f 2018-08-18 stsp return err;
3495 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3496 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3498 a3404814 2018-09-02 stsp const struct got_error *err;
3499 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3500 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3502 a3404814 2018-09-02 stsp if (s->id1) {
3503 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3505 a3404814 2018-09-02 stsp return err;
3507 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3509 a3404814 2018-09-02 stsp return err;
3511 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
3512 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3513 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3514 a3404814 2018-09-02 stsp free(id_str1);
3515 a3404814 2018-09-02 stsp free(id_str2);
3516 a3404814 2018-09-02 stsp return err;
3518 a3404814 2018-09-02 stsp free(id_str1);
3519 a3404814 2018-09-02 stsp free(id_str2);
3521 fe621944 2020-11-10 stsp return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3522 fe621944 2020-11-10 stsp s->line_offsets, s->selected_line, view->nlines,
3523 41605754 2020-11-12 stsp &s->last_displayed_line, &s->eof, header, &s->colors,
3524 41605754 2020-11-12 stsp s->matched_line, &view->regmatch);
3527 15a087fe 2019-02-21 stsp static const struct got_error *
3528 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3529 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3531 d7a04538 2019-02-21 stsp const struct got_error *err;
3532 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3533 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3534 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3536 15a087fe 2019-02-21 stsp free(s->id2);
3537 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3538 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3539 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3541 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3543 d7a04538 2019-02-21 stsp return err;
3544 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3545 15a087fe 2019-02-21 stsp free(s->id1);
3546 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3547 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3548 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3549 15a087fe 2019-02-21 stsp return NULL;
3552 0cf4efb1 2018-09-29 stsp static const struct got_error *
3553 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3554 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3556 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3557 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3558 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3559 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3562 e5a0f69f 2018-08-18 stsp switch (ch) {
3564 1e37a5c2 2019-05-12 jcs case KEY_UP:
3565 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3566 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3568 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3569 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3570 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3573 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3574 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3575 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3578 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3579 1e37a5c2 2019-05-12 jcs if (!s->eof)
3580 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3582 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3583 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3585 00ba99a7 2019-05-12 jcs if (s->eof)
3588 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3589 1e37a5c2 2019-05-12 jcs char *line;
3590 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3591 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3592 1e37a5c2 2019-05-12 jcs if (line == NULL)
3597 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3598 1e37a5c2 2019-05-12 jcs s->diff_context--;
3599 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3600 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3604 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3605 1e37a5c2 2019-05-12 jcs s->diff_context++;
3606 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3607 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3612 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3614 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3615 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3616 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3617 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3620 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3625 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3629 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3630 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3632 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3633 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3637 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3639 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3641 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3642 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3643 7c1452c1 2020-03-26 stsp err = trigger_log_thread(s->log_view, 1,
3644 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
3645 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
3646 7c1452c1 2020-03-26 stsp &ls->thread_args.need_commits,
3647 7c1452c1 2020-03-26 stsp &ls->thread_args.commit_loaded);
3651 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3656 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3657 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3660 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3664 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3665 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3667 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3668 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3674 bcbd79e2 2018-08-19 stsp return err;
3677 4ed7e80c 2018-05-20 stsp static const struct got_error *
3678 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3680 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3681 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3682 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3683 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3684 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3685 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3686 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3688 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3690 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3692 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3693 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3694 eb6600df 2019-01-04 stsp NULL) == -1)
3695 26ed57b2 2018-05-19 stsp err(1, "pledge");
3698 09b5bff8 2020-02-23 naddy while ((ch = getopt(argc, argv, "r:")) != -1) {
3699 26ed57b2 2018-05-19 stsp switch (ch) {
3700 09b5bff8 2020-02-23 naddy case 'r':
3701 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3702 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3703 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3707 17020d27 2019-03-07 stsp usage_diff();
3708 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3712 26ed57b2 2018-05-19 stsp argc -= optind;
3713 26ed57b2 2018-05-19 stsp argv += optind;
3715 26ed57b2 2018-05-19 stsp if (argc == 0) {
3716 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3717 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3718 15a94983 2018-12-23 stsp id_str1 = argv[0];
3719 15a94983 2018-12-23 stsp id_str2 = argv[1];
3721 26ed57b2 2018-05-19 stsp usage_diff();
3723 a273ac94 2020-02-23 naddy cwd = getcwd(NULL, 0);
3724 a273ac94 2020-02-23 naddy if (cwd == NULL)
3725 a273ac94 2020-02-23 naddy return got_error_from_errno("getcwd");
3727 a273ac94 2020-02-23 naddy error = got_worktree_open(&worktree, cwd);
3728 a273ac94 2020-02-23 naddy if (error && error->code != GOT_ERR_NOT_WORKTREE)
3729 a273ac94 2020-02-23 naddy goto done;
3731 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3732 a273ac94 2020-02-23 naddy if (worktree)
3733 a273ac94 2020-02-23 naddy repo_path =
3734 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3736 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3738 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3739 a273ac94 2020-02-23 naddy error = got_error_from_errno("strdup");
3740 a273ac94 2020-02-23 naddy goto done;
3743 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3744 eb6600df 2019-01-04 stsp if (error)
3745 eb6600df 2019-01-04 stsp goto done;
3747 a273ac94 2020-02-23 naddy init_curses();
3749 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3750 26ed57b2 2018-05-19 stsp if (error)
3751 26ed57b2 2018-05-19 stsp goto done;
3753 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
3754 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3755 26ed57b2 2018-05-19 stsp if (error)
3756 26ed57b2 2018-05-19 stsp goto done;
3758 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
3759 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3760 26ed57b2 2018-05-19 stsp if (error)
3761 26ed57b2 2018-05-19 stsp goto done;
3763 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3764 8b473291 2019-02-21 stsp if (error)
3765 8b473291 2019-02-21 stsp goto done;
3767 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3768 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3769 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3770 ea5e7bb5 2018-08-01 stsp goto done;
3772 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3773 5dc9f4bc 2018-08-04 stsp if (error)
3774 5dc9f4bc 2018-08-04 stsp goto done;
3775 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3777 c02c541e 2019-03-29 stsp free(repo_path);
3778 a273ac94 2020-02-23 naddy free(cwd);
3780 921be706 2019-06-28 stsp got_repo_close(repo);
3781 a273ac94 2020-02-23 naddy if (worktree)
3782 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3783 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3784 26ed57b2 2018-05-19 stsp return error;
3787 4ed7e80c 2018-05-20 stsp __dead static void
3788 9f7d7167 2018-04-29 stsp usage_blame(void)
3791 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3792 9f7d7167 2018-04-29 stsp getprogname());
3796 84451b3e 2018-07-10 stsp struct tog_blame_line {
3797 84451b3e 2018-07-10 stsp int annotated;
3798 84451b3e 2018-07-10 stsp struct got_object_id *id;
3801 4ed7e80c 2018-05-20 stsp static const struct got_error *
3802 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3803 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3804 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3805 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3806 41605754 2020-11-12 stsp struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3808 84451b3e 2018-07-10 stsp const struct got_error *err;
3809 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3810 84451b3e 2018-07-10 stsp char *line;
3811 84451b3e 2018-07-10 stsp size_t len;
3812 84451b3e 2018-07-10 stsp wchar_t *wline;
3813 27a741e5 2019-09-11 stsp int width;
3814 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3815 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3816 ab089a2a 2018-07-12 stsp char *id_str;
3817 11b20872 2019-11-08 stsp struct tog_color *tc;
3819 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3821 ab089a2a 2018-07-12 stsp return err;
3823 84451b3e 2018-07-10 stsp rewind(f);
3824 f7d12f7e 2018-08-01 stsp werase(view->window);
3826 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3827 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3828 ab089a2a 2018-07-12 stsp free(id_str);
3829 ab089a2a 2018-07-12 stsp return err;
3832 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3833 ab089a2a 2018-07-12 stsp free(line);
3834 2550e4c3 2018-07-13 stsp line = NULL;
3836 1cae65b4 2019-09-22 stsp return err;
3837 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3838 a3404814 2018-09-02 stsp wstandout(view->window);
3839 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3841 11b20872 2019-11-08 stsp wattr_on(view->window,
3842 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3843 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3845 11b20872 2019-11-08 stsp wattr_off(view->window,
3846 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3847 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3848 a3404814 2018-09-02 stsp wstandend(view->window);
3849 2550e4c3 2018-07-13 stsp free(wline);
3850 2550e4c3 2018-07-13 stsp wline = NULL;
3851 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3852 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3854 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3855 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3856 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3857 ab089a2a 2018-07-12 stsp free(id_str);
3858 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3860 ab089a2a 2018-07-12 stsp free(id_str);
3861 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3862 3f60a8ef 2018-07-10 stsp free(line);
3863 2550e4c3 2018-07-13 stsp line = NULL;
3865 3f60a8ef 2018-07-10 stsp return err;
3866 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3867 2550e4c3 2018-07-13 stsp free(wline);
3868 2550e4c3 2018-07-13 stsp wline = NULL;
3869 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3870 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3873 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3874 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3875 84451b3e 2018-07-10 stsp if (line == NULL) {
3879 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3880 84451b3e 2018-07-10 stsp free(line);
3884 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3885 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3887 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3888 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3889 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3890 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3891 27a741e5 2019-09-11 stsp !(view->focussed &&
3892 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3893 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3894 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3895 8d0fe45a 2019-08-12 stsp char *id_str;
3896 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3897 8d0fe45a 2019-08-12 stsp if (err) {
3898 8d0fe45a 2019-08-12 stsp free(line);
3899 8d0fe45a 2019-08-12 stsp return err;
3901 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3903 11b20872 2019-11-08 stsp wattr_on(view->window,
3904 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3905 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3907 11b20872 2019-11-08 stsp wattr_off(view->window,
3908 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3909 8d0fe45a 2019-08-12 stsp free(id_str);
3910 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3912 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3913 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3916 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3917 ee41ec32 2018-07-10 stsp prev_id = NULL;
3920 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3921 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3922 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3924 41605754 2020-11-12 stsp if (view->ncols <= 9) {
3925 41605754 2020-11-12 stsp width = 9;
3926 41605754 2020-11-12 stsp wline = wcsdup(L"");
3927 41605754 2020-11-12 stsp if (wline == NULL) {
3928 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
3929 41605754 2020-11-12 stsp free(line);
3930 41605754 2020-11-12 stsp return err;
3932 41605754 2020-11-12 stsp } else if (*first_displayed_line + nprinted == matched_line &&
3933 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3934 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
3935 41605754 2020-11-12 stsp view->window, regmatch);
3936 41605754 2020-11-12 stsp if (err) {
3937 41605754 2020-11-12 stsp free(line);
3938 41605754 2020-11-12 stsp return err;
3940 41605754 2020-11-12 stsp width += 9;
3942 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
3943 41605754 2020-11-12 stsp view->ncols - 9, 9);
3944 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3945 41605754 2020-11-12 stsp free(wline);
3946 41605754 2020-11-12 stsp wline = NULL;
3947 41605754 2020-11-12 stsp width += 9;
3950 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3951 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3952 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3953 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3954 84451b3e 2018-07-10 stsp free(line);
3956 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3958 1a57306a 2018-09-02 stsp view_vborder(view);
3960 84451b3e 2018-07-10 stsp return NULL;
3963 84451b3e 2018-07-10 stsp static const struct got_error *
3964 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3966 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3967 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3968 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3969 1a76625f 2018-10-22 stsp int errcode;
3971 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3972 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3973 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3975 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3976 1a76625f 2018-10-22 stsp if (errcode)
3977 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3979 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3980 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3981 d68a0a7d 2018-07-10 stsp goto done;
3984 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3985 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3987 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3988 d68a0a7d 2018-07-10 stsp if (line->annotated)
3989 d68a0a7d 2018-07-10 stsp goto done;
3991 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3992 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3993 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3994 84451b3e 2018-07-10 stsp goto done;
3996 84451b3e 2018-07-10 stsp line->annotated = 1;
3998 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3999 1a76625f 2018-10-22 stsp if (errcode)
4000 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4001 84451b3e 2018-07-10 stsp return err;
4004 84451b3e 2018-07-10 stsp static void *
4005 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4007 18430de3 2018-07-10 stsp const struct got_error *err;
4008 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4009 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4010 1a76625f 2018-10-22 stsp int errcode;
4012 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4014 61266923 2020-01-14 stsp return (void *)err;
4016 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4017 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4018 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4019 fc06ba56 2019-08-22 stsp err = NULL;
4021 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4022 1a76625f 2018-10-22 stsp if (errcode)
4023 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4024 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4026 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
4027 c9beca56 2018-07-22 stsp ta->repo = NULL;
4028 c9beca56 2018-07-22 stsp *ta->complete = 1;
4030 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4031 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4032 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4034 18430de3 2018-07-10 stsp return (void *)err;
4037 245d91c1 2018-07-12 stsp static struct got_object_id *
4038 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4039 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4041 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4043 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4044 8d0fe45a 2019-08-12 stsp return NULL;
4046 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4047 245d91c1 2018-07-12 stsp if (!line->annotated)
4048 245d91c1 2018-07-12 stsp return NULL;
4050 245d91c1 2018-07-12 stsp return line->id;
4053 b880a918 2018-07-10 stsp static const struct got_error *
4054 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4056 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4059 245d91c1 2018-07-12 stsp if (blame->thread) {
4060 1a76625f 2018-10-22 stsp int errcode;
4061 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4062 1a76625f 2018-10-22 stsp if (errcode)
4063 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4064 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4065 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4066 1a76625f 2018-10-22 stsp if (errcode)
4067 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4068 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4069 1a76625f 2018-10-22 stsp if (errcode)
4070 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4071 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4072 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4073 245d91c1 2018-07-12 stsp err = NULL;
4074 245d91c1 2018-07-12 stsp blame->thread = NULL;
4076 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4077 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
4078 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4080 245d91c1 2018-07-12 stsp if (blame->f) {
4081 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
4082 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4083 245d91c1 2018-07-12 stsp blame->f = NULL;
4085 57670559 2018-12-23 stsp if (blame->lines) {
4086 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4087 57670559 2018-12-23 stsp free(blame->lines[i].id);
4088 57670559 2018-12-23 stsp free(blame->lines);
4089 57670559 2018-12-23 stsp blame->lines = NULL;
4091 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4092 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4094 245d91c1 2018-07-12 stsp return err;
4097 245d91c1 2018-07-12 stsp static const struct got_error *
4098 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4100 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4101 fc06ba56 2019-08-22 stsp int *done = arg;
4102 fc06ba56 2019-08-22 stsp int errcode;
4104 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4105 fc06ba56 2019-08-22 stsp if (errcode)
4106 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4107 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4109 fc06ba56 2019-08-22 stsp if (*done)
4110 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4112 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4113 fc06ba56 2019-08-22 stsp if (errcode)
4114 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4115 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4117 fc06ba56 2019-08-22 stsp return err;
4120 fc06ba56 2019-08-22 stsp static const struct got_error *
4121 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4122 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
4123 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
4124 245d91c1 2018-07-12 stsp struct got_repository *repo)
4126 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4127 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4128 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4129 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4130 15a94983 2018-12-23 stsp int obj_type;
4132 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4134 15a94983 2018-12-23 stsp return err;
4136 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
4138 84451b3e 2018-07-10 stsp goto done;
4140 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4141 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4142 84451b3e 2018-07-10 stsp goto done;
4145 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4147 a70480e0 2018-06-23 stsp goto done;
4148 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4149 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4150 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4151 84451b3e 2018-07-10 stsp goto done;
4153 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4154 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4155 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
4156 84451b3e 2018-07-10 stsp goto done;
4158 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4159 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4160 b02560ec 2019-08-19 stsp blame->nlines--;
4162 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4163 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4164 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4165 84451b3e 2018-07-10 stsp goto done;
4168 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4170 bd24772e 2018-07-11 stsp goto done;
4172 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4173 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4174 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4175 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
4176 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4177 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4178 245d91c1 2018-07-12 stsp goto done;
4180 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
4182 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
4183 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4184 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4185 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
4186 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4187 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
4188 245d91c1 2018-07-12 stsp *blame_complete = 0;
4192 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4193 27d434c2 2018-09-15 stsp free(obj_id);
4195 245d91c1 2018-07-12 stsp stop_blame(blame);
4196 245d91c1 2018-07-12 stsp return err;
4199 245d91c1 2018-07-12 stsp static const struct got_error *
4200 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4201 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4202 8b473291 2019-02-21 stsp struct got_repository *repo)
4204 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4205 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4207 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4209 c4843652 2019-08-12 stsp s->path = strdup(path);
4210 c4843652 2019-08-12 stsp if (s->path == NULL)
4211 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4213 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4214 c4843652 2019-08-12 stsp if (err) {
4215 c4843652 2019-08-12 stsp free(s->path);
4216 7cbe629d 2018-08-04 stsp return err;
4219 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4220 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4221 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4222 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4223 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4224 fb2756b9 2018-08-04 stsp s->repo = repo;
4225 8b473291 2019-02-21 stsp s->refs = refs;
4226 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4227 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4229 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4230 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4231 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4232 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4234 11b20872 2019-11-08 stsp return err;
4237 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4238 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4239 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4240 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4241 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4243 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
4244 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
4245 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
4246 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
4249 e5a0f69f 2018-08-18 stsp static const struct got_error *
4250 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4252 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4253 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4255 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4256 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4258 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4259 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4260 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4261 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4262 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4265 e5a0f69f 2018-08-18 stsp free(s->path);
4266 11b20872 2019-11-08 stsp free_colors(&s->colors);
4268 e5a0f69f 2018-08-18 stsp return err;
4271 7cbe629d 2018-08-04 stsp static const struct got_error *
4272 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4274 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4276 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4277 6c4c42e0 2019-06-24 stsp return NULL;
4280 6c4c42e0 2019-06-24 stsp static const struct got_error *
4281 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4283 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4284 6c4c42e0 2019-06-24 stsp int lineno;
4286 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4287 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4288 6c4c42e0 2019-06-24 stsp return NULL;
4291 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4292 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4293 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4295 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4297 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4298 6c4c42e0 2019-06-24 stsp lineno = 1;
4300 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4303 6c4c42e0 2019-06-24 stsp while (1) {
4304 6c4c42e0 2019-06-24 stsp char *line = NULL;
4305 6c4c42e0 2019-06-24 stsp off_t offset;
4306 6c4c42e0 2019-06-24 stsp size_t len;
4308 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4309 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4310 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4311 6c4c42e0 2019-06-24 stsp free(line);
4315 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4316 6c4c42e0 2019-06-24 stsp lineno = 1;
4318 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4321 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4322 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4323 6c4c42e0 2019-06-24 stsp free(line);
4324 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4326 6c4c42e0 2019-06-24 stsp free(line);
4327 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
4328 41605754 2020-11-12 stsp if (line &&
4329 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4330 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4331 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4332 6c4c42e0 2019-06-24 stsp free(line);
4335 6c4c42e0 2019-06-24 stsp free(line);
4336 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4342 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4343 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4344 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4347 6c4c42e0 2019-06-24 stsp return NULL;
4350 6c4c42e0 2019-06-24 stsp static const struct got_error *
4351 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4353 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4354 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4355 2b380cc8 2018-10-24 stsp int errcode;
4357 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4358 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4359 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4360 2b380cc8 2018-10-24 stsp if (errcode)
4361 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4363 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4366 51fe7530 2019-08-19 stsp if (s->blame_complete)
4367 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4369 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4370 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4371 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
4372 41605754 2020-11-12 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4373 41605754 2020-11-12 stsp s->matched_line, &view->regmatch);
4375 669b5ffa 2018-10-07 stsp view_vborder(view);
4376 e5a0f69f 2018-08-18 stsp return err;
4379 e5a0f69f 2018-08-18 stsp static const struct got_error *
4380 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4381 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4383 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4384 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4385 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4386 669b5ffa 2018-10-07 stsp int begin_x = 0;
4388 e5a0f69f 2018-08-18 stsp switch (ch) {
4390 1e37a5c2 2019-05-12 jcs s->done = 1;
4393 1e37a5c2 2019-05-12 jcs case KEY_UP:
4394 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4395 1e37a5c2 2019-05-12 jcs s->selected_line--;
4396 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4397 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4398 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4400 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4401 ea025d1d 2020-02-22 naddy case CTRL('b'):
4402 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4403 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4406 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4407 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4408 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4410 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4413 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4414 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4415 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4416 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4417 1e37a5c2 2019-05-12 jcs s->selected_line++;
4418 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4419 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4420 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4423 1e37a5c2 2019-05-12 jcs case 'p': {
4424 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4425 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4426 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4427 1e37a5c2 2019-05-12 jcs if (id == NULL)
4429 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4430 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4431 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4432 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4433 1e37a5c2 2019-05-12 jcs int obj_type;
4434 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4435 1e37a5c2 2019-05-12 jcs s->repo, id);
4438 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4439 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4440 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4441 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4444 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4445 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4446 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4447 e5a0f69f 2018-08-18 stsp if (err) {
4448 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4449 1e37a5c2 2019-05-12 jcs err = NULL;
4450 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4453 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4455 1e37a5c2 2019-05-12 jcs free(blob_id);
4456 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4457 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4458 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4461 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4463 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4465 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4466 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4468 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4473 1e37a5c2 2019-05-12 jcs s->done = 1;
4474 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4475 1e37a5c2 2019-05-12 jcs s->done = 0;
4476 1e37a5c2 2019-05-12 jcs if (thread_err)
4478 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4479 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4480 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4481 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4482 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
4483 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
4488 1e37a5c2 2019-05-12 jcs case 'B': {
4489 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4490 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4491 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4493 1e37a5c2 2019-05-12 jcs s->done = 1;
4494 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4495 1e37a5c2 2019-05-12 jcs s->done = 0;
4496 1e37a5c2 2019-05-12 jcs if (thread_err)
4498 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4499 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4500 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4501 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4502 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4503 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4504 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
4505 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
4510 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4511 1e37a5c2 2019-05-12 jcs case '\r': {
4512 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4513 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4514 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4515 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4516 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4517 1e37a5c2 2019-05-12 jcs if (id == NULL)
4519 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4522 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4523 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4524 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4525 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4526 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4527 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4528 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4529 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4532 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4533 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
4534 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4536 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4539 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4540 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4543 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4545 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4548 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4549 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4551 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4556 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4557 ea025d1d 2020-02-22 naddy case CTRL('f'):
4559 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4560 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4561 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4564 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4565 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4566 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4567 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4570 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4571 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4572 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4573 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4575 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4576 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4577 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4579 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4580 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4581 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4582 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4588 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4591 a70480e0 2018-06-23 stsp static const struct got_error *
4592 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4594 a70480e0 2018-06-23 stsp const struct got_error *error;
4595 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4596 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4597 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4598 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4599 0587e10c 2020-07-23 stsp char *link_target = NULL;
4600 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4601 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4603 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4605 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4607 a70480e0 2018-06-23 stsp #ifndef PROFILE
4608 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4609 8e94dd5b 2019-01-04 stsp NULL) == -1)
4610 a70480e0 2018-06-23 stsp err(1, "pledge");
4613 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4614 a70480e0 2018-06-23 stsp switch (ch) {
4616 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4619 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4620 69069811 2018-08-02 stsp if (repo_path == NULL)
4621 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4625 17020d27 2019-03-07 stsp usage_blame();
4626 a70480e0 2018-06-23 stsp /* NOTREACHED */
4630 a70480e0 2018-06-23 stsp argc -= optind;
4631 a70480e0 2018-06-23 stsp argv += optind;
4633 f135c941 2020-02-20 stsp if (argc != 1)
4634 a70480e0 2018-06-23 stsp usage_blame();
4636 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4637 6962eb72 2020-02-20 stsp if (cwd == NULL)
4638 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
4640 f135c941 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
4641 f135c941 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4642 f135c941 2020-02-20 stsp goto done;
4644 69069811 2018-08-02 stsp if (repo_path == NULL) {
4645 f135c941 2020-02-20 stsp if (worktree)
4646 eb41ed75 2019-02-05 stsp repo_path =
4647 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4649 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4651 f135c941 2020-02-20 stsp if (repo_path == NULL) {
4652 f135c941 2020-02-20 stsp error = got_error_from_errno("strdup");
4653 f135c941 2020-02-20 stsp goto done;
4656 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4657 c02c541e 2019-03-29 stsp if (error != NULL)
4658 8e94dd5b 2019-01-04 stsp goto done;
4660 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4661 f135c941 2020-02-20 stsp worktree);
4662 c02c541e 2019-03-29 stsp if (error)
4663 92205607 2019-01-04 stsp goto done;
4665 f135c941 2020-02-20 stsp init_curses();
4667 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4668 eb41ed75 2019-02-05 stsp if (error)
4669 69069811 2018-08-02 stsp goto done;
4671 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4672 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4673 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4674 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4675 a70480e0 2018-06-23 stsp if (error != NULL)
4676 66b4983c 2018-06-23 stsp goto done;
4677 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4678 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4680 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4681 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4683 a19e88aa 2018-06-23 stsp if (error != NULL)
4684 8b473291 2019-02-21 stsp goto done;
4686 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4687 8b473291 2019-02-21 stsp if (error)
4688 a19e88aa 2018-06-23 stsp goto done;
4690 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4691 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4692 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4693 e1cd8fed 2018-08-01 stsp goto done;
4696 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4697 0587e10c 2020-07-23 stsp commit_id, repo);
4698 7cbe629d 2018-08-04 stsp if (error)
4699 7cbe629d 2018-08-04 stsp goto done;
4701 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4702 0587e10c 2020-07-23 stsp commit_id, &refs, repo);
4703 0587e10c 2020-07-23 stsp if (error)
4704 0587e10c 2020-07-23 stsp goto done;
4705 12314ad4 2019-08-31 stsp if (worktree) {
4706 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4707 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4708 12314ad4 2019-08-31 stsp worktree = NULL;
4710 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4712 69069811 2018-08-02 stsp free(repo_path);
4713 f135c941 2020-02-20 stsp free(in_repo_path);
4714 0587e10c 2020-07-23 stsp free(link_target);
4715 69069811 2018-08-02 stsp free(cwd);
4716 a70480e0 2018-06-23 stsp free(commit_id);
4717 eb41ed75 2019-02-05 stsp if (worktree)
4718 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4720 a70480e0 2018-06-23 stsp got_repo_close(repo);
4721 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4722 a70480e0 2018-06-23 stsp return error;
4725 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4726 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4727 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4728 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4729 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4730 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4731 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4732 0d6c6ee3 2020-05-20 stsp int isroot, struct tog_colors *colors, struct got_repository *repo)
4734 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4735 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4736 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4737 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4738 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4740 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4742 f7d12f7e 2018-08-01 stsp werase(view->window);
4744 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4745 ffd1d5e5 2018-06-23 stsp return NULL;
4747 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4749 ffd1d5e5 2018-06-23 stsp return err;
4750 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4751 a3404814 2018-09-02 stsp wstandout(view->window);
4752 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4754 11b20872 2019-11-08 stsp wattr_on(view->window,
4755 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4756 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4758 11b20872 2019-11-08 stsp wattr_off(view->window,
4759 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4760 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4761 a3404814 2018-09-02 stsp wstandend(view->window);
4762 2550e4c3 2018-07-13 stsp free(wline);
4763 2550e4c3 2018-07-13 stsp wline = NULL;
4764 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4765 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4766 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4767 ffd1d5e5 2018-06-23 stsp return NULL;
4768 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4770 ce52c690 2018-06-23 stsp return err;
4771 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4772 2550e4c3 2018-07-13 stsp free(wline);
4773 2550e4c3 2018-07-13 stsp wline = NULL;
4774 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4775 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4776 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4777 ffd1d5e5 2018-06-23 stsp return NULL;
4778 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4779 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4780 a1eca9bb 2018-06-23 stsp return NULL;
4782 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4783 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4784 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4785 0cf4efb1 2018-09-29 stsp if (view->focussed)
4786 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4787 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4789 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4790 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4791 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4792 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4793 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4794 ffd1d5e5 2018-06-23 stsp return NULL;
4798 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4801 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4802 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4803 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4804 848d6979 2019-08-12 stsp const char *modestr = "";
4805 56e0773d 2019-11-28 stsp mode_t mode;
4807 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4808 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4810 1d13200f 2018-07-12 stsp if (show_ids) {
4811 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4812 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4814 638f9024 2019-05-13 stsp return got_error_from_errno(
4815 230a42bd 2019-05-11 jcs "got_object_id_str");
4817 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4818 63c5ca5d 2019-08-24 stsp modestr = "$";
4819 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4822 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4823 0d6c6ee3 2020-05-20 stsp te, repo);
4824 0d6c6ee3 2020-05-20 stsp if (err) {
4825 0d6c6ee3 2020-05-20 stsp free(id_str);
4826 0d6c6ee3 2020-05-20 stsp return err;
4828 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4829 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4830 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4832 848d6979 2019-08-12 stsp modestr = "@";
4834 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4835 848d6979 2019-08-12 stsp modestr = "/";
4836 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4837 848d6979 2019-08-12 stsp modestr = "*";
4838 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4839 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
4840 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
4841 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
4842 1d13200f 2018-07-12 stsp free(id_str);
4843 0d6c6ee3 2020-05-20 stsp free(link_target);
4844 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4846 1d13200f 2018-07-12 stsp free(id_str);
4847 0d6c6ee3 2020-05-20 stsp free(link_target);
4848 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4849 ffd1d5e5 2018-06-23 stsp if (err) {
4850 ffd1d5e5 2018-06-23 stsp free(line);
4853 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4854 0cf4efb1 2018-09-29 stsp if (view->focussed)
4855 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4856 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4858 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4860 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4861 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4862 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4864 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4865 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4866 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4867 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4868 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4869 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4870 ffd1d5e5 2018-06-23 stsp free(line);
4871 2550e4c3 2018-07-13 stsp free(wline);
4872 2550e4c3 2018-07-13 stsp wline = NULL;
4874 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4875 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4876 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4880 ffd1d5e5 2018-06-23 stsp return err;
4883 ffd1d5e5 2018-06-23 stsp static void
4884 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4885 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4886 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int isroot)
4888 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4891 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4894 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, 0);
4895 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4896 ffd1d5e5 2018-06-23 stsp if (!isroot)
4897 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4902 56e0773d 2019-11-28 stsp while (*first_displayed_entry && i < maxscroll) {
4903 56e0773d 2019-11-28 stsp *first_displayed_entry = got_tree_entry_get_prev(tree,
4904 56e0773d 2019-11-28 stsp *first_displayed_entry);
4907 56e0773d 2019-11-28 stsp if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4908 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4911 768394f3 2019-01-24 stsp static int
4912 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4913 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4914 56e0773d 2019-11-28 stsp struct got_tree_object *tree)
4916 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4917 ffd1d5e5 2018-06-23 stsp int n = 0;
4919 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4920 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, *first_displayed_entry);
4922 56e0773d 2019-11-28 stsp next = got_object_tree_get_first_entry(tree);
4924 768394f3 2019-01-24 stsp last = last_displayed_entry;
4925 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4926 56e0773d 2019-11-28 stsp last = got_tree_entry_get_next(tree, last);
4927 768394f3 2019-01-24 stsp if (last) {
4928 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4929 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, next);
4935 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4936 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4937 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4939 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4940 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4941 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4943 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4944 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4945 56e0773d 2019-11-28 stsp + 1 /* slash */;
4947 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4949 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4950 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4951 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4953 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4954 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4955 d9765a41 2018-06-23 stsp while (pt) {
4956 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4957 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4958 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4959 cb2ebc8a 2018-06-23 stsp goto done;
4961 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4962 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4963 cb2ebc8a 2018-06-23 stsp goto done;
4965 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4968 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4969 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4970 ce52c690 2018-06-23 stsp goto done;
4974 ce52c690 2018-06-23 stsp if (err) {
4975 ce52c690 2018-06-23 stsp free(*path);
4976 ce52c690 2018-06-23 stsp *path = NULL;
4978 ce52c690 2018-06-23 stsp return err;
4981 ce52c690 2018-06-23 stsp static const struct got_error *
4982 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4983 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4984 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4985 8b473291 2019-02-21 stsp struct got_repository *repo)
4987 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4988 ce52c690 2018-06-23 stsp char *path;
4989 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4991 a0de39f3 2019-08-09 stsp *new_view = NULL;
4993 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4995 ce52c690 2018-06-23 stsp return err;
4997 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4998 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4999 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5000 83ce39e3 2019-08-12 stsp goto done;
5003 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
5004 e5a0f69f 2018-08-18 stsp if (err) {
5005 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5006 fc06ba56 2019-08-22 stsp err = NULL;
5007 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5009 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5011 83ce39e3 2019-08-12 stsp free(path);
5012 69efd4c4 2018-07-18 stsp return err;
5015 69efd4c4 2018-07-18 stsp static const struct got_error *
5016 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
5017 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5018 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
5019 8b473291 2019-02-21 stsp struct got_repository *repo)
5021 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5022 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5023 69efd4c4 2018-07-18 stsp char *path;
5025 a0de39f3 2019-08-09 stsp *new_view = NULL;
5027 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5028 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5029 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5031 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
5033 69efd4c4 2018-07-18 stsp return err;
5035 f135c941 2020-02-20 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
5037 e5a0f69f 2018-08-18 stsp view_close(log_view);
5039 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5040 cb2ebc8a 2018-06-23 stsp free(path);
5041 cb2ebc8a 2018-06-23 stsp return err;
5044 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5045 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
5046 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
5047 8b473291 2019-02-21 stsp struct got_repository *repo)
5049 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5050 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5051 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5053 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5055 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5056 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5057 ffd1d5e5 2018-06-23 stsp goto done;
5059 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5060 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5061 ffd1d5e5 2018-06-23 stsp goto done;
5064 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
5065 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5066 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5067 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
5068 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
5069 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5070 6484ec90 2018-09-29 stsp goto done;
5072 8b473291 2019-02-21 stsp s->refs = refs;
5073 fb2756b9 2018-08-04 stsp s->repo = repo;
5075 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
5077 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5078 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5079 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5080 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5082 c0b01bdb 2019-11-08 stsp goto done;
5083 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5084 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5085 c0b01bdb 2019-11-08 stsp if (err) {
5086 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5087 c0b01bdb 2019-11-08 stsp goto done;
5089 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5090 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5091 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5092 c0b01bdb 2019-11-08 stsp if (err) {
5093 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5094 c0b01bdb 2019-11-08 stsp goto done;
5097 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5098 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5099 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5100 11b20872 2019-11-08 stsp if (err) {
5101 11b20872 2019-11-08 stsp free_colors(&s->colors);
5102 11b20872 2019-11-08 stsp goto done;
5105 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5106 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5107 c0b01bdb 2019-11-08 stsp if (err) {
5108 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5109 c0b01bdb 2019-11-08 stsp goto done;
5113 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5114 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5115 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5116 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5117 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5119 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5120 6484ec90 2018-09-29 stsp if (err) {
5121 fb2756b9 2018-08-04 stsp free(s->tree_label);
5122 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5124 ad80ab7b 2018-08-04 stsp return err;
5127 e5a0f69f 2018-08-18 stsp static const struct got_error *
5128 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5130 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5132 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5133 fb2756b9 2018-08-04 stsp free(s->tree_label);
5134 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5135 6484ec90 2018-09-29 stsp free(s->commit_id);
5136 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5137 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5138 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5139 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5140 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5141 ad80ab7b 2018-08-04 stsp free(parent);
5144 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
5145 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5146 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
5148 7c32bd05 2019-06-22 stsp return NULL;
5151 7c32bd05 2019-06-22 stsp static const struct got_error *
5152 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5154 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5156 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5157 7c32bd05 2019-06-22 stsp return NULL;
5160 7c32bd05 2019-06-22 stsp static int
5161 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5163 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5165 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5169 7c32bd05 2019-06-22 stsp static const struct got_error *
5170 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5172 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5173 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5175 7c32bd05 2019-06-22 stsp if (!view->searching) {
5176 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5177 7c32bd05 2019-06-22 stsp return NULL;
5180 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5181 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5182 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5183 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5184 56e0773d 2019-11-28 stsp s->selected_entry);
5186 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5188 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5189 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5191 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5192 56e0773d 2019-11-28 stsp s->selected_entry);
5195 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5196 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5198 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5201 7c32bd05 2019-06-22 stsp while (1) {
5202 56e0773d 2019-11-28 stsp if (te == NULL) {
5203 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5204 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5205 ac66afb8 2019-06-24 stsp return NULL;
5207 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5208 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5210 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5213 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5214 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5215 56e0773d 2019-11-28 stsp s->matched_entry = te;
5219 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5220 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5222 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5225 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5226 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5227 7c32bd05 2019-06-22 stsp s->selected = 0;
5230 e5a0f69f 2018-08-18 stsp return NULL;
5233 ad80ab7b 2018-08-04 stsp static const struct got_error *
5234 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5236 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5237 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5238 e5a0f69f 2018-08-18 stsp char *parent_path;
5240 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5242 e5a0f69f 2018-08-18 stsp return err;
5244 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
5245 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
5246 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5247 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
5248 0d6c6ee3 2020-05-20 stsp &s->colors, s->repo);
5249 e5a0f69f 2018-08-18 stsp free(parent_path);
5251 669b5ffa 2018-10-07 stsp view_vborder(view);
5252 e5a0f69f 2018-08-18 stsp return err;
5255 e5a0f69f 2018-08-18 stsp static const struct got_error *
5256 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5257 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
5259 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5260 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5261 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
5262 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
5264 e5a0f69f 2018-08-18 stsp switch (ch) {
5266 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5269 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5271 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5272 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5273 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
5274 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
5275 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
5276 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5277 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5279 1e37a5c2 2019-05-12 jcs return err;
5280 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
5282 1e37a5c2 2019-05-12 jcs view_close(log_view);
5285 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
5286 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
5288 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5291 1e37a5c2 2019-05-12 jcs case KEY_UP:
5292 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5293 1e37a5c2 2019-05-12 jcs s->selected--;
5294 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
5297 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
5299 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
5300 56e0773d 2019-11-28 stsp s->tree, s->tree == s->root);
5302 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5303 ea025d1d 2020-02-22 naddy case CTRL('b'):
5304 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
5305 56e0773d 2019-11-28 stsp MAX(0, view->nlines - 4 - s->selected), s->tree,
5306 1e37a5c2 2019-05-12 jcs s->tree == s->root);
5307 1e37a5c2 2019-05-12 jcs s->selected = 0;
5308 56e0773d 2019-11-28 stsp if (got_object_tree_get_first_entry(s->tree) ==
5309 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
5310 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
5313 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5314 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5315 1e37a5c2 2019-05-12 jcs s->selected++;
5318 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5320 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5322 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
5323 56e0773d 2019-11-28 stsp s->last_displayed_entry, s->tree);
5325 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5326 ea025d1d 2020-02-22 naddy case CTRL('f'):
5327 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5329 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5330 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5331 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5334 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
5335 56e0773d 2019-11-28 stsp view->nlines, s->last_displayed_entry, s->tree);
5336 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
5337 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
5338 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
5339 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
5341 1e37a5c2 2019-05-12 jcs ndisplayed++;
5342 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5343 1e37a5c2 2019-05-12 jcs } while (te);
5344 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
5347 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5349 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5350 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5351 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5352 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5353 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5355 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5356 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5358 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5359 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5360 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5361 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5362 1e37a5c2 2019-05-12 jcs s->selected_entry =
5363 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5364 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5365 1e37a5c2 2019-05-12 jcs free(parent);
5366 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5367 56e0773d 2019-11-28 stsp s->selected_entry))) {
5368 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5369 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5370 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5373 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
5374 941e9f74 2019-05-21 stsp if (err) {
5375 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5378 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5379 56e0773d 2019-11-28 stsp s->selected_entry))) {
5380 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5381 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5382 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5384 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5385 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5386 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
5389 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5390 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5392 669b5ffa 2018-10-07 stsp return err;
5393 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
5394 669b5ffa 2018-10-07 stsp if (err) {
5395 1e37a5c2 2019-05-12 jcs view_close(blame_view);
5398 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
5399 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
5401 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5404 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5405 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
5406 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5412 ffd1d5e5 2018-06-23 stsp return err;
5415 ffd1d5e5 2018-06-23 stsp __dead static void
5416 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5419 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5420 ffd1d5e5 2018-06-23 stsp getprogname());
5424 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5425 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5427 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5428 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5429 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5430 8b473291 2019-02-21 stsp struct got_reflist_head refs;
5431 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5432 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5433 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
5434 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5435 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5437 5221c383 2018-08-01 stsp struct tog_view *view;
5439 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
5441 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
5442 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5443 d188b9a6 2019-01-04 stsp NULL) == -1)
5444 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
5447 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5448 ffd1d5e5 2018-06-23 stsp switch (ch) {
5450 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5453 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5454 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5455 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5459 17020d27 2019-03-07 stsp usage_tree();
5460 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5464 ffd1d5e5 2018-06-23 stsp argc -= optind;
5465 ffd1d5e5 2018-06-23 stsp argv += optind;
5467 55cccc34 2020-02-20 stsp if (argc > 1)
5468 74283ab8 2020-02-07 stsp usage_tree();
5470 55cccc34 2020-02-20 stsp cwd = getcwd(NULL, 0);
5471 55cccc34 2020-02-20 stsp if (cwd == NULL)
5472 55cccc34 2020-02-20 stsp return got_error_from_errno("getcwd");
5474 55cccc34 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
5475 55cccc34 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5476 55cccc34 2020-02-20 stsp goto done;
5478 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5479 55cccc34 2020-02-20 stsp if (worktree)
5480 52185f70 2019-02-05 stsp repo_path =
5481 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5483 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5485 55cccc34 2020-02-20 stsp if (repo_path == NULL) {
5486 55cccc34 2020-02-20 stsp error = got_error_from_errno("strdup");
5487 55cccc34 2020-02-20 stsp goto done;
5490 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5491 c02c541e 2019-03-29 stsp if (error != NULL)
5492 52185f70 2019-02-05 stsp goto done;
5494 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5495 55cccc34 2020-02-20 stsp repo, worktree);
5496 55cccc34 2020-02-20 stsp if (error)
5497 55cccc34 2020-02-20 stsp goto done;
5499 55cccc34 2020-02-20 stsp init_curses();
5501 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5502 c02c541e 2019-03-29 stsp if (error)
5503 52185f70 2019-02-05 stsp goto done;
5505 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5506 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5507 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5508 55cccc34 2020-02-20 stsp if (error)
5509 ffd1d5e5 2018-06-23 stsp goto done;
5511 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5512 55cccc34 2020-02-20 stsp if (error)
5513 ffd1d5e5 2018-06-23 stsp goto done;
5515 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5516 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5517 55cccc34 2020-02-20 stsp if (error)
5518 8b473291 2019-02-21 stsp goto done;
5520 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5521 8b473291 2019-02-21 stsp if (error)
5522 ffd1d5e5 2018-06-23 stsp goto done;
5524 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5525 5221c383 2018-08-01 stsp if (view == NULL) {
5526 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5527 5221c383 2018-08-01 stsp goto done;
5529 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
5530 ad80ab7b 2018-08-04 stsp if (error)
5531 ad80ab7b 2018-08-04 stsp goto done;
5532 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5533 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5534 55cccc34 2020-02-20 stsp in_repo_path, repo);
5535 55cccc34 2020-02-20 stsp if (error)
5536 55cccc34 2020-02-20 stsp goto done;
5539 55cccc34 2020-02-20 stsp if (worktree) {
5540 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5541 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5542 55cccc34 2020-02-20 stsp worktree = NULL;
5544 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5546 52185f70 2019-02-05 stsp free(repo_path);
5547 e4a0e26d 2020-02-20 stsp free(cwd);
5548 ffd1d5e5 2018-06-23 stsp free(commit_id);
5549 ffd1d5e5 2018-06-23 stsp if (commit)
5550 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5552 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5554 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5555 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5556 ffd1d5e5 2018-06-23 stsp return error;
5559 ce5b7c56 2019-07-09 stsp static void
5560 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
5564 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
5565 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
5566 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
5567 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
5569 6879ba42 2020-10-01 naddy fputc('\n', fp);
5572 4ed7e80c 2018-05-20 stsp __dead static void
5573 6879ba42 2020-10-01 naddy usage(int hflag, int status)
5575 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
5577 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5578 6879ba42 2020-10-01 naddy getprogname());
5579 6879ba42 2020-10-01 naddy if (hflag) {
5580 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
5581 6879ba42 2020-10-01 naddy list_commands(fp);
5583 6879ba42 2020-10-01 naddy exit(status);
5586 c2301be8 2018-04-30 stsp static char **
5587 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
5589 ee85c5e8 2020-02-29 stsp va_list ap;
5590 c2301be8 2018-04-30 stsp char **argv;
5593 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
5595 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
5596 c2301be8 2018-04-30 stsp if (argv == NULL)
5597 c2301be8 2018-04-30 stsp err(1, "calloc");
5598 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
5599 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
5600 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
5601 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5604 ee85c5e8 2020-02-29 stsp va_end(ap);
5605 c2301be8 2018-04-30 stsp return argv;
5609 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
5610 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
5611 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
5612 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
5613 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
5615 ee85c5e8 2020-02-29 stsp static const struct got_error *
5616 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
5618 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
5619 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
5620 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
5621 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
5622 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
5623 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5624 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
5626 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
5627 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
5628 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
5630 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
5631 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5632 ee85c5e8 2020-02-29 stsp goto done;
5634 ee85c5e8 2020-02-29 stsp if (worktree)
5635 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
5637 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
5638 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
5639 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
5640 ee85c5e8 2020-02-29 stsp goto done;
5643 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
5644 ee85c5e8 2020-02-29 stsp if (error != NULL)
5645 ee85c5e8 2020-02-29 stsp goto done;
5647 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5648 ee85c5e8 2020-02-29 stsp repo, worktree);
5649 ee85c5e8 2020-02-29 stsp if (error)
5650 ee85c5e8 2020-02-29 stsp goto done;
5652 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5653 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5654 ee85c5e8 2020-02-29 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5655 ee85c5e8 2020-02-29 stsp if (error)
5656 ee85c5e8 2020-02-29 stsp goto done;
5658 ee85c5e8 2020-02-29 stsp if (worktree) {
5659 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
5660 ee85c5e8 2020-02-29 stsp worktree = NULL;
5663 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5664 ee85c5e8 2020-02-29 stsp if (error) {
5665 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
5666 ee85c5e8 2020-02-29 stsp goto done;
5667 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
5668 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
5669 6879ba42 2020-10-01 naddy usage(1, 1);
5670 ee85c5e8 2020-02-29 stsp /* not reached */
5673 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
5674 ee85c5e8 2020-02-29 stsp repo = NULL;
5676 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
5677 ee85c5e8 2020-02-29 stsp if (error)
5678 ee85c5e8 2020-02-29 stsp goto done;
5680 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
5682 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5683 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
5686 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
5687 ee85c5e8 2020-02-29 stsp if (worktree)
5688 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
5690 ee85c5e8 2020-02-29 stsp free(commit_id_str);
5691 ee85c5e8 2020-02-29 stsp free(commit_id);
5692 ee85c5e8 2020-02-29 stsp free(cwd);
5693 ee85c5e8 2020-02-29 stsp free(repo_path);
5694 ee85c5e8 2020-02-29 stsp free(in_repo_path);
5695 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
5697 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
5698 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
5699 ee85c5e8 2020-02-29 stsp free(cmd_argv);
5701 ee85c5e8 2020-02-29 stsp return error;
5705 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
5707 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
5708 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
5709 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
5710 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
5711 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
5712 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
5713 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
5716 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
5718 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5719 9f7d7167 2018-04-29 stsp switch (ch) {
5721 9f7d7167 2018-04-29 stsp hflag = 1;
5724 53ccebc2 2019-07-30 stsp Vflag = 1;
5727 6879ba42 2020-10-01 naddy usage(hflag, 1);
5728 9f7d7167 2018-04-29 stsp /* NOTREACHED */
5732 9f7d7167 2018-04-29 stsp argc -= optind;
5733 9f7d7167 2018-04-29 stsp argv += optind;
5734 9814e6a3 2020-09-27 naddy optind = 1;
5735 c2301be8 2018-04-30 stsp optreset = 1;
5737 53ccebc2 2019-07-30 stsp if (Vflag) {
5738 53ccebc2 2019-07-30 stsp got_version_print_str();
5739 6879ba42 2020-10-01 naddy return 0;
5742 c2301be8 2018-04-30 stsp if (argc == 0) {
5743 f29d3e89 2018-06-23 stsp if (hflag)
5744 6879ba42 2020-10-01 naddy usage(hflag, 0);
5745 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
5746 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
5748 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
5752 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
5753 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
5754 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
5755 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
5756 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
5762 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
5763 ee85c5e8 2020-02-29 stsp if (argc != 1)
5764 6879ba42 2020-10-01 naddy usage(0, 1);
5765 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
5766 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
5768 ee85c5e8 2020-02-29 stsp if (hflag)
5769 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
5771 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5775 b46c1e04 2020-09-20 naddy putchar('\n');
5776 a2f4a359 2020-02-28 stsp if (cmd_argv) {
5778 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
5779 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
5780 a2f4a359 2020-02-28 stsp free(cmd_argv);
5783 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
5784 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);