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 64453f7e 2020-11-21 stsp int force_text_diff;
253 15a087fe 2019-02-21 stsp struct got_repository *repo;
254 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
255 bddb1296 2019-11-08 stsp struct tog_colors colors;
256 fe621944 2020-11-10 stsp size_t nlines;
257 f44b1f58 2020-02-02 tracey off_t *line_offsets;
258 f44b1f58 2020-02-02 tracey int matched_line;
259 f44b1f58 2020-02-02 tracey int selected_line;
261 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
262 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
265 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
267 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
268 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
269 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
270 1a76625f 2018-10-22 stsp int commits_needed;
271 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
272 1a76625f 2018-10-22 stsp struct commit_queue *commits;
273 1a76625f 2018-10-22 stsp const char *in_repo_path;
274 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
275 1a76625f 2018-10-22 stsp struct got_repository *repo;
276 1a76625f 2018-10-22 stsp int log_complete;
277 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
278 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
279 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
280 13add988 2019-10-15 stsp int *searching;
281 13add988 2019-10-15 stsp int *search_next_done;
282 13add988 2019-10-15 stsp regex_t *regex;
285 1a76625f 2018-10-22 stsp struct tog_log_view_state {
286 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
287 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
288 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
289 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
290 b01e7d3b 2018-08-04 stsp int selected;
291 b01e7d3b 2018-08-04 stsp char *in_repo_path;
292 d01904d4 2019-06-25 stsp const char *head_ref_name;
293 b672a97a 2020-01-27 stsp int log_branches;
294 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
295 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
296 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
297 1a76625f 2018-10-22 stsp sig_atomic_t quit;
298 1a76625f 2018-10-22 stsp pthread_t thread;
299 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
300 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
301 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
302 11b20872 2019-11-08 stsp struct tog_colors colors;
305 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
306 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
307 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
308 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
309 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
310 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
311 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
312 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
313 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
314 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
315 11b20872 2019-11-08 stsp #define TOG_COLOR_DATE 11
317 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
318 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
319 e9424729 2018-08-04 stsp int nlines;
321 e9424729 2018-08-04 stsp struct tog_view *view;
322 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
326 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
327 e9424729 2018-08-04 stsp const char *path;
328 e9424729 2018-08-04 stsp struct got_repository *repo;
329 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
330 e9424729 2018-08-04 stsp int *complete;
331 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
332 fc06ba56 2019-08-22 stsp void *cancel_arg;
335 e9424729 2018-08-04 stsp struct tog_blame {
337 be659d10 2020-11-18 stsp off_t filesize;
338 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
339 6fcac457 2018-11-19 stsp int nlines;
340 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
341 e9424729 2018-08-04 stsp pthread_t thread;
342 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
343 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
344 e9424729 2018-08-04 stsp const char *path;
347 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
348 7cbe629d 2018-08-04 stsp int first_displayed_line;
349 7cbe629d 2018-08-04 stsp int last_displayed_line;
350 7cbe629d 2018-08-04 stsp int selected_line;
351 7cbe629d 2018-08-04 stsp int blame_complete;
354 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
355 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
356 e5a0f69f 2018-08-18 stsp char *path;
357 7cbe629d 2018-08-04 stsp struct got_repository *repo;
358 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
359 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
360 e9424729 2018-08-04 stsp struct tog_blame blame;
361 6c4c42e0 2019-06-24 stsp int matched_line;
362 11b20872 2019-11-08 stsp struct tog_colors colors;
365 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
366 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
367 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
368 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
369 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
370 ad80ab7b 2018-08-04 stsp int selected;
373 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
375 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
376 ad80ab7b 2018-08-04 stsp char *tree_label;
377 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
378 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
379 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
380 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
381 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
382 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
383 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
384 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
385 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
386 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
387 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
388 bddb1296 2019-11-08 stsp struct tog_colors colors;
392 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
394 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
395 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
396 669b5ffa 2018-10-07 stsp * there is enough screen estate.
398 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
399 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
401 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
402 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
403 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
405 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
406 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
408 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
410 cc3c9aac 2018-08-01 stsp struct tog_view {
411 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
412 26ed57b2 2018-05-19 stsp WINDOW *window;
413 26ed57b2 2018-05-19 stsp PANEL *panel;
414 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
415 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
416 1004088d 2018-09-29 stsp int focussed;
417 669b5ffa 2018-10-07 stsp struct tog_view *parent;
418 669b5ffa 2018-10-07 stsp struct tog_view *child;
419 669b5ffa 2018-10-07 stsp int child_focussed;
421 5dc9f4bc 2018-08-04 stsp /* type-specific state */
422 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
424 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
425 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
426 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
427 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
430 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
431 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
432 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
433 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
435 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
436 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
437 60493ae3 2019-06-20 stsp int searching;
438 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
439 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
440 60493ae3 2019-06-20 stsp int search_next_done;
441 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
442 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
443 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
444 1803e47f 2019-06-22 stsp regex_t regex;
445 41605754 2020-11-12 stsp regmatch_t regmatch;
448 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
449 64453f7e 2020-11-21 stsp struct got_object_id *, struct got_object_id *, int, struct tog_view *,
450 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
451 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
452 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
453 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
454 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
455 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
456 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
458 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
459 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
460 f135c941 2020-02-20 stsp struct got_repository *, const char *, const char *, int);
461 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
462 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
463 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
464 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
465 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
466 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
468 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
469 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
470 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
471 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
472 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
473 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
474 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
475 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
477 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
478 55cccc34 2020-02-20 stsp struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
479 55cccc34 2020-02-20 stsp struct got_repository *);
480 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
481 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
482 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
483 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
484 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
485 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
487 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
488 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
489 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
491 25791caa 2018-10-24 stsp static void
492 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
494 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
497 83baff54 2019-08-12 stsp static void
498 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
500 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
503 61266923 2020-01-14 stsp static void
504 61266923 2020-01-14 stsp tog_sigcont(int signo)
506 61266923 2020-01-14 stsp tog_sigcont_received = 1;
509 e5a0f69f 2018-08-18 stsp static const struct got_error *
510 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
512 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
514 669b5ffa 2018-10-07 stsp if (view->child) {
515 669b5ffa 2018-10-07 stsp view_close(view->child);
516 669b5ffa 2018-10-07 stsp view->child = NULL;
518 e5a0f69f 2018-08-18 stsp if (view->close)
519 e5a0f69f 2018-08-18 stsp err = view->close(view);
520 ea5e7bb5 2018-08-01 stsp if (view->panel)
521 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
522 ea5e7bb5 2018-08-01 stsp if (view->window)
523 ea5e7bb5 2018-08-01 stsp delwin(view->window);
524 ea5e7bb5 2018-08-01 stsp free(view);
525 e5a0f69f 2018-08-18 stsp return err;
528 ea5e7bb5 2018-08-01 stsp static struct tog_view *
529 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
530 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
532 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
534 ea5e7bb5 2018-08-01 stsp if (view == NULL)
535 ea5e7bb5 2018-08-01 stsp return NULL;
537 d6b05b5b 2018-08-04 stsp view->type = type;
538 f7d12f7e 2018-08-01 stsp view->lines = LINES;
539 f7d12f7e 2018-08-01 stsp view->cols = COLS;
540 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
541 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
542 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
543 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
544 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
545 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
546 96a765a8 2018-08-04 stsp view_close(view);
547 ea5e7bb5 2018-08-01 stsp return NULL;
549 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
550 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
551 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
552 96a765a8 2018-08-04 stsp view_close(view);
553 ea5e7bb5 2018-08-01 stsp return NULL;
556 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
557 ea5e7bb5 2018-08-01 stsp return view;
561 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
563 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
565 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
568 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
570 5c60c32a 2018-10-18 stsp static const struct got_error *
571 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
573 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
575 5c60c32a 2018-10-18 stsp view->begin_y = 0;
576 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
577 5c60c32a 2018-10-18 stsp view->nlines = LINES;
578 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
579 5c60c32a 2018-10-18 stsp view->lines = LINES;
580 5c60c32a 2018-10-18 stsp view->cols = COLS;
581 5c60c32a 2018-10-18 stsp err = view_resize(view);
583 5c60c32a 2018-10-18 stsp return err;
585 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
586 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
588 5c60c32a 2018-10-18 stsp return NULL;
591 5c60c32a 2018-10-18 stsp static const struct got_error *
592 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
594 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
596 5c60c32a 2018-10-18 stsp view->begin_x = 0;
597 5c60c32a 2018-10-18 stsp view->begin_y = 0;
598 5c60c32a 2018-10-18 stsp view->nlines = LINES;
599 5c60c32a 2018-10-18 stsp view->ncols = COLS;
600 5c60c32a 2018-10-18 stsp view->lines = LINES;
601 5c60c32a 2018-10-18 stsp view->cols = COLS;
602 5c60c32a 2018-10-18 stsp err = view_resize(view);
604 5c60c32a 2018-10-18 stsp return err;
606 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
607 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
609 5c60c32a 2018-10-18 stsp return NULL;
613 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
615 5c60c32a 2018-10-18 stsp return view->parent == NULL;
618 4d8c2215 2018-08-19 stsp static const struct got_error *
619 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
621 f7d12f7e 2018-08-01 stsp int nlines, ncols;
623 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
624 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
626 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
628 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
629 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
631 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
633 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
634 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
635 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
636 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
637 25791caa 2018-10-24 stsp wclear(view->window);
639 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
640 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
641 0cf4efb1 2018-09-29 stsp view->lines = LINES;
642 0cf4efb1 2018-09-29 stsp view->cols = COLS;
644 6e3e5d9c 2018-10-18 stsp if (view->child) {
645 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
646 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
647 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
648 5c60c32a 2018-10-18 stsp if (view->child->focussed)
649 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
651 5c60c32a 2018-10-18 stsp show_panel(view->panel);
653 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
654 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
658 5c60c32a 2018-10-18 stsp return NULL;
661 669b5ffa 2018-10-07 stsp static const struct got_error *
662 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
664 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
666 669b5ffa 2018-10-07 stsp if (view->child == NULL)
667 669b5ffa 2018-10-07 stsp return NULL;
669 669b5ffa 2018-10-07 stsp err = view_close(view->child);
670 669b5ffa 2018-10-07 stsp view->child = NULL;
671 669b5ffa 2018-10-07 stsp return err;
674 669b5ffa 2018-10-07 stsp static const struct got_error *
675 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
677 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
679 669b5ffa 2018-10-07 stsp view->child = child;
680 669b5ffa 2018-10-07 stsp child->parent = view;
681 669b5ffa 2018-10-07 stsp return err;
685 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
687 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
690 34bc9ec9 2019-02-22 stsp static void
691 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
693 25791caa 2018-10-24 stsp int cols, lines;
694 25791caa 2018-10-24 stsp struct winsize size;
696 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
697 25791caa 2018-10-24 stsp cols = 80; /* Default */
698 25791caa 2018-10-24 stsp lines = 24;
700 25791caa 2018-10-24 stsp cols = size.ws_col;
701 25791caa 2018-10-24 stsp lines = size.ws_row;
703 25791caa 2018-10-24 stsp resize_term(lines, cols);
706 2b49a8ae 2019-06-22 stsp static const struct got_error *
707 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
709 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
710 2b49a8ae 2019-06-22 stsp char pattern[1024];
713 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
714 2b49a8ae 2019-06-22 stsp return NULL;
716 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
717 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
719 2b49a8ae 2019-06-22 stsp nocbreak();
721 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
724 2b49a8ae 2019-06-22 stsp if (ret == ERR)
725 2b49a8ae 2019-06-22 stsp return NULL;
727 2b49a8ae 2019-06-22 stsp if (view->searching) {
728 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
729 2b49a8ae 2019-06-22 stsp view->searching = 0;
732 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
733 7c32bd05 2019-06-22 stsp err = view->search_start(view);
735 7c32bd05 2019-06-22 stsp regfree(&view->regex);
736 7c32bd05 2019-06-22 stsp return err;
738 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
739 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
740 2b49a8ae 2019-06-22 stsp view->search_next(view);
743 2b49a8ae 2019-06-22 stsp return NULL;
746 0cf4efb1 2018-09-29 stsp static const struct got_error *
747 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
748 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
749 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
751 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
752 669b5ffa 2018-10-07 stsp struct tog_view *v;
753 1a76625f 2018-10-22 stsp int ch, errcode;
755 e5a0f69f 2018-08-18 stsp *new = NULL;
756 e5a0f69f 2018-08-18 stsp *dead = NULL;
757 0cf4efb1 2018-09-29 stsp *focus = NULL;
759 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
760 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
761 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
762 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
764 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
765 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
766 82954512 2020-02-03 stsp if (errcode)
767 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
768 82954512 2020-02-03 stsp "pthread_mutex_unlock");
769 82954512 2020-02-03 stsp pthread_yield();
770 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
771 82954512 2020-02-03 stsp if (errcode)
772 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
773 82954512 2020-02-03 stsp "pthread_mutex_lock");
774 60493ae3 2019-06-20 stsp view->search_next(view);
775 60493ae3 2019-06-20 stsp return NULL;
778 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
779 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
780 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
781 1a76625f 2018-10-22 stsp if (errcode)
782 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
783 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
784 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
785 1a76625f 2018-10-22 stsp if (errcode)
786 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
787 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
789 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
790 25791caa 2018-10-24 stsp tog_resizeterm();
791 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
792 61266923 2020-01-14 stsp tog_sigcont_received = 0;
793 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
794 25791caa 2018-10-24 stsp err = view_resize(v);
796 25791caa 2018-10-24 stsp return err;
797 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
799 25791caa 2018-10-24 stsp return err;
803 e5a0f69f 2018-08-18 stsp switch (ch) {
807 1e37a5c2 2019-05-12 jcs if (view->child) {
808 1e37a5c2 2019-05-12 jcs *focus = view->child;
809 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
810 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
811 1e37a5c2 2019-05-12 jcs *focus = view->parent;
812 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
816 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
817 1e37a5c2 2019-05-12 jcs *dead = view;
823 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
824 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
826 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
827 669b5ffa 2018-10-07 stsp *focus = view->child;
828 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
829 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
831 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
834 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
835 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
837 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
838 1e37a5c2 2019-05-12 jcs *focus = view;
839 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
840 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
842 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
846 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
847 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
850 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
853 60493ae3 2019-06-20 stsp if (view->search_start)
854 2b49a8ae 2019-06-22 stsp view_search_start(view);
856 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
860 8f4ed634 2020-03-26 stsp if (view->search_next) {
861 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
862 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
863 60493ae3 2019-06-20 stsp view->search_next_done = 0;
864 60493ae3 2019-06-20 stsp view->search_next(view);
866 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
869 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
873 e5a0f69f 2018-08-18 stsp return err;
877 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
879 0cf4efb1 2018-09-29 stsp PANEL *panel;
880 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
882 669b5ffa 2018-10-07 stsp if (view->parent)
883 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
885 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
886 0cf4efb1 2018-09-29 stsp if (panel == NULL)
889 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
890 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
891 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
895 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
897 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
898 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
900 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
902 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
905 669b5ffa 2018-10-07 stsp return view->focussed;
908 bcbd79e2 2018-08-19 stsp static const struct got_error *
909 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
911 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
912 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
913 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
914 fd823528 2018-10-22 stsp int fast_refresh = 10;
915 1a76625f 2018-10-22 stsp int done = 0, errcode;
917 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
918 1a76625f 2018-10-22 stsp if (errcode)
919 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
921 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
922 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
924 a81bf10d 2018-09-29 stsp main_view = view;
925 1004088d 2018-09-29 stsp view->focussed = 1;
926 878940b7 2018-09-29 stsp err = view->show(view);
928 0cf4efb1 2018-09-29 stsp return err;
929 0cf4efb1 2018-09-29 stsp update_panels();
930 0cf4efb1 2018-09-29 stsp doupdate();
931 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
932 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
933 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
934 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
936 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
937 e4197bf9 2018-08-18 stsp view, &views);
940 e5a0f69f 2018-08-18 stsp if (dead_view) {
941 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
943 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
944 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
945 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
946 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
947 669b5ffa 2018-10-07 stsp prev = view->parent;
949 669b5ffa 2018-10-07 stsp if (dead_view->parent)
950 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
952 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
954 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
955 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
958 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
959 0cf4efb1 2018-09-29 stsp if (focus_view)
960 0cf4efb1 2018-09-29 stsp view = focus_view;
961 669b5ffa 2018-10-07 stsp else if (prev)
962 669b5ffa 2018-10-07 stsp view = prev;
963 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
964 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
965 0cf4efb1 2018-09-29 stsp tog_view_list_head);
967 0cf4efb1 2018-09-29 stsp view = NULL;
968 669b5ffa 2018-10-07 stsp if (view) {
969 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
970 669b5ffa 2018-10-07 stsp focus_view = view->child;
972 669b5ffa 2018-10-07 stsp focus_view = view;
976 bcbd79e2 2018-08-19 stsp if (new_view) {
977 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
978 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
979 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
980 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
982 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
983 86c66b02 2018-10-18 stsp err = view_close(v);
988 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
989 fed7eaa8 2018-10-24 stsp view = new_view;
990 878940b7 2018-09-29 stsp if (focus_view == NULL)
991 878940b7 2018-09-29 stsp focus_view = new_view;
993 0cf4efb1 2018-09-29 stsp if (focus_view) {
994 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
996 0cf4efb1 2018-09-29 stsp view->focussed = 0;
997 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
998 0cf4efb1 2018-09-29 stsp view = focus_view;
999 878940b7 2018-09-29 stsp if (new_view)
1000 878940b7 2018-09-29 stsp show_panel(new_view->panel);
1001 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
1002 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
1004 669b5ffa 2018-10-07 stsp if (view) {
1005 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
1006 758194b5 2018-10-24 stsp view->focussed = 1;
1007 758194b5 2018-10-24 stsp show_panel(view->panel);
1008 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
1009 758194b5 2018-10-24 stsp show_panel(view->child->panel);
1010 1a76625f 2018-10-22 stsp focus_view = view;
1012 669b5ffa 2018-10-07 stsp if (view->parent) {
1013 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1015 1a76625f 2018-10-22 stsp goto done;
1017 669b5ffa 2018-10-07 stsp err = view->show(view);
1019 1a76625f 2018-10-22 stsp goto done;
1020 669b5ffa 2018-10-07 stsp if (view->child) {
1021 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1023 1a76625f 2018-10-22 stsp goto done;
1025 1a76625f 2018-10-22 stsp update_panels();
1026 1a76625f 2018-10-22 stsp doupdate();
1030 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1031 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1032 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1033 e5a0f69f 2018-08-18 stsp view_close(view);
1036 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1037 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1038 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1040 e5a0f69f 2018-08-18 stsp return err;
1043 4ed7e80c 2018-05-20 stsp __dead static void
1044 9f7d7167 2018-04-29 stsp usage_log(void)
1047 c70c5802 2018-08-01 stsp fprintf(stderr,
1048 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1049 9f7d7167 2018-04-29 stsp getprogname());
1053 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1054 80ddbec8 2018-04-29 stsp static const struct got_error *
1055 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1057 00dfcb92 2018-06-11 stsp char *vis = NULL;
1058 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1060 963b370f 2018-05-20 stsp *ws = NULL;
1061 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1062 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1063 00dfcb92 2018-06-11 stsp int vislen;
1064 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1065 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1067 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1068 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1070 00dfcb92 2018-06-11 stsp return err;
1071 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1072 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1073 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1074 a7f50699 2018-06-11 stsp goto done;
1078 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1079 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1080 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1081 a7f50699 2018-06-11 stsp goto done;
1084 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1085 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1087 00dfcb92 2018-06-11 stsp free(vis);
1088 963b370f 2018-05-20 stsp if (err) {
1089 963b370f 2018-05-20 stsp free(*ws);
1090 963b370f 2018-05-20 stsp *ws = NULL;
1091 963b370f 2018-05-20 stsp *wlen = 0;
1093 963b370f 2018-05-20 stsp return err;
1096 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1097 963b370f 2018-05-20 stsp static const struct got_error *
1098 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1099 27a741e5 2019-09-11 stsp int col_tab_align)
1101 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1102 963b370f 2018-05-20 stsp int cols = 0;
1103 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1104 963b370f 2018-05-20 stsp size_t wlen;
1107 963b370f 2018-05-20 stsp *wlinep = NULL;
1108 b700b5d6 2018-07-10 stsp *widthp = 0;
1110 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1112 963b370f 2018-05-20 stsp return err;
1115 27a741e5 2019-09-11 stsp while (i < wlen) {
1116 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1118 27a741e5 2019-09-11 stsp if (width == 0) {
1123 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1124 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1126 27a741e5 2019-09-11 stsp cols += width;
1128 27a741e5 2019-09-11 stsp } else if (width == -1) {
1129 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1130 27a741e5 2019-09-11 stsp width = TABSIZE -
1131 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1132 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1134 27a741e5 2019-09-11 stsp cols += width;
1138 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1139 963b370f 2018-05-20 stsp goto done;
1142 963b370f 2018-05-20 stsp wline[i] = L'\0';
1143 b700b5d6 2018-07-10 stsp if (widthp)
1144 b700b5d6 2018-07-10 stsp *widthp = cols;
1147 963b370f 2018-05-20 stsp free(wline);
1149 963b370f 2018-05-20 stsp *wlinep = wline;
1150 963b370f 2018-05-20 stsp return err;
1153 8b473291 2019-02-21 stsp static const struct got_error*
1154 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1155 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1157 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1158 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1160 8b473291 2019-02-21 stsp const char *name;
1162 8b473291 2019-02-21 stsp *refs_str = NULL;
1164 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1165 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1166 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1169 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1170 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1172 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1173 8b473291 2019-02-21 stsp name += 5;
1174 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1176 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1177 8b473291 2019-02-21 stsp name += 6;
1178 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1179 8b473291 2019-02-21 stsp name += 8;
1180 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1181 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1184 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1187 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1188 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1189 5d844a1e 2019-08-13 stsp if (err) {
1190 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1191 48cae60d 2020-09-22 stsp free(ref_id);
1194 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1195 5d844a1e 2019-08-13 stsp err = NULL;
1196 5d844a1e 2019-08-13 stsp tag = NULL;
1199 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1200 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1201 48cae60d 2020-09-22 stsp free(ref_id);
1203 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1204 52b5abe1 2019-08-13 stsp if (cmp != 0)
1206 8b473291 2019-02-21 stsp s = *refs_str;
1207 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1208 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1209 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1211 8b473291 2019-02-21 stsp *refs_str = NULL;
1217 8b473291 2019-02-21 stsp return err;
1220 963b370f 2018-05-20 stsp static const struct got_error *
1221 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1222 27a741e5 2019-09-11 stsp int col_tab_align)
1224 5813d178 2019-03-09 stsp char *smallerthan, *at;
1226 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1227 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1228 5813d178 2019-03-09 stsp author = smallerthan + 1;
1229 5813d178 2019-03-09 stsp at = strchr(author, '@');
1231 5813d178 2019-03-09 stsp *at = '\0';
1232 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1235 5813d178 2019-03-09 stsp static const struct got_error *
1236 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1237 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1238 11b20872 2019-11-08 stsp const size_t date_display_cols, int author_display_cols,
1239 11b20872 2019-11-08 stsp struct tog_colors *colors)
1241 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1242 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1243 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1244 5813d178 2019-03-09 stsp char *author = NULL;
1245 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1246 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1247 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1248 bb737323 2018-05-20 stsp int col, limit;
1249 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1250 ccb26ccd 2018-11-05 stsp struct tm tm;
1251 45d799e2 2018-12-23 stsp time_t committer_time;
1252 11b20872 2019-11-08 stsp struct tog_color *tc;
1254 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1255 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1256 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1257 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1258 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1259 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1261 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1262 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1264 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1265 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_DATE);
1267 11b20872 2019-11-08 stsp wattr_on(view->window,
1268 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1269 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1271 11b20872 2019-11-08 stsp wattr_off(view->window,
1272 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1273 27a741e5 2019-09-11 stsp col = limit;
1274 b39d25c7 2018-07-10 stsp if (col > avail)
1275 b39d25c7 2018-07-10 stsp goto done;
1277 6570a66d 2019-11-08 stsp if (avail >= 120) {
1278 6570a66d 2019-11-08 stsp char *id_str;
1279 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1281 6570a66d 2019-11-08 stsp goto done;
1282 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1284 11b20872 2019-11-08 stsp wattr_on(view->window,
1285 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1286 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1288 11b20872 2019-11-08 stsp wattr_off(view->window,
1289 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1290 6570a66d 2019-11-08 stsp free(id_str);
1292 6570a66d 2019-11-08 stsp if (col > avail)
1293 6570a66d 2019-11-08 stsp goto done;
1296 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1297 5813d178 2019-03-09 stsp if (author == NULL) {
1298 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1299 80ddbec8 2018-04-29 stsp goto done;
1301 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1303 bb737323 2018-05-20 stsp goto done;
1304 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_AUTHOR);
1306 11b20872 2019-11-08 stsp wattr_on(view->window,
1307 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1308 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1310 11b20872 2019-11-08 stsp wattr_off(view->window,
1311 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1312 bb737323 2018-05-20 stsp col += author_width;
1313 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1314 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1316 bb737323 2018-05-20 stsp author_width++;
1318 9c2eaf34 2018-05-20 stsp if (col > avail)
1319 9c2eaf34 2018-05-20 stsp goto done;
1321 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1323 6d9fbc00 2018-04-29 stsp goto done;
1324 bb737323 2018-05-20 stsp logmsg = logmsg0;
1325 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1327 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1328 bb737323 2018-05-20 stsp if (newline)
1329 bb737323 2018-05-20 stsp *newline = '\0';
1330 bb737323 2018-05-20 stsp limit = avail - col;
1331 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1333 bb737323 2018-05-20 stsp goto done;
1334 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1335 bb737323 2018-05-20 stsp col += logmsg_width;
1336 27a741e5 2019-09-11 stsp while (col < avail) {
1337 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1341 80ddbec8 2018-04-29 stsp free(logmsg0);
1342 bb737323 2018-05-20 stsp free(wlogmsg);
1343 5813d178 2019-03-09 stsp free(author);
1344 bb737323 2018-05-20 stsp free(wauthor);
1345 80ddbec8 2018-04-29 stsp free(line);
1346 80ddbec8 2018-04-29 stsp return err;
1349 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1350 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1351 899d86c2 2018-05-10 stsp struct got_object_id *id)
1353 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1355 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1356 80ddbec8 2018-04-29 stsp if (entry == NULL)
1357 899d86c2 2018-05-10 stsp return NULL;
1359 899d86c2 2018-05-10 stsp entry->id = id;
1360 99db9666 2018-05-07 stsp entry->commit = commit;
1361 899d86c2 2018-05-10 stsp return entry;
1364 99db9666 2018-05-07 stsp static void
1365 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1367 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1369 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1370 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1371 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1372 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1373 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1374 99db9666 2018-05-07 stsp free(entry);
1377 99db9666 2018-05-07 stsp static void
1378 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1380 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1381 99db9666 2018-05-07 stsp pop_commit(commits);
1384 c4972b91 2018-05-07 stsp static const struct got_error *
1385 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1386 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1388 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1389 13add988 2019-10-15 stsp regmatch_t regmatch;
1390 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1392 13add988 2019-10-15 stsp *have_match = 0;
1394 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1396 13add988 2019-10-15 stsp return err;
1398 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1400 13add988 2019-10-15 stsp goto done;
1402 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1403 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1404 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1405 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1406 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1407 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1408 13add988 2019-10-15 stsp *have_match = 1;
1410 13add988 2019-10-15 stsp free(id_str);
1411 13add988 2019-10-15 stsp free(logmsg);
1412 13add988 2019-10-15 stsp return err;
1415 13add988 2019-10-15 stsp static const struct got_error *
1416 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1417 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1418 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1420 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1421 7c1452c1 2020-03-26 stsp int nqueued = 0;
1424 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1425 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1426 1a76625f 2018-10-22 stsp * while updating the display.
1428 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1429 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1430 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1431 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1432 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1433 1a76625f 2018-10-22 stsp int errcode;
1435 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1436 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1439 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1442 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1443 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1444 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1448 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1449 1a76625f 2018-10-22 stsp if (errcode) {
1450 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1451 13add988 2019-10-15 stsp "pthread_mutex_lock");
1455 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1456 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1457 ecb28ae0 2018-07-16 stsp nqueued++;
1458 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1460 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1461 7c1452c1 2020-03-26 stsp int have_match;
1462 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1465 7c1452c1 2020-03-26 stsp if (have_match)
1466 8f4ed634 2020-03-26 stsp *search_next_done = TOG_SEARCH_HAVE_MORE;
1469 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1470 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1471 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1472 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1477 9ba79e04 2018-06-11 stsp return err;
1480 0553a4e3 2018-04-30 stsp static const struct got_error *
1481 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1482 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1483 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1484 11b20872 2019-11-08 stsp struct got_reflist_head *refs, const char *path, int commits_needed,
1485 11b20872 2019-11-08 stsp struct tog_colors *colors)
1487 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1488 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1489 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1490 60493ae3 2019-06-20 stsp int width;
1491 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1492 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1493 8b473291 2019-02-21 stsp char *refs_str = NULL;
1494 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1495 11b20872 2019-11-08 stsp struct tog_color *tc;
1496 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1498 e0d42f60 2018-07-22 stsp entry = first;
1499 e0d42f60 2018-07-22 stsp ncommits = 0;
1500 e0d42f60 2018-07-22 stsp while (entry) {
1501 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1502 e0d42f60 2018-07-22 stsp *selected = entry;
1505 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1506 e0d42f60 2018-07-22 stsp ncommits++;
1509 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1510 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1512 ecb28ae0 2018-07-16 stsp return err;
1513 8b473291 2019-02-21 stsp if (refs) {
1514 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1517 8b473291 2019-02-21 stsp goto done;
1521 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1522 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1524 8f4ed634 2020-03-26 stsp if (commits_needed > 0) {
1525 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1526 8f4ed634 2020-03-26 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1527 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1528 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1529 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1530 8f4ed634 2020-03-26 stsp goto done;
1533 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1535 f9686aa5 2020-03-27 stsp if (view->searching) {
1536 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1537 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1538 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1539 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1540 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1541 f9686aa5 2020-03-27 stsp search_str = "searching...";
1544 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1545 8f4ed634 2020-03-26 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1546 f9686aa5 2020-03-27 stsp search_str ? search_str :
1547 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1548 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1549 8f4ed634 2020-03-26 stsp goto done;
1553 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1554 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1555 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1556 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1557 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1558 1a76625f 2018-10-22 stsp header = NULL;
1559 1a76625f 2018-10-22 stsp goto done;
1561 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1562 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1563 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1564 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1565 1a76625f 2018-10-22 stsp header = NULL;
1566 1a76625f 2018-10-22 stsp goto done;
1568 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1570 1a76625f 2018-10-22 stsp goto done;
1572 2814baeb 2018-08-01 stsp werase(view->window);
1574 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1575 a3404814 2018-09-02 stsp wstandout(view->window);
1576 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1578 11b20872 2019-11-08 stsp wattr_on(view->window,
1579 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1580 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1582 11b20872 2019-11-08 stsp wattr_off(view->window,
1583 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1584 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1585 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1588 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1589 a3404814 2018-09-02 stsp wstandend(view->window);
1590 ecb28ae0 2018-07-16 stsp free(wline);
1591 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1592 1a76625f 2018-10-22 stsp goto done;
1594 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1595 899d86c2 2018-05-10 stsp entry = first;
1596 5813d178 2019-03-09 stsp ncommits = 0;
1597 5813d178 2019-03-09 stsp while (entry) {
1598 5813d178 2019-03-09 stsp char *author;
1599 5813d178 2019-03-09 stsp wchar_t *wauthor;
1600 5813d178 2019-03-09 stsp int width;
1601 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1603 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1604 5813d178 2019-03-09 stsp if (author == NULL) {
1605 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1606 5813d178 2019-03-09 stsp goto done;
1608 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1609 27a741e5 2019-09-11 stsp date_display_cols);
1610 5813d178 2019-03-09 stsp if (author_cols < width)
1611 5813d178 2019-03-09 stsp author_cols = width;
1612 5813d178 2019-03-09 stsp free(wauthor);
1613 5813d178 2019-03-09 stsp free(author);
1614 7ca04879 2019-10-19 stsp ncommits++;
1615 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1618 5813d178 2019-03-09 stsp entry = first;
1619 899d86c2 2018-05-10 stsp *last = first;
1620 867c6645 2018-07-10 stsp ncommits = 0;
1621 899d86c2 2018-05-10 stsp while (entry) {
1622 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1624 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1625 2814baeb 2018-08-01 stsp wstandout(view->window);
1626 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1627 11b20872 2019-11-08 stsp date_display_cols, author_cols, colors);
1628 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1629 2814baeb 2018-08-01 stsp wstandend(view->window);
1631 60493ae3 2019-06-20 stsp goto done;
1632 0553a4e3 2018-04-30 stsp ncommits++;
1633 899d86c2 2018-05-10 stsp *last = entry;
1634 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1637 1a57306a 2018-09-02 stsp view_vborder(view);
1639 1a76625f 2018-10-22 stsp free(id_str);
1640 8b473291 2019-02-21 stsp free(refs_str);
1641 1a76625f 2018-10-22 stsp free(ncommits_str);
1642 1a76625f 2018-10-22 stsp free(header);
1643 80ddbec8 2018-04-29 stsp return err;
1646 07b55e75 2018-05-10 stsp static void
1647 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1648 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1649 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1651 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1652 07b55e75 2018-05-10 stsp int nscrolled = 0;
1654 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1655 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1658 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1659 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1660 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1661 07b55e75 2018-05-10 stsp if (entry) {
1662 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1663 07b55e75 2018-05-10 stsp nscrolled++;
1668 aa075928 2018-05-10 stsp static const struct got_error *
1669 7c1452c1 2020-03-26 stsp trigger_log_thread(struct tog_view *log_view, int wait,
1670 7c1452c1 2020-03-26 stsp int *commits_needed, int *log_complete,
1671 7c1452c1 2020-03-26 stsp pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1673 5e224a3e 2019-02-22 stsp int errcode;
1675 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1677 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1678 5e224a3e 2019-02-22 stsp if (*log_complete)
1681 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1682 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1683 7aafa0d1 2019-02-22 stsp if (errcode)
1684 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1685 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1688 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1689 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1691 7c1452c1 2020-03-26 stsp if (!wait)
1694 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1695 7c1452c1 2020-03-26 stsp show_log_view(log_view);
1696 7c1452c1 2020-03-26 stsp update_panels();
1697 7c1452c1 2020-03-26 stsp doupdate();
1699 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1700 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1701 82954512 2020-02-03 stsp if (errcode)
1702 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1703 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1705 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1706 7c1452c1 2020-03-26 stsp show_log_view(log_view);
1707 7c1452c1 2020-03-26 stsp update_panels();
1708 7c1452c1 2020-03-26 stsp doupdate();
1711 5e224a3e 2019-02-22 stsp return NULL;
1714 5e224a3e 2019-02-22 stsp static const struct got_error *
1715 7c1452c1 2020-03-26 stsp scroll_down(struct tog_view *log_view,
1716 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1717 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1718 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1719 7c1452c1 2020-03-26 stsp pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1721 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1722 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1723 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1725 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1726 5e224a3e 2019-02-22 stsp return NULL;
1728 7c1452c1 2020-03-26 stsp ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1729 7c1452c1 2020-03-26 stsp if (commits->ncommits < ncommits_needed && !*log_complete) {
1731 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1733 7c1452c1 2020-03-26 stsp (*commits_needed) += maxscroll;
1734 7c1452c1 2020-03-26 stsp err = trigger_log_thread(log_view, 1, commits_needed,
1735 7c1452c1 2020-03-26 stsp log_complete, need_commits, commit_loaded);
1737 5e224a3e 2019-02-22 stsp return err;
1741 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1742 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1745 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1747 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1748 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1750 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1751 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1753 dd0a52c1 2018-05-20 stsp return err;
1756 cd0acaa7 2018-05-20 stsp static const struct got_error *
1757 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1758 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1759 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1760 8b473291 2019-02-21 stsp struct got_repository *repo)
1762 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1763 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1764 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1766 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1767 15a94983 2018-12-23 stsp if (diff_view == NULL)
1768 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1770 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1771 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1772 64453f7e 2020-11-21 stsp commit_id, 0, log_view, refs, repo);
1773 e5a0f69f 2018-08-18 stsp if (err == NULL)
1774 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1775 cd0acaa7 2018-05-20 stsp return err;
1778 80ddbec8 2018-04-29 stsp static const struct got_error *
1779 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1780 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1782 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1784 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1785 941e9f74 2019-05-21 stsp if (parent == NULL)
1786 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1788 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1789 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1790 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1791 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1792 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1793 941e9f74 2019-05-21 stsp s->tree = subtree;
1794 941e9f74 2019-05-21 stsp s->selected = 0;
1795 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1796 941e9f74 2019-05-21 stsp return NULL;
1799 941e9f74 2019-05-21 stsp static const struct got_error *
1800 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1801 55cccc34 2020-02-20 stsp struct got_object_id *commit_id,
1802 55cccc34 2020-02-20 stsp const char *path, struct got_repository *repo)
1804 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1805 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1806 941e9f74 2019-05-21 stsp const char *p;
1807 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1809 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1811 941e9f74 2019-05-21 stsp while (*p) {
1812 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1813 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1814 56e0773d 2019-11-28 stsp char *te_name;
1816 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1819 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1820 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1821 941e9f74 2019-05-21 stsp if (slash == NULL)
1822 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1824 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1825 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1826 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1829 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1830 56e0773d 2019-11-28 stsp if (te == NULL) {
1831 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1832 56e0773d 2019-11-28 stsp free(te_name);
1835 56e0773d 2019-11-28 stsp free(te_name);
1836 56e0773d 2019-11-28 stsp s->selected_entry = te;
1837 56e0773d 2019-11-28 stsp s->selected = got_tree_entry_get_index(te);
1838 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1839 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1841 56e0773d 2019-11-28 stsp if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1842 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1843 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1844 67409a31 2019-05-24 stsp s->selected = 0;
1848 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1849 941e9f74 2019-05-21 stsp if (slash)
1850 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1852 941e9f74 2019-05-21 stsp subpath = strdup(path);
1853 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1854 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1858 55cccc34 2020-02-20 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
1863 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1864 941e9f74 2019-05-21 stsp free(tree_id);
1868 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1869 941e9f74 2019-05-21 stsp if (err) {
1870 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1873 941e9f74 2019-05-21 stsp if (slash == NULL)
1875 941e9f74 2019-05-21 stsp free(subpath);
1876 941e9f74 2019-05-21 stsp subpath = NULL;
1877 941e9f74 2019-05-21 stsp p = slash;
1880 941e9f74 2019-05-21 stsp free(subpath);
1881 1a76625f 2018-10-22 stsp return err;
1884 61266923 2020-01-14 stsp static const struct got_error *
1885 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1886 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1887 55cccc34 2020-02-20 stsp struct got_reflist_head *refs, struct got_repository *repo)
1889 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1890 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1891 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1892 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1894 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1895 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1897 55cccc34 2020-02-20 stsp return err;
1899 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1900 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1901 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1903 55cccc34 2020-02-20 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1904 55cccc34 2020-02-20 stsp if (err) {
1905 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1906 55cccc34 2020-02-20 stsp return err;
1908 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1910 55cccc34 2020-02-20 stsp *new_view = tree_view;
1912 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1913 55cccc34 2020-02-20 stsp return NULL;
1915 55cccc34 2020-02-20 stsp return tree_view_walk_path(s, entry->id, path, repo);
1918 55cccc34 2020-02-20 stsp static const struct got_error *
1919 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1921 61266923 2020-01-14 stsp sigset_t sigset;
1922 61266923 2020-01-14 stsp int errcode;
1924 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1925 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1927 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1928 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1929 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1930 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1931 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1933 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1934 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1935 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1937 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1938 61266923 2020-01-14 stsp if (errcode)
1939 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1941 61266923 2020-01-14 stsp return NULL;
1944 1a76625f 2018-10-22 stsp static void *
1945 1a76625f 2018-10-22 stsp log_thread(void *arg)
1947 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1948 1a76625f 2018-10-22 stsp int errcode = 0;
1949 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1950 1a76625f 2018-10-22 stsp int done = 0;
1952 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1954 61266923 2020-01-14 stsp return (void *)err;
1956 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1957 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1958 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1959 13add988 2019-10-15 stsp a->regex);
1960 1a76625f 2018-10-22 stsp if (err) {
1961 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1962 1a76625f 2018-10-22 stsp return (void *)err;
1963 1a76625f 2018-10-22 stsp err = NULL;
1965 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1966 1a76625f 2018-10-22 stsp a->commits_needed--;
1968 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1969 3abe8080 2019-04-10 stsp if (errcode) {
1970 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1971 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1973 3abe8080 2019-04-10 stsp } else if (*a->quit)
1975 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1976 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1977 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1978 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1981 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
1982 7c1452c1 2020-03-26 stsp if (errcode) {
1983 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
1984 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
1985 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
1990 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1992 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
1993 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
1994 7c1452c1 2020-03-26 stsp &tog_mutex);
1995 7c1452c1 2020-03-26 stsp if (errcode)
1996 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
1997 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2001 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2002 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2003 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2004 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2006 3abe8080 2019-04-10 stsp a->log_complete = 1;
2007 1a76625f 2018-10-22 stsp return (void *)err;
2010 1a76625f 2018-10-22 stsp static const struct got_error *
2011 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2013 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2014 1a76625f 2018-10-22 stsp int errcode;
2016 1a76625f 2018-10-22 stsp if (s->thread) {
2017 1a76625f 2018-10-22 stsp s->quit = 1;
2018 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2019 1a76625f 2018-10-22 stsp if (errcode)
2020 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2021 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2022 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2023 1a76625f 2018-10-22 stsp if (errcode)
2024 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2025 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2026 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2027 1a76625f 2018-10-22 stsp if (errcode)
2028 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2029 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2030 1a76625f 2018-10-22 stsp if (errcode)
2031 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2032 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2033 1a76625f 2018-10-22 stsp s->thread = NULL;
2036 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2037 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2038 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2041 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2042 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2043 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2046 9343a5fb 2018-06-23 stsp return err;
2049 9343a5fb 2018-06-23 stsp static const struct got_error *
2050 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2052 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2053 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2054 276b94a1 2020-11-13 naddy int errcode;
2056 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2058 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2059 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2060 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2062 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2063 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2064 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2066 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2067 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2068 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2069 1a76625f 2018-10-22 stsp free(s->start_id);
2070 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2071 1a76625f 2018-10-22 stsp return err;
2074 1a76625f 2018-10-22 stsp static const struct got_error *
2075 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2077 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2079 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2080 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2081 60493ae3 2019-06-20 stsp return NULL;
2084 60493ae3 2019-06-20 stsp static const struct got_error *
2085 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2087 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2088 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2089 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2091 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2092 f9686aa5 2020-03-27 stsp show_log_view(view);
2093 f9686aa5 2020-03-27 stsp update_panels();
2094 f9686aa5 2020-03-27 stsp doupdate();
2096 96e2b566 2019-07-08 stsp if (s->search_entry) {
2097 13add988 2019-10-15 stsp int errcode, ch;
2098 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2099 13add988 2019-10-15 stsp if (errcode)
2100 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2101 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2102 13add988 2019-10-15 stsp ch = wgetch(view->window);
2103 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2104 13add988 2019-10-15 stsp if (errcode)
2105 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2106 13add988 2019-10-15 stsp "pthread_mutex_lock");
2107 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2108 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2109 678cbce5 2019-07-28 stsp return NULL;
2111 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2112 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2114 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2115 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2116 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2117 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2118 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2120 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2121 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2123 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2124 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2126 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2129 60493ae3 2019-06-20 stsp while (1) {
2130 13add988 2019-10-15 stsp int have_match = 0;
2132 60493ae3 2019-06-20 stsp if (entry == NULL) {
2133 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2134 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2135 f9967bca 2020-03-27 stsp view->search_next_done =
2136 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2137 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2138 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2139 f801134a 2019-06-25 stsp return NULL;
2142 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2143 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2144 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2146 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2147 7c1452c1 2020-03-26 stsp return trigger_log_thread(view, 0,
2148 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
2149 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
2150 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2151 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2154 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2155 13add988 2019-10-15 stsp &view->regex);
2158 13add988 2019-10-15 stsp if (have_match) {
2159 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2160 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2164 96e2b566 2019-07-08 stsp s->search_entry = entry;
2165 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2166 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2168 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2171 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2172 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2173 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2174 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2176 60493ae3 2019-06-20 stsp return err;
2179 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2180 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2182 60493ae3 2019-06-20 stsp return err;
2187 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2189 60493ae3 2019-06-20 stsp return NULL;
2192 60493ae3 2019-06-20 stsp static const struct got_error *
2193 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2194 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
2195 f135c941 2020-02-20 stsp const char *head_ref_name, const char *in_repo_path,
2196 b672a97a 2020-01-27 stsp int log_branches)
2198 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2199 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2200 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2201 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2202 1a76625f 2018-10-22 stsp int errcode;
2204 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2205 f135c941 2020-02-20 stsp free(s->in_repo_path);
2206 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2207 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2208 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2211 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2212 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2213 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2215 8b473291 2019-02-21 stsp s->refs = refs;
2216 fb2756b9 2018-08-04 stsp s->repo = repo;
2217 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2218 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2219 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2220 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2221 5036bf37 2018-09-24 stsp goto done;
2223 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2225 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2226 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2227 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2228 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2230 11b20872 2019-11-08 stsp goto done;
2231 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2232 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2233 11b20872 2019-11-08 stsp if (err) {
2234 11b20872 2019-11-08 stsp free_colors(&s->colors);
2235 11b20872 2019-11-08 stsp goto done;
2237 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2238 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2239 11b20872 2019-11-08 stsp if (err) {
2240 11b20872 2019-11-08 stsp free_colors(&s->colors);
2241 11b20872 2019-11-08 stsp goto done;
2245 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2246 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2247 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2248 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2249 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2251 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2253 1a76625f 2018-10-22 stsp goto done;
2254 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2255 b672a97a 2020-01-27 stsp !s->log_branches);
2257 1a76625f 2018-10-22 stsp goto done;
2258 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2259 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2261 c5b78334 2020-01-12 stsp goto done;
2263 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2264 1a76625f 2018-10-22 stsp if (errcode) {
2265 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2266 1a76625f 2018-10-22 stsp goto done;
2268 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2269 7c1452c1 2020-03-26 stsp if (errcode) {
2270 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2271 7c1452c1 2020-03-26 stsp goto done;
2274 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2275 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2276 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2277 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2278 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2279 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2280 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2281 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2282 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2283 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2284 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2285 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2286 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2289 1a76625f 2018-10-22 stsp close_log_view(view);
2290 ba4f502b 2018-08-04 stsp return err;
2293 e5a0f69f 2018-08-18 stsp static const struct got_error *
2294 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2296 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2298 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2299 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2300 2b380cc8 2018-10-24 stsp &s->thread_args);
2301 2b380cc8 2018-10-24 stsp if (errcode)
2302 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2305 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2306 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2307 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2308 11b20872 2019-11-08 stsp s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2311 e5a0f69f 2018-08-18 stsp static const struct got_error *
2312 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2313 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2315 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2316 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2317 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2318 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2319 669b5ffa 2018-10-07 stsp int begin_x = 0;
2320 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2322 e5a0f69f 2018-08-18 stsp switch (ch) {
2324 1e37a5c2 2019-05-12 jcs s->quit = 1;
2327 1e37a5c2 2019-05-12 jcs case KEY_UP:
2330 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2332 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2333 1e37a5c2 2019-05-12 jcs s->selected--;
2335 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2336 1144d21a 2019-06-21 stsp &s->commits);
2338 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2339 a4292ac5 2019-05-12 jcs case CTRL('b'):
2340 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2342 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2343 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2344 1e37a5c2 2019-05-12 jcs s->selected = 0;
2347 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2348 1831ac02 2020-03-23 naddy view->nlines - 1, &s->commits);
2351 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2354 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2356 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2357 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2358 1e37a5c2 2019-05-12 jcs s->selected++;
2361 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2362 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2363 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2364 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2365 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2366 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2368 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2369 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2370 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2371 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2372 1e37a5c2 2019-05-12 jcs if (first == NULL)
2374 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2375 1831ac02 2020-03-23 naddy view->nlines - 1, &s->last_displayed_entry,
2376 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2377 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2378 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2379 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2382 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2383 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2384 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2385 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2386 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2387 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2389 1e37a5c2 2019-05-12 jcs err = NULL;
2392 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2393 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2394 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2395 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2396 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2398 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2401 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2403 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2404 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2405 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2406 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2407 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2410 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2411 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2413 1e37a5c2 2019-05-12 jcs return err;
2414 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2416 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2419 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2420 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2422 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2425 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2427 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2428 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2429 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2430 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2433 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2434 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2436 1e37a5c2 2019-05-12 jcs return err;
2437 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2439 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2442 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2443 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2445 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2447 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2448 74cfe85e 2020-10-20 stsp if (got_path_cmp(s->in_repo_path, "/",
2449 74cfe85e 2020-10-20 stsp strlen(s->in_repo_path), 1) == 0)
2451 74cfe85e 2020-10-20 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2453 74cfe85e 2020-10-20 stsp return err;
2454 74cfe85e 2020-10-20 stsp err = stop_log_thread(s);
2455 74cfe85e 2020-10-20 stsp if (err) {
2456 74cfe85e 2020-10-20 stsp free(parent_path);
2457 74cfe85e 2020-10-20 stsp return err;
2459 74cfe85e 2020-10-20 stsp lv = view_open(view->nlines, view->ncols,
2460 74cfe85e 2020-10-20 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2461 74cfe85e 2020-10-20 stsp if (lv == NULL) {
2462 74cfe85e 2020-10-20 stsp free(parent_path);
2463 74cfe85e 2020-10-20 stsp return got_error_from_errno("view_open");
2465 74cfe85e 2020-10-20 stsp err = open_log_view(lv, s->start_id, s->refs,
2466 74cfe85e 2020-10-20 stsp s->repo, s->head_ref_name, parent_path,
2467 74cfe85e 2020-10-20 stsp s->log_branches);
2468 74cfe85e 2020-10-20 stsp free(parent_path);
2470 74cfe85e 2020-10-20 stsp return err;;
2471 74cfe85e 2020-10-20 stsp if (view_is_parent_view(view))
2472 74cfe85e 2020-10-20 stsp *new_view = lv;
2474 74cfe85e 2020-10-20 stsp view_set_child(view->parent, lv);
2475 74cfe85e 2020-10-20 stsp *focus_view = lv;
2478 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2479 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2481 d01904d4 2019-06-25 stsp return err;
2482 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2483 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2484 d01904d4 2019-06-25 stsp if (lv == NULL)
2485 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2486 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2487 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2488 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2489 ef129c5e 2019-08-03 stsp if (err) {
2490 ef129c5e 2019-08-03 stsp view_close(lv);
2491 d01904d4 2019-06-25 stsp return err;
2493 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2494 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2495 e6b23735 2019-10-04 stsp free(start_id);
2496 e6b23735 2019-10-04 stsp view_close(lv);
2497 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2499 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2500 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2501 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2502 e6b23735 2019-10-04 stsp if (err) {
2503 d01904d4 2019-06-25 stsp free(start_id);
2504 ef129c5e 2019-08-03 stsp view_close(lv);
2505 50284065 2019-10-04 stsp return err;
2507 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2508 f135c941 2020-02-20 stsp s->head_ref_name, in_repo_path, s->log_branches);
2509 ef129c5e 2019-08-03 stsp if (err) {
2510 ef129c5e 2019-08-03 stsp free(start_id);
2511 b672a97a 2020-01-27 stsp view_close(lv);
2512 b672a97a 2020-01-27 stsp return err;;
2514 b672a97a 2020-01-27 stsp *dead_view = view;
2515 b672a97a 2020-01-27 stsp *new_view = lv;
2518 b672a97a 2020-01-27 stsp s->log_branches = !s->log_branches;
2519 b672a97a 2020-01-27 stsp err = stop_log_thread(s);
2521 b672a97a 2020-01-27 stsp return err;
2522 b672a97a 2020-01-27 stsp lv = view_open(view->nlines, view->ncols,
2523 b672a97a 2020-01-27 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2524 b672a97a 2020-01-27 stsp if (lv == NULL)
2525 b672a97a 2020-01-27 stsp return got_error_from_errno("view_open");
2526 b672a97a 2020-01-27 stsp err = open_log_view(lv, s->start_id, s->refs, s->repo,
2527 f135c941 2020-02-20 stsp s->head_ref_name, s->in_repo_path, s->log_branches);
2528 b672a97a 2020-01-27 stsp if (err) {
2529 ef129c5e 2019-08-03 stsp view_close(lv);
2530 d01904d4 2019-06-25 stsp return err;;
2532 d01904d4 2019-06-25 stsp *dead_view = view;
2533 d01904d4 2019-06-25 stsp *new_view = lv;
2539 80ddbec8 2018-04-29 stsp return err;
2542 4ed7e80c 2018-05-20 stsp static const struct got_error *
2543 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2545 c2db6724 2019-01-04 stsp const struct got_error *error;
2547 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2548 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2549 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2551 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2552 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2554 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2555 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2557 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2558 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2560 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2561 c2db6724 2019-01-04 stsp if (error != NULL)
2562 c2db6724 2019-01-04 stsp return error;
2564 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2565 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2567 c2db6724 2019-01-04 stsp return NULL;
2570 a915003a 2019-02-05 stsp static void
2571 a915003a 2019-02-05 stsp init_curses(void)
2573 a915003a 2019-02-05 stsp initscr();
2575 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2578 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2579 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2580 a915003a 2019-02-05 stsp curs_set(0);
2581 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2582 6d17833f 2019-11-08 stsp start_color();
2583 6d17833f 2019-11-08 stsp use_default_colors();
2585 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2586 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2587 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2590 c2db6724 2019-01-04 stsp static const struct got_error *
2591 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2592 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2594 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2596 f135c941 2020-02-20 stsp if (argc == 0) {
2597 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2598 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2599 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2600 f135c941 2020-02-20 stsp return NULL;
2603 f135c941 2020-02-20 stsp if (worktree) {
2604 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2607 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2609 f135c941 2020-02-20 stsp return err;
2610 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2611 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2612 bfd61697 2020-11-14 stsp p) == -1) {
2613 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2614 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2618 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2620 f135c941 2020-02-20 stsp return err;
2623 f135c941 2020-02-20 stsp static const struct got_error *
2624 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2626 80ddbec8 2018-04-29 stsp const struct got_error *error;
2627 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2628 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2629 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2630 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2631 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2632 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2633 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2634 04cc582a 2018-08-01 stsp struct tog_view *view;
2636 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2638 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2639 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2640 c2db6724 2019-01-04 stsp NULL) == -1)
2641 80ddbec8 2018-04-29 stsp err(1, "pledge");
2644 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2645 80ddbec8 2018-04-29 stsp switch (ch) {
2647 b672a97a 2020-01-27 stsp log_branches = 1;
2650 80ddbec8 2018-04-29 stsp start_commit = optarg;
2653 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2654 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2655 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2659 17020d27 2019-03-07 stsp usage_log();
2660 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2664 80ddbec8 2018-04-29 stsp argc -= optind;
2665 80ddbec8 2018-04-29 stsp argv += optind;
2667 f135c941 2020-02-20 stsp if (argc > 1)
2668 f135c941 2020-02-20 stsp usage_log();
2670 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2671 6962eb72 2020-02-20 stsp if (cwd == NULL)
2672 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
2674 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2675 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2676 963f97a1 2019-03-18 stsp goto done;
2678 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2679 a1fbf39a 2019-08-11 stsp if (worktree)
2680 6962eb72 2020-02-20 stsp repo_path =
2681 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2683 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2685 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2686 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2687 963f97a1 2019-03-18 stsp goto done;
2690 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2691 80ddbec8 2018-04-29 stsp if (error != NULL)
2692 ecb28ae0 2018-07-16 stsp goto done;
2694 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2695 f135c941 2020-02-20 stsp repo, worktree);
2696 f135c941 2020-02-20 stsp if (error)
2697 f135c941 2020-02-20 stsp goto done;
2699 f135c941 2020-02-20 stsp init_curses();
2701 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2702 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2703 c02c541e 2019-03-29 stsp if (error)
2704 c02c541e 2019-03-29 stsp goto done;
2706 15a94983 2018-12-23 stsp if (start_commit == NULL)
2707 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2708 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2709 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2711 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2712 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2713 80ddbec8 2018-04-29 stsp if (error != NULL)
2714 8b473291 2019-02-21 stsp goto done;
2716 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2717 8b473291 2019-02-21 stsp if (error)
2718 ecb28ae0 2018-07-16 stsp goto done;
2720 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2721 04cc582a 2018-08-01 stsp if (view == NULL) {
2722 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2723 04cc582a 2018-08-01 stsp goto done;
2725 2fc00ff4 2019-08-31 stsp if (worktree) {
2726 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2727 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2728 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2729 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2730 2fc00ff4 2019-08-31 stsp goto done;
2733 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2734 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2735 ba4f502b 2018-08-04 stsp if (error)
2736 ba4f502b 2018-08-04 stsp goto done;
2737 2fc00ff4 2019-08-31 stsp if (worktree) {
2738 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2739 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2740 2fc00ff4 2019-08-31 stsp worktree = NULL;
2742 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2744 f135c941 2020-02-20 stsp free(in_repo_path);
2745 ecb28ae0 2018-07-16 stsp free(repo_path);
2746 ecb28ae0 2018-07-16 stsp free(cwd);
2747 899d86c2 2018-05-10 stsp free(start_id);
2748 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2750 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2751 ec142235 2019-03-07 stsp if (worktree)
2752 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2753 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2754 80ddbec8 2018-04-29 stsp return error;
2757 4ed7e80c 2018-05-20 stsp __dead static void
2758 9f7d7167 2018-04-29 stsp usage_diff(void)
2761 64453f7e 2020-11-21 stsp fprintf(stderr, "usage: %s diff [-a] [-r repository-path] "
2762 64453f7e 2020-11-21 stsp "object1 object2\n", getprogname());
2766 b304db33 2018-05-20 stsp static char *
2767 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2769 b304db33 2018-05-20 stsp char *line;
2770 b304db33 2018-05-20 stsp size_t linelen;
2771 b304db33 2018-05-20 stsp size_t lineno;
2772 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2774 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2776 b304db33 2018-05-20 stsp *len = linelen;
2777 b304db33 2018-05-20 stsp return line;
2780 6d17833f 2019-11-08 stsp static int
2781 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2782 41605754 2020-11-12 stsp regmatch_t *regmatch)
2784 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2787 f26dddb7 2019-11-08 stsp struct tog_color *
2788 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2790 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2792 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2793 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2794 6d17833f 2019-11-08 stsp return tc;
2797 6d17833f 2019-11-08 stsp return NULL;
2800 4ed7e80c 2018-05-20 stsp static const struct got_error *
2801 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2802 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2804 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2805 41605754 2020-11-12 stsp wchar_t *wline;
2806 41605754 2020-11-12 stsp int width;
2809 41605754 2020-11-12 stsp *wtotal = 0;
2811 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2812 41605754 2020-11-12 stsp if (s == NULL)
2813 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2815 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2816 41605754 2020-11-12 stsp if (err) {
2818 41605754 2020-11-12 stsp return err;
2820 41605754 2020-11-12 stsp waddwstr(window, wline);
2821 41605754 2020-11-12 stsp free(wline);
2823 41605754 2020-11-12 stsp wlimit -= width;
2824 41605754 2020-11-12 stsp *wtotal += width;
2826 41605754 2020-11-12 stsp if (wlimit > 0) {
2827 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2828 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2829 41605754 2020-11-12 stsp if (s == NULL) {
2830 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2832 41605754 2020-11-12 stsp return err;
2834 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2835 41605754 2020-11-12 stsp if (err) {
2837 41605754 2020-11-12 stsp return err;
2839 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2840 41605754 2020-11-12 stsp waddwstr(window, wline);
2841 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2842 41605754 2020-11-12 stsp free(wline);
2844 41605754 2020-11-12 stsp wlimit -= width;
2845 41605754 2020-11-12 stsp *wtotal += width;
2848 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2849 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2850 41605754 2020-11-12 stsp line + regmatch->rm_eo, wlimit, col_tab_align);
2852 41605754 2020-11-12 stsp return err;
2853 41605754 2020-11-12 stsp waddwstr(window, wline);
2854 41605754 2020-11-12 stsp free(wline);
2855 41605754 2020-11-12 stsp *wtotal += width;
2858 41605754 2020-11-12 stsp return NULL;
2861 41605754 2020-11-12 stsp static const struct got_error *
2862 fe621944 2020-11-10 stsp draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2863 fe621944 2020-11-10 stsp off_t *line_offsets, int selected_line, int max_lines,
2864 41605754 2020-11-12 stsp int *last_displayed_line, int *eof, char *header,
2865 41605754 2020-11-12 stsp struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2867 61e69b96 2018-05-20 stsp const struct got_error *err;
2868 fe621944 2020-11-10 stsp int nprinted = 0;
2869 b304db33 2018-05-20 stsp char *line;
2870 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2871 b304db33 2018-05-20 stsp size_t len;
2872 61e69b96 2018-05-20 stsp wchar_t *wline;
2873 e0b650dd 2018-05-20 stsp int width;
2874 fe621944 2020-11-10 stsp off_t line_offset;
2876 fe621944 2020-11-10 stsp line_offset = line_offsets[first_displayed_line - 1];
2877 fe621944 2020-11-10 stsp if (fseeko(f, line_offset, SEEK_SET) == -1)
2878 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2880 f7d12f7e 2018-08-01 stsp werase(view->window);
2882 a3404814 2018-09-02 stsp if (header) {
2883 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2884 135a2da0 2020-11-11 stsp first_displayed_line - 1 + selected_line, nlines,
2885 135a2da0 2020-11-11 stsp header) == -1)
2886 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2887 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2888 135a2da0 2020-11-11 stsp free(line);
2890 a3404814 2018-09-02 stsp return err;
2892 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2893 a3404814 2018-09-02 stsp wstandout(view->window);
2894 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2895 e54cc94a 2020-11-11 stsp free(wline);
2896 e54cc94a 2020-11-11 stsp wline = NULL;
2897 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2898 a3404814 2018-09-02 stsp wstandend(view->window);
2899 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2900 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2902 a3404814 2018-09-02 stsp if (max_lines <= 1)
2903 a3404814 2018-09-02 stsp return NULL;
2904 a3404814 2018-09-02 stsp max_lines--;
2908 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2909 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2910 26ed57b2 2018-05-19 stsp if (line == NULL) {
2915 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2917 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2918 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2919 41605754 2020-11-12 stsp if (first_displayed_line + nprinted == matched_line &&
2920 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2921 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
2922 41605754 2020-11-12 stsp view->window, regmatch);
2923 41605754 2020-11-12 stsp if (err) {
2924 41605754 2020-11-12 stsp free(line);
2925 41605754 2020-11-12 stsp return err;
2928 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2929 41605754 2020-11-12 stsp if (err) {
2930 41605754 2020-11-12 stsp free(line);
2931 41605754 2020-11-12 stsp return err;
2933 41605754 2020-11-12 stsp waddwstr(view->window, wline);
2934 41605754 2020-11-12 stsp free(wline);
2935 41605754 2020-11-12 stsp wline = NULL;
2938 6d17833f 2019-11-08 stsp wattr_off(view->window,
2939 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2940 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2941 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2942 fe621944 2020-11-10 stsp nprinted++;
2943 26ed57b2 2018-05-19 stsp free(line);
2945 fe621944 2020-11-10 stsp if (nprinted >= 1)
2946 fe621944 2020-11-10 stsp *last_displayed_line = first_displayed_line + (nprinted - 1);
2948 fe621944 2020-11-10 stsp *last_displayed_line = first_displayed_line;
2950 1a57306a 2018-09-02 stsp view_vborder(view);
2952 c3e9aa98 2019-05-13 jcs if (*eof) {
2953 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2954 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2955 c3e9aa98 2019-05-13 jcs nprinted++;
2958 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2960 c3e9aa98 2019-05-13 jcs return err;
2963 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2964 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2965 e54cc94a 2020-11-11 stsp free(wline);
2966 e54cc94a 2020-11-11 stsp wline = NULL;
2967 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2970 26ed57b2 2018-05-19 stsp return NULL;
2973 abd2672a 2018-12-23 stsp static char *
2974 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2976 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2977 09867e48 2019-08-13 stsp char *p, *s;
2979 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2980 09867e48 2019-08-13 stsp if (tm == NULL)
2981 09867e48 2019-08-13 stsp return NULL;
2982 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2983 09867e48 2019-08-13 stsp if (s == NULL)
2984 09867e48 2019-08-13 stsp return NULL;
2985 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2987 abd2672a 2018-12-23 stsp *p = '\0';
2991 4ed7e80c 2018-05-20 stsp static const struct got_error *
2992 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
2993 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
2995 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
2996 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2997 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2998 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3000 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3001 0208f208 2020-05-05 stsp if (qid != NULL) {
3002 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3003 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3006 0208f208 2020-05-05 stsp return err;
3008 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3009 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3013 0208f208 2020-05-05 stsp if (tree_id1) {
3014 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3016 0208f208 2020-05-05 stsp goto done;
3019 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3020 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3022 0208f208 2020-05-05 stsp goto done;
3024 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3025 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3027 0208f208 2020-05-05 stsp if (tree1)
3028 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3029 0208f208 2020-05-05 stsp if (tree2)
3030 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3031 0208f208 2020-05-05 stsp return err;
3034 0208f208 2020-05-05 stsp static const struct got_error *
3035 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3039 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3040 fe621944 2020-11-10 stsp if (p == NULL)
3041 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3042 fe621944 2020-11-10 stsp *line_offsets = p;
3043 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3044 fe621944 2020-11-10 stsp (*nlines)++;
3045 fe621944 2020-11-10 stsp return NULL;
3048 fe621944 2020-11-10 stsp static const struct got_error *
3049 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3050 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3051 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3053 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3054 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3055 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3056 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3057 45d799e2 2018-12-23 stsp time_t committer_time;
3058 45d799e2 2018-12-23 stsp const char *author, *committer;
3059 8b473291 2019-02-21 stsp char *refs_str = NULL;
3060 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3061 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3062 fe621944 2020-11-10 stsp off_t outoff = 0;
3065 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3067 8b473291 2019-02-21 stsp if (refs) {
3068 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3070 8b473291 2019-02-21 stsp return err;
3073 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3075 abd2672a 2018-12-23 stsp return err;
3077 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3078 15a94983 2018-12-23 stsp if (err) {
3079 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3080 15a94983 2018-12-23 stsp goto done;
3083 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3085 fe621944 2020-11-10 stsp goto done;
3087 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3088 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3089 fe621944 2020-11-10 stsp if (n < 0) {
3090 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3091 abd2672a 2018-12-23 stsp goto done;
3093 fe621944 2020-11-10 stsp outoff += n;
3094 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3096 fe621944 2020-11-10 stsp goto done;
3098 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3099 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3100 fe621944 2020-11-10 stsp if (n < 0) {
3101 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3102 abd2672a 2018-12-23 stsp goto done;
3104 fe621944 2020-11-10 stsp outoff += n;
3105 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3107 fe621944 2020-11-10 stsp goto done;
3109 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3110 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3111 fe621944 2020-11-10 stsp if (datestr) {
3112 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3113 fe621944 2020-11-10 stsp if (n < 0) {
3114 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3115 fe621944 2020-11-10 stsp goto done;
3117 fe621944 2020-11-10 stsp outoff += n;
3118 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3120 fe621944 2020-11-10 stsp goto done;
3122 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3123 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3124 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3125 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3126 fe621944 2020-11-10 stsp if (n < 0) {
3127 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3128 fe621944 2020-11-10 stsp goto done;
3130 fe621944 2020-11-10 stsp outoff += n;
3131 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3133 fe621944 2020-11-10 stsp goto done;
3135 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3137 5943eee2 2019-08-13 stsp goto done;
3138 fe621944 2020-11-10 stsp s = logmsg;
3139 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3140 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3141 fe621944 2020-11-10 stsp if (n < 0) {
3142 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3143 fe621944 2020-11-10 stsp goto done;
3145 fe621944 2020-11-10 stsp outoff += n;
3146 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3148 fe621944 2020-11-10 stsp goto done;
3151 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3153 0208f208 2020-05-05 stsp goto done;
3154 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3155 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3156 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3157 fe621944 2020-11-10 stsp if (n < 0) {
3158 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3159 fe621944 2020-11-10 stsp goto done;
3161 fe621944 2020-11-10 stsp outoff += n;
3162 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3164 fe621944 2020-11-10 stsp goto done;
3165 0208f208 2020-05-05 stsp free((char *)pe->path);
3166 0208f208 2020-05-05 stsp free(pe->data);
3169 0208f208 2020-05-05 stsp fputc('\n', outfile);
3171 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3173 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3174 abd2672a 2018-12-23 stsp free(id_str);
3175 5943eee2 2019-08-13 stsp free(logmsg);
3176 8b473291 2019-02-21 stsp free(refs_str);
3177 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3178 7510f233 2020-08-09 stsp if (err) {
3179 ae6a6978 2020-08-09 stsp free(*line_offsets);
3180 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3181 ae6a6978 2020-08-09 stsp *nlines = 0;
3183 fe621944 2020-11-10 stsp return err;
3186 abd2672a 2018-12-23 stsp static const struct got_error *
3187 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3189 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3190 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3191 15a94983 2018-12-23 stsp int obj_type;
3193 fe621944 2020-11-10 stsp free(s->line_offsets);
3194 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3195 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3196 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3197 fe621944 2020-11-10 stsp s->nlines = 0;
3199 511a516b 2018-05-19 stsp f = got_opentemp();
3200 48ae06ee 2018-10-18 stsp if (f == NULL) {
3201 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3202 48ae06ee 2018-10-18 stsp goto done;
3204 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
3205 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3206 fb43ecf1 2019-02-11 stsp goto done;
3210 15a94983 2018-12-23 stsp if (s->id1)
3211 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3213 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3215 15a94983 2018-12-23 stsp goto done;
3217 15a94983 2018-12-23 stsp switch (obj_type) {
3218 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3219 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3220 fe621944 2020-11-10 stsp s->id1, s->id2, NULL, NULL, s->diff_context, 0,
3221 64453f7e 2020-11-21 stsp s->force_text_diff, s->repo, s->f);
3223 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3224 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3225 64453f7e 2020-11-21 stsp s->id1, s->id2, "", "", s->diff_context, 0,
3226 64453f7e 2020-11-21 stsp s->force_text_diff, s->repo, s->f);
3228 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3229 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3230 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3231 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3233 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3235 3ffacbe1 2020-02-02 tracey goto done;
3236 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3237 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3238 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3239 fe621944 2020-11-10 stsp s->id2, s->refs, s->repo, s->f);
3240 f44b1f58 2020-02-02 tracey if (err)
3241 f44b1f58 2020-02-02 tracey goto done;
3242 f44b1f58 2020-02-02 tracey } else {
3243 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3244 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3245 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3246 fe621944 2020-11-10 stsp err = write_commit_info(
3247 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3248 fe621944 2020-11-10 stsp s->id2, s->refs, s->repo, s->f);
3249 f44b1f58 2020-02-02 tracey if (err)
3250 f44b1f58 2020-02-02 tracey goto done;
3255 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3257 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3258 64453f7e 2020-11-21 stsp s->id1, s->id2, s->diff_context, 0, s->force_text_diff,
3259 64453f7e 2020-11-21 stsp s->repo, s->f);
3263 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3266 f44b1f58 2020-02-02 tracey if (err)
3267 f44b1f58 2020-02-02 tracey goto done;
3269 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3270 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3271 48ae06ee 2018-10-18 stsp return err;
3274 f5215bb9 2019-02-22 stsp static void
3275 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3277 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3278 f5215bb9 2019-02-22 stsp update_panels();
3279 f5215bb9 2019-02-22 stsp doupdate();
3282 f44b1f58 2020-02-02 tracey static const struct got_error *
3283 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3285 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3287 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3288 f44b1f58 2020-02-02 tracey return NULL;
3291 f44b1f58 2020-02-02 tracey static const struct got_error *
3292 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3294 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3295 f44b1f58 2020-02-02 tracey int lineno;
3297 f44b1f58 2020-02-02 tracey if (!view->searching) {
3298 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3299 f44b1f58 2020-02-02 tracey return NULL;
3302 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3303 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3304 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3306 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3307 f44b1f58 2020-02-02 tracey } else {
3308 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3309 f44b1f58 2020-02-02 tracey lineno = 1;
3311 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3314 f44b1f58 2020-02-02 tracey while (1) {
3315 f44b1f58 2020-02-02 tracey char *line = NULL;
3316 f44b1f58 2020-02-02 tracey off_t offset;
3317 f44b1f58 2020-02-02 tracey size_t len;
3319 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3320 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3321 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3322 f44b1f58 2020-02-02 tracey free(line);
3326 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3327 f44b1f58 2020-02-02 tracey lineno = 1;
3329 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3332 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3333 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3334 f44b1f58 2020-02-02 tracey free(line);
3335 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3337 f44b1f58 2020-02-02 tracey free(line);
3338 f44b1f58 2020-02-02 tracey line = parse_next_line(s->f, &len);
3339 41605754 2020-11-12 stsp if (line &&
3340 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3341 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3342 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3343 f44b1f58 2020-02-02 tracey free(line);
3346 f44b1f58 2020-02-02 tracey free(line);
3347 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3348 f44b1f58 2020-02-02 tracey lineno++;
3350 f44b1f58 2020-02-02 tracey lineno--;
3353 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3354 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3355 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3358 f44b1f58 2020-02-02 tracey return NULL;
3361 48ae06ee 2018-10-18 stsp static const struct got_error *
3362 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3363 64453f7e 2020-11-21 stsp struct got_object_id *id2, int force_text_diff, struct tog_view *log_view,
3364 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
3366 48ae06ee 2018-10-18 stsp const struct got_error *err;
3367 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3369 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3370 15a94983 2018-12-23 stsp int type1, type2;
3371 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3373 15a94983 2018-12-23 stsp return err;
3374 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3376 15a94983 2018-12-23 stsp return err;
3378 15a94983 2018-12-23 stsp if (type1 != type2)
3379 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3381 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3382 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3383 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3384 f44b1f58 2020-02-02 tracey s->repo = repo;
3385 f44b1f58 2020-02-02 tracey s->refs = refs;
3386 f44b1f58 2020-02-02 tracey s->id1 = id1;
3387 f44b1f58 2020-02-02 tracey s->id2 = id2;
3389 15a94983 2018-12-23 stsp if (id1) {
3390 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3391 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3392 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3394 5465d566 2020-02-01 tracey s->id1 = NULL;
3396 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3397 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3398 5465d566 2020-02-01 tracey free(s->id1);
3399 5465d566 2020-02-01 tracey s->id1 = NULL;
3400 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3402 5465d566 2020-02-01 tracey s->f = NULL;
3403 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3404 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3405 5465d566 2020-02-01 tracey s->diff_context = 3;
3406 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3407 5465d566 2020-02-01 tracey s->log_view = log_view;
3408 5465d566 2020-02-01 tracey s->repo = repo;
3409 5465d566 2020-02-01 tracey s->refs = refs;
3411 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3412 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3413 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3414 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3415 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3417 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_PLUS,
3420 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
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;
3425 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3426 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3427 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3428 6d17833f 2019-11-08 stsp if (err) {
3429 5465d566 2020-02-01 tracey free_colors(&s->colors);
3430 6d17833f 2019-11-08 stsp return err;
3433 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3434 528c17dd 2020-07-31 stsp "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3435 0208f208 2020-05-05 stsp TOG_COLOR_DIFF_META,
3436 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3437 6d17833f 2019-11-08 stsp if (err) {
3438 5465d566 2020-02-01 tracey free_colors(&s->colors);
3439 6d17833f 2019-11-08 stsp return err;
3442 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3443 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3444 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3445 11b20872 2019-11-08 stsp if (err) {
3446 5465d566 2020-02-01 tracey free_colors(&s->colors);
3447 11b20872 2019-11-08 stsp return err;
3450 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3451 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3452 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3453 11b20872 2019-11-08 stsp if (err) {
3454 5465d566 2020-02-01 tracey free_colors(&s->colors);
3455 11b20872 2019-11-08 stsp return err;
3459 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3460 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3461 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3463 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3464 fe621944 2020-11-10 stsp s->nlines = 0;
3465 5465d566 2020-02-01 tracey err = create_diff(s);
3466 48ae06ee 2018-10-18 stsp if (err) {
3467 5465d566 2020-02-01 tracey free(s->id1);
3468 5465d566 2020-02-01 tracey s->id1 = NULL;
3469 5465d566 2020-02-01 tracey free(s->id2);
3470 5465d566 2020-02-01 tracey s->id2 = NULL;
3471 48ae06ee 2018-10-18 stsp return err;
3474 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3475 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3476 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3477 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3478 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3480 5dc9f4bc 2018-08-04 stsp return NULL;
3483 e5a0f69f 2018-08-18 stsp static const struct got_error *
3484 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3486 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3487 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3489 5465d566 2020-02-01 tracey free(s->id1);
3490 5465d566 2020-02-01 tracey s->id1 = NULL;
3491 5465d566 2020-02-01 tracey free(s->id2);
3492 5465d566 2020-02-01 tracey s->id2 = NULL;
3493 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3494 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3495 5465d566 2020-02-01 tracey free_colors(&s->colors);
3496 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3497 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3498 fe621944 2020-11-10 stsp s->nlines = 0;
3499 e5a0f69f 2018-08-18 stsp return err;
3502 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3503 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3505 a3404814 2018-09-02 stsp const struct got_error *err;
3506 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3507 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3509 a3404814 2018-09-02 stsp if (s->id1) {
3510 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3512 a3404814 2018-09-02 stsp return err;
3514 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3516 a3404814 2018-09-02 stsp return err;
3518 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
3519 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3520 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3521 a3404814 2018-09-02 stsp free(id_str1);
3522 a3404814 2018-09-02 stsp free(id_str2);
3523 a3404814 2018-09-02 stsp return err;
3525 a3404814 2018-09-02 stsp free(id_str1);
3526 a3404814 2018-09-02 stsp free(id_str2);
3528 fe621944 2020-11-10 stsp return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3529 fe621944 2020-11-10 stsp s->line_offsets, s->selected_line, view->nlines,
3530 41605754 2020-11-12 stsp &s->last_displayed_line, &s->eof, header, &s->colors,
3531 41605754 2020-11-12 stsp s->matched_line, &view->regmatch);
3534 15a087fe 2019-02-21 stsp static const struct got_error *
3535 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3536 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3538 d7a04538 2019-02-21 stsp const struct got_error *err;
3539 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3540 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3541 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3543 15a087fe 2019-02-21 stsp free(s->id2);
3544 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3545 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3546 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3548 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3550 d7a04538 2019-02-21 stsp return err;
3551 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3552 15a087fe 2019-02-21 stsp free(s->id1);
3553 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3554 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3555 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3556 15a087fe 2019-02-21 stsp return NULL;
3559 0cf4efb1 2018-09-29 stsp static const struct got_error *
3560 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3561 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3563 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3564 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3565 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3566 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3569 e5a0f69f 2018-08-18 stsp switch (ch) {
3571 64453f7e 2020-11-21 stsp s->force_text_diff = !s->force_text_diff;
3572 64453f7e 2020-11-21 stsp wclear(view->window);
3573 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3574 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3575 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3576 64453f7e 2020-11-21 stsp err = create_diff(s);
3579 1e37a5c2 2019-05-12 jcs case KEY_UP:
3580 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3581 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3583 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3584 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3585 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3588 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3589 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3590 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3593 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3594 1e37a5c2 2019-05-12 jcs if (!s->eof)
3595 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3597 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3598 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3600 00ba99a7 2019-05-12 jcs if (s->eof)
3603 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3604 1e37a5c2 2019-05-12 jcs char *line;
3605 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3606 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3607 1e37a5c2 2019-05-12 jcs if (line == NULL)
3612 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3613 1e37a5c2 2019-05-12 jcs s->diff_context--;
3614 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3615 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3616 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3617 27829c9e 2020-11-21 stsp s->nlines) {
3618 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3619 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3624 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3625 1e37a5c2 2019-05-12 jcs s->diff_context++;
3626 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3627 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3632 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3634 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3635 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3636 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3637 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3640 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3645 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3649 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3650 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3652 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3653 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3657 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3659 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3661 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3662 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3663 7c1452c1 2020-03-26 stsp err = trigger_log_thread(s->log_view, 1,
3664 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
3665 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
3666 7c1452c1 2020-03-26 stsp &ls->thread_args.need_commits,
3667 7c1452c1 2020-03-26 stsp &ls->thread_args.commit_loaded);
3671 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3676 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3677 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3680 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3684 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3685 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3687 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3688 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3694 bcbd79e2 2018-08-19 stsp return err;
3697 4ed7e80c 2018-05-20 stsp static const struct got_error *
3698 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3700 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3701 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3702 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3703 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3704 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3705 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3706 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3707 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3708 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3710 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3712 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3713 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3714 eb6600df 2019-01-04 stsp NULL) == -1)
3715 26ed57b2 2018-05-19 stsp err(1, "pledge");
3718 64453f7e 2020-11-21 stsp while ((ch = getopt(argc, argv, "ar:")) != -1) {
3719 26ed57b2 2018-05-19 stsp switch (ch) {
3721 64453f7e 2020-11-21 stsp force_text_diff = 1;
3723 09b5bff8 2020-02-23 naddy case 'r':
3724 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3725 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3726 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3730 17020d27 2019-03-07 stsp usage_diff();
3731 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3735 26ed57b2 2018-05-19 stsp argc -= optind;
3736 26ed57b2 2018-05-19 stsp argv += optind;
3738 26ed57b2 2018-05-19 stsp if (argc == 0) {
3739 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3740 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3741 15a94983 2018-12-23 stsp id_str1 = argv[0];
3742 15a94983 2018-12-23 stsp id_str2 = argv[1];
3744 26ed57b2 2018-05-19 stsp usage_diff();
3746 a273ac94 2020-02-23 naddy cwd = getcwd(NULL, 0);
3747 a273ac94 2020-02-23 naddy if (cwd == NULL)
3748 a273ac94 2020-02-23 naddy return got_error_from_errno("getcwd");
3750 a273ac94 2020-02-23 naddy error = got_worktree_open(&worktree, cwd);
3751 a273ac94 2020-02-23 naddy if (error && error->code != GOT_ERR_NOT_WORKTREE)
3752 a273ac94 2020-02-23 naddy goto done;
3754 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3755 a273ac94 2020-02-23 naddy if (worktree)
3756 a273ac94 2020-02-23 naddy repo_path =
3757 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3759 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3761 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3762 a273ac94 2020-02-23 naddy error = got_error_from_errno("strdup");
3763 a273ac94 2020-02-23 naddy goto done;
3766 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3767 eb6600df 2019-01-04 stsp if (error)
3768 eb6600df 2019-01-04 stsp goto done;
3770 a273ac94 2020-02-23 naddy init_curses();
3772 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3773 26ed57b2 2018-05-19 stsp if (error)
3774 26ed57b2 2018-05-19 stsp goto done;
3776 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
3777 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3778 26ed57b2 2018-05-19 stsp if (error)
3779 26ed57b2 2018-05-19 stsp goto done;
3781 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
3782 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3783 26ed57b2 2018-05-19 stsp if (error)
3784 26ed57b2 2018-05-19 stsp goto done;
3786 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3787 8b473291 2019-02-21 stsp if (error)
3788 8b473291 2019-02-21 stsp goto done;
3790 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3791 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3792 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3793 ea5e7bb5 2018-08-01 stsp goto done;
3795 64453f7e 2020-11-21 stsp error = open_diff_view(view, id1, id2, force_text_diff, NULL, &refs,
3797 5dc9f4bc 2018-08-04 stsp if (error)
3798 5dc9f4bc 2018-08-04 stsp goto done;
3799 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3801 c02c541e 2019-03-29 stsp free(repo_path);
3802 a273ac94 2020-02-23 naddy free(cwd);
3804 921be706 2019-06-28 stsp got_repo_close(repo);
3805 a273ac94 2020-02-23 naddy if (worktree)
3806 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3807 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3808 26ed57b2 2018-05-19 stsp return error;
3811 4ed7e80c 2018-05-20 stsp __dead static void
3812 9f7d7167 2018-04-29 stsp usage_blame(void)
3815 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3816 9f7d7167 2018-04-29 stsp getprogname());
3820 84451b3e 2018-07-10 stsp struct tog_blame_line {
3821 84451b3e 2018-07-10 stsp int annotated;
3822 84451b3e 2018-07-10 stsp struct got_object_id *id;
3825 4ed7e80c 2018-05-20 stsp static const struct got_error *
3826 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3827 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3828 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3829 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3830 41605754 2020-11-12 stsp struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3832 84451b3e 2018-07-10 stsp const struct got_error *err;
3833 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3834 84451b3e 2018-07-10 stsp char *line;
3835 84451b3e 2018-07-10 stsp size_t len;
3836 84451b3e 2018-07-10 stsp wchar_t *wline;
3837 27a741e5 2019-09-11 stsp int width;
3838 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3839 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3840 ab089a2a 2018-07-12 stsp char *id_str;
3841 11b20872 2019-11-08 stsp struct tog_color *tc;
3843 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3845 ab089a2a 2018-07-12 stsp return err;
3847 84451b3e 2018-07-10 stsp rewind(f);
3848 f7d12f7e 2018-08-01 stsp werase(view->window);
3850 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3851 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3852 ab089a2a 2018-07-12 stsp free(id_str);
3853 ab089a2a 2018-07-12 stsp return err;
3856 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3857 ab089a2a 2018-07-12 stsp free(line);
3858 2550e4c3 2018-07-13 stsp line = NULL;
3860 1cae65b4 2019-09-22 stsp return err;
3861 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3862 a3404814 2018-09-02 stsp wstandout(view->window);
3863 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3865 11b20872 2019-11-08 stsp wattr_on(view->window,
3866 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3867 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3869 11b20872 2019-11-08 stsp wattr_off(view->window,
3870 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3871 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3872 a3404814 2018-09-02 stsp wstandend(view->window);
3873 2550e4c3 2018-07-13 stsp free(wline);
3874 2550e4c3 2018-07-13 stsp wline = NULL;
3875 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3876 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3878 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3879 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3880 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3881 ab089a2a 2018-07-12 stsp free(id_str);
3882 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3884 ab089a2a 2018-07-12 stsp free(id_str);
3885 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3886 3f60a8ef 2018-07-10 stsp free(line);
3887 2550e4c3 2018-07-13 stsp line = NULL;
3889 3f60a8ef 2018-07-10 stsp return err;
3890 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3891 2550e4c3 2018-07-13 stsp free(wline);
3892 2550e4c3 2018-07-13 stsp wline = NULL;
3893 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3894 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3897 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3898 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3899 84451b3e 2018-07-10 stsp if (line == NULL) {
3903 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3904 84451b3e 2018-07-10 stsp free(line);
3908 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3909 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3911 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3912 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3913 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3914 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3915 27a741e5 2019-09-11 stsp !(view->focussed &&
3916 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3917 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3918 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3919 8d0fe45a 2019-08-12 stsp char *id_str;
3920 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3921 8d0fe45a 2019-08-12 stsp if (err) {
3922 8d0fe45a 2019-08-12 stsp free(line);
3923 8d0fe45a 2019-08-12 stsp return err;
3925 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3927 11b20872 2019-11-08 stsp wattr_on(view->window,
3928 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3929 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3931 11b20872 2019-11-08 stsp wattr_off(view->window,
3932 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3933 8d0fe45a 2019-08-12 stsp free(id_str);
3934 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3936 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3937 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3940 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3941 ee41ec32 2018-07-10 stsp prev_id = NULL;
3944 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3945 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3946 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3948 41605754 2020-11-12 stsp if (view->ncols <= 9) {
3949 41605754 2020-11-12 stsp width = 9;
3950 41605754 2020-11-12 stsp wline = wcsdup(L"");
3951 41605754 2020-11-12 stsp if (wline == NULL) {
3952 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
3953 41605754 2020-11-12 stsp free(line);
3954 41605754 2020-11-12 stsp return err;
3956 41605754 2020-11-12 stsp } else if (*first_displayed_line + nprinted == matched_line &&
3957 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3958 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
3959 41605754 2020-11-12 stsp view->window, regmatch);
3960 41605754 2020-11-12 stsp if (err) {
3961 41605754 2020-11-12 stsp free(line);
3962 41605754 2020-11-12 stsp return err;
3964 41605754 2020-11-12 stsp width += 9;
3966 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
3967 41605754 2020-11-12 stsp view->ncols - 9, 9);
3968 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3969 41605754 2020-11-12 stsp free(wline);
3970 41605754 2020-11-12 stsp wline = NULL;
3971 41605754 2020-11-12 stsp width += 9;
3974 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3975 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3976 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3977 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3978 84451b3e 2018-07-10 stsp free(line);
3980 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3982 1a57306a 2018-09-02 stsp view_vborder(view);
3984 84451b3e 2018-07-10 stsp return NULL;
3987 84451b3e 2018-07-10 stsp static const struct got_error *
3988 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3990 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3991 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3992 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3993 1a76625f 2018-10-22 stsp int errcode;
3995 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3996 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3997 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3999 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4000 1a76625f 2018-10-22 stsp if (errcode)
4001 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4003 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4004 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4005 d68a0a7d 2018-07-10 stsp goto done;
4008 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4009 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4011 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4012 d68a0a7d 2018-07-10 stsp if (line->annotated)
4013 d68a0a7d 2018-07-10 stsp goto done;
4015 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4016 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4017 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4018 84451b3e 2018-07-10 stsp goto done;
4020 84451b3e 2018-07-10 stsp line->annotated = 1;
4022 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4023 1a76625f 2018-10-22 stsp if (errcode)
4024 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4025 84451b3e 2018-07-10 stsp return err;
4028 84451b3e 2018-07-10 stsp static void *
4029 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4031 18430de3 2018-07-10 stsp const struct got_error *err;
4032 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4033 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4034 1a76625f 2018-10-22 stsp int errcode;
4036 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4038 61266923 2020-01-14 stsp return (void *)err;
4040 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4041 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4042 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4043 fc06ba56 2019-08-22 stsp err = NULL;
4045 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4046 1a76625f 2018-10-22 stsp if (errcode)
4047 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4048 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4050 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
4051 c9beca56 2018-07-22 stsp ta->repo = NULL;
4052 c9beca56 2018-07-22 stsp *ta->complete = 1;
4054 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4055 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4056 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4058 18430de3 2018-07-10 stsp return (void *)err;
4061 245d91c1 2018-07-12 stsp static struct got_object_id *
4062 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4063 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4065 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4067 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4068 8d0fe45a 2019-08-12 stsp return NULL;
4070 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4071 245d91c1 2018-07-12 stsp if (!line->annotated)
4072 245d91c1 2018-07-12 stsp return NULL;
4074 245d91c1 2018-07-12 stsp return line->id;
4077 b880a918 2018-07-10 stsp static const struct got_error *
4078 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4080 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4083 245d91c1 2018-07-12 stsp if (blame->thread) {
4084 1a76625f 2018-10-22 stsp int errcode;
4085 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4086 1a76625f 2018-10-22 stsp if (errcode)
4087 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4088 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4089 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4090 1a76625f 2018-10-22 stsp if (errcode)
4091 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4092 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4093 1a76625f 2018-10-22 stsp if (errcode)
4094 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4095 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4096 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4097 245d91c1 2018-07-12 stsp err = NULL;
4098 245d91c1 2018-07-12 stsp blame->thread = NULL;
4100 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4101 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
4102 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4104 245d91c1 2018-07-12 stsp if (blame->f) {
4105 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
4106 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4107 245d91c1 2018-07-12 stsp blame->f = NULL;
4109 57670559 2018-12-23 stsp if (blame->lines) {
4110 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4111 57670559 2018-12-23 stsp free(blame->lines[i].id);
4112 57670559 2018-12-23 stsp free(blame->lines);
4113 57670559 2018-12-23 stsp blame->lines = NULL;
4115 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4116 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4118 245d91c1 2018-07-12 stsp return err;
4121 245d91c1 2018-07-12 stsp static const struct got_error *
4122 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4124 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4125 fc06ba56 2019-08-22 stsp int *done = arg;
4126 fc06ba56 2019-08-22 stsp int errcode;
4128 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4129 fc06ba56 2019-08-22 stsp if (errcode)
4130 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4131 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4133 fc06ba56 2019-08-22 stsp if (*done)
4134 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4136 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4137 fc06ba56 2019-08-22 stsp if (errcode)
4138 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4139 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4141 fc06ba56 2019-08-22 stsp return err;
4144 fc06ba56 2019-08-22 stsp static const struct got_error *
4145 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4146 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
4147 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
4148 245d91c1 2018-07-12 stsp struct got_repository *repo)
4150 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4151 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4152 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4153 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4154 15a94983 2018-12-23 stsp int obj_type;
4156 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4158 15a94983 2018-12-23 stsp return err;
4160 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
4162 84451b3e 2018-07-10 stsp goto done;
4164 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4165 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4166 84451b3e 2018-07-10 stsp goto done;
4169 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4171 a70480e0 2018-06-23 stsp goto done;
4172 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4173 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4174 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4175 84451b3e 2018-07-10 stsp goto done;
4177 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4178 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4179 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
4180 84451b3e 2018-07-10 stsp goto done;
4182 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4183 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4184 b02560ec 2019-08-19 stsp blame->nlines--;
4186 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4187 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4188 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4189 84451b3e 2018-07-10 stsp goto done;
4192 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4194 bd24772e 2018-07-11 stsp goto done;
4196 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4197 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4198 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4199 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
4200 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4201 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4202 245d91c1 2018-07-12 stsp goto done;
4204 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
4206 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
4207 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4208 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4209 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
4210 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4211 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
4212 245d91c1 2018-07-12 stsp *blame_complete = 0;
4216 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4217 27d434c2 2018-09-15 stsp free(obj_id);
4219 245d91c1 2018-07-12 stsp stop_blame(blame);
4220 245d91c1 2018-07-12 stsp return err;
4223 245d91c1 2018-07-12 stsp static const struct got_error *
4224 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4225 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4226 8b473291 2019-02-21 stsp struct got_repository *repo)
4228 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4229 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4231 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4233 c4843652 2019-08-12 stsp s->path = strdup(path);
4234 c4843652 2019-08-12 stsp if (s->path == NULL)
4235 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4237 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4238 c4843652 2019-08-12 stsp if (err) {
4239 c4843652 2019-08-12 stsp free(s->path);
4240 7cbe629d 2018-08-04 stsp return err;
4243 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4244 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4245 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4246 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4247 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4248 fb2756b9 2018-08-04 stsp s->repo = repo;
4249 8b473291 2019-02-21 stsp s->refs = refs;
4250 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4251 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4253 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4254 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4255 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4256 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4258 11b20872 2019-11-08 stsp return err;
4261 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4262 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4263 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4264 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4265 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4267 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
4268 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
4269 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
4270 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
4273 e5a0f69f 2018-08-18 stsp static const struct got_error *
4274 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4276 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4277 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4279 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4280 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4282 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4283 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4284 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4285 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4286 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4289 e5a0f69f 2018-08-18 stsp free(s->path);
4290 11b20872 2019-11-08 stsp free_colors(&s->colors);
4292 e5a0f69f 2018-08-18 stsp return err;
4295 7cbe629d 2018-08-04 stsp static const struct got_error *
4296 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4298 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4300 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4301 6c4c42e0 2019-06-24 stsp return NULL;
4304 6c4c42e0 2019-06-24 stsp static const struct got_error *
4305 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4307 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4308 6c4c42e0 2019-06-24 stsp int lineno;
4310 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4311 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4312 6c4c42e0 2019-06-24 stsp return NULL;
4315 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4316 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4317 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4319 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4321 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4322 6c4c42e0 2019-06-24 stsp lineno = 1;
4324 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4327 6c4c42e0 2019-06-24 stsp while (1) {
4328 6c4c42e0 2019-06-24 stsp char *line = NULL;
4329 6c4c42e0 2019-06-24 stsp off_t offset;
4330 6c4c42e0 2019-06-24 stsp size_t len;
4332 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4333 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4334 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4335 6c4c42e0 2019-06-24 stsp free(line);
4339 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4340 6c4c42e0 2019-06-24 stsp lineno = 1;
4342 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4345 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4346 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4347 6c4c42e0 2019-06-24 stsp free(line);
4348 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4350 6c4c42e0 2019-06-24 stsp free(line);
4351 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
4352 41605754 2020-11-12 stsp if (line &&
4353 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4354 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4355 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4356 6c4c42e0 2019-06-24 stsp free(line);
4359 6c4c42e0 2019-06-24 stsp free(line);
4360 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4366 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4367 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4368 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4371 6c4c42e0 2019-06-24 stsp return NULL;
4374 6c4c42e0 2019-06-24 stsp static const struct got_error *
4375 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4377 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4378 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4379 2b380cc8 2018-10-24 stsp int errcode;
4381 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4382 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4383 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4384 2b380cc8 2018-10-24 stsp if (errcode)
4385 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4387 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4390 51fe7530 2019-08-19 stsp if (s->blame_complete)
4391 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4393 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4394 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4395 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
4396 41605754 2020-11-12 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4397 41605754 2020-11-12 stsp s->matched_line, &view->regmatch);
4399 669b5ffa 2018-10-07 stsp view_vborder(view);
4400 e5a0f69f 2018-08-18 stsp return err;
4403 e5a0f69f 2018-08-18 stsp static const struct got_error *
4404 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4405 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4407 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4408 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4409 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4410 669b5ffa 2018-10-07 stsp int begin_x = 0;
4412 e5a0f69f 2018-08-18 stsp switch (ch) {
4414 1e37a5c2 2019-05-12 jcs s->done = 1;
4417 1e37a5c2 2019-05-12 jcs case KEY_UP:
4418 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4419 1e37a5c2 2019-05-12 jcs s->selected_line--;
4420 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4421 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4422 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4424 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4425 ea025d1d 2020-02-22 naddy case CTRL('b'):
4426 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4427 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4430 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4431 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4432 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4434 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4437 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4438 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4439 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4440 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4441 1e37a5c2 2019-05-12 jcs s->selected_line++;
4442 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4443 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4444 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4447 1e37a5c2 2019-05-12 jcs case 'p': {
4448 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4449 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4450 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4451 1e37a5c2 2019-05-12 jcs if (id == NULL)
4453 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4454 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4455 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4456 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4457 1e37a5c2 2019-05-12 jcs int obj_type;
4458 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4459 1e37a5c2 2019-05-12 jcs s->repo, id);
4462 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4463 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4464 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4465 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4468 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4469 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4470 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4471 e5a0f69f 2018-08-18 stsp if (err) {
4472 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4473 1e37a5c2 2019-05-12 jcs err = NULL;
4474 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4477 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4479 1e37a5c2 2019-05-12 jcs free(blob_id);
4480 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4481 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4482 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4485 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4487 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4489 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4490 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4492 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4497 1e37a5c2 2019-05-12 jcs s->done = 1;
4498 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4499 1e37a5c2 2019-05-12 jcs s->done = 0;
4500 1e37a5c2 2019-05-12 jcs if (thread_err)
4502 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4503 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4504 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4505 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4506 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
4507 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
4512 1e37a5c2 2019-05-12 jcs case 'B': {
4513 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4514 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4515 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4517 1e37a5c2 2019-05-12 jcs s->done = 1;
4518 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4519 1e37a5c2 2019-05-12 jcs s->done = 0;
4520 1e37a5c2 2019-05-12 jcs if (thread_err)
4522 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4523 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4524 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4525 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4526 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4527 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4528 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
4529 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
4534 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4535 1e37a5c2 2019-05-12 jcs case '\r': {
4536 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4537 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4538 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4539 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4540 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4541 1e37a5c2 2019-05-12 jcs if (id == NULL)
4543 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4546 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4547 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4548 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4549 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4550 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4551 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4552 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4553 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4556 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4557 64453f7e 2020-11-21 stsp id, 0, NULL, s->refs, s->repo);
4558 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4560 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4563 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4564 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4567 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4569 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4572 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4573 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4575 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4580 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4581 ea025d1d 2020-02-22 naddy case CTRL('f'):
4583 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4584 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4585 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4588 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4589 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4590 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4591 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4594 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4595 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4596 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4597 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4599 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4600 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4601 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4603 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4604 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4605 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4606 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4612 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4615 a70480e0 2018-06-23 stsp static const struct got_error *
4616 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4618 a70480e0 2018-06-23 stsp const struct got_error *error;
4619 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4620 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4621 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4622 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4623 0587e10c 2020-07-23 stsp char *link_target = NULL;
4624 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4625 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4627 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4629 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4631 a70480e0 2018-06-23 stsp #ifndef PROFILE
4632 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4633 8e94dd5b 2019-01-04 stsp NULL) == -1)
4634 a70480e0 2018-06-23 stsp err(1, "pledge");
4637 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4638 a70480e0 2018-06-23 stsp switch (ch) {
4640 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4643 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4644 69069811 2018-08-02 stsp if (repo_path == NULL)
4645 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4649 17020d27 2019-03-07 stsp usage_blame();
4650 a70480e0 2018-06-23 stsp /* NOTREACHED */
4654 a70480e0 2018-06-23 stsp argc -= optind;
4655 a70480e0 2018-06-23 stsp argv += optind;
4657 f135c941 2020-02-20 stsp if (argc != 1)
4658 a70480e0 2018-06-23 stsp usage_blame();
4660 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4661 6962eb72 2020-02-20 stsp if (cwd == NULL)
4662 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
4664 f135c941 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
4665 f135c941 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4666 f135c941 2020-02-20 stsp goto done;
4668 69069811 2018-08-02 stsp if (repo_path == NULL) {
4669 f135c941 2020-02-20 stsp if (worktree)
4670 eb41ed75 2019-02-05 stsp repo_path =
4671 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4673 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4675 f135c941 2020-02-20 stsp if (repo_path == NULL) {
4676 f135c941 2020-02-20 stsp error = got_error_from_errno("strdup");
4677 f135c941 2020-02-20 stsp goto done;
4680 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4681 c02c541e 2019-03-29 stsp if (error != NULL)
4682 8e94dd5b 2019-01-04 stsp goto done;
4684 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4685 f135c941 2020-02-20 stsp worktree);
4686 c02c541e 2019-03-29 stsp if (error)
4687 92205607 2019-01-04 stsp goto done;
4689 f135c941 2020-02-20 stsp init_curses();
4691 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4692 eb41ed75 2019-02-05 stsp if (error)
4693 69069811 2018-08-02 stsp goto done;
4695 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4696 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4697 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4698 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4699 a70480e0 2018-06-23 stsp if (error != NULL)
4700 66b4983c 2018-06-23 stsp goto done;
4701 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4702 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4704 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4705 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4707 a19e88aa 2018-06-23 stsp if (error != NULL)
4708 8b473291 2019-02-21 stsp goto done;
4710 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4711 8b473291 2019-02-21 stsp if (error)
4712 a19e88aa 2018-06-23 stsp goto done;
4714 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4715 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4716 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4717 e1cd8fed 2018-08-01 stsp goto done;
4720 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4721 0587e10c 2020-07-23 stsp commit_id, repo);
4722 7cbe629d 2018-08-04 stsp if (error)
4723 7cbe629d 2018-08-04 stsp goto done;
4725 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4726 0587e10c 2020-07-23 stsp commit_id, &refs, repo);
4727 0587e10c 2020-07-23 stsp if (error)
4728 0587e10c 2020-07-23 stsp goto done;
4729 12314ad4 2019-08-31 stsp if (worktree) {
4730 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4731 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4732 12314ad4 2019-08-31 stsp worktree = NULL;
4734 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4736 69069811 2018-08-02 stsp free(repo_path);
4737 f135c941 2020-02-20 stsp free(in_repo_path);
4738 0587e10c 2020-07-23 stsp free(link_target);
4739 69069811 2018-08-02 stsp free(cwd);
4740 a70480e0 2018-06-23 stsp free(commit_id);
4741 eb41ed75 2019-02-05 stsp if (worktree)
4742 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4744 a70480e0 2018-06-23 stsp got_repo_close(repo);
4745 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4746 a70480e0 2018-06-23 stsp return error;
4749 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4750 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4751 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4752 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4753 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4754 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4755 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4756 0d6c6ee3 2020-05-20 stsp int isroot, struct tog_colors *colors, struct got_repository *repo)
4758 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4759 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4760 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4761 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4762 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4764 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4766 f7d12f7e 2018-08-01 stsp werase(view->window);
4768 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4769 ffd1d5e5 2018-06-23 stsp return NULL;
4771 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4773 ffd1d5e5 2018-06-23 stsp return err;
4774 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4775 a3404814 2018-09-02 stsp wstandout(view->window);
4776 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4778 11b20872 2019-11-08 stsp wattr_on(view->window,
4779 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4780 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4782 11b20872 2019-11-08 stsp wattr_off(view->window,
4783 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4784 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4785 a3404814 2018-09-02 stsp wstandend(view->window);
4786 2550e4c3 2018-07-13 stsp free(wline);
4787 2550e4c3 2018-07-13 stsp wline = NULL;
4788 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4789 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4790 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4791 ffd1d5e5 2018-06-23 stsp return NULL;
4792 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4794 ce52c690 2018-06-23 stsp return err;
4795 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4796 2550e4c3 2018-07-13 stsp free(wline);
4797 2550e4c3 2018-07-13 stsp wline = NULL;
4798 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4799 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4800 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4801 ffd1d5e5 2018-06-23 stsp return NULL;
4802 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4803 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4804 a1eca9bb 2018-06-23 stsp return NULL;
4806 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4807 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4808 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4809 0cf4efb1 2018-09-29 stsp if (view->focussed)
4810 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4811 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4813 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4814 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4815 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4816 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4817 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4818 ffd1d5e5 2018-06-23 stsp return NULL;
4822 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4825 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4826 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4827 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4828 848d6979 2019-08-12 stsp const char *modestr = "";
4829 56e0773d 2019-11-28 stsp mode_t mode;
4831 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4832 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4834 1d13200f 2018-07-12 stsp if (show_ids) {
4835 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4836 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4838 638f9024 2019-05-13 stsp return got_error_from_errno(
4839 230a42bd 2019-05-11 jcs "got_object_id_str");
4841 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4842 63c5ca5d 2019-08-24 stsp modestr = "$";
4843 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4846 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4847 0d6c6ee3 2020-05-20 stsp te, repo);
4848 0d6c6ee3 2020-05-20 stsp if (err) {
4849 0d6c6ee3 2020-05-20 stsp free(id_str);
4850 0d6c6ee3 2020-05-20 stsp return err;
4852 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4853 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4854 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4856 848d6979 2019-08-12 stsp modestr = "@";
4858 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4859 848d6979 2019-08-12 stsp modestr = "/";
4860 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4861 848d6979 2019-08-12 stsp modestr = "*";
4862 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4863 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
4864 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
4865 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
4866 1d13200f 2018-07-12 stsp free(id_str);
4867 0d6c6ee3 2020-05-20 stsp free(link_target);
4868 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4870 1d13200f 2018-07-12 stsp free(id_str);
4871 0d6c6ee3 2020-05-20 stsp free(link_target);
4872 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4873 ffd1d5e5 2018-06-23 stsp if (err) {
4874 ffd1d5e5 2018-06-23 stsp free(line);
4877 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4878 0cf4efb1 2018-09-29 stsp if (view->focussed)
4879 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4880 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4882 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4884 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4885 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4886 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4888 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4889 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4890 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4891 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4892 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4893 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4894 ffd1d5e5 2018-06-23 stsp free(line);
4895 2550e4c3 2018-07-13 stsp free(wline);
4896 2550e4c3 2018-07-13 stsp wline = NULL;
4898 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4899 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4900 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4904 ffd1d5e5 2018-06-23 stsp return err;
4907 ffd1d5e5 2018-06-23 stsp static void
4908 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4909 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4910 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int isroot)
4912 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4915 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4918 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, 0);
4919 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4920 ffd1d5e5 2018-06-23 stsp if (!isroot)
4921 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4926 56e0773d 2019-11-28 stsp while (*first_displayed_entry && i < maxscroll) {
4927 56e0773d 2019-11-28 stsp *first_displayed_entry = got_tree_entry_get_prev(tree,
4928 56e0773d 2019-11-28 stsp *first_displayed_entry);
4931 56e0773d 2019-11-28 stsp if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4932 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4935 768394f3 2019-01-24 stsp static int
4936 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4937 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4938 56e0773d 2019-11-28 stsp struct got_tree_object *tree)
4940 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4941 ffd1d5e5 2018-06-23 stsp int n = 0;
4943 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4944 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, *first_displayed_entry);
4946 56e0773d 2019-11-28 stsp next = got_object_tree_get_first_entry(tree);
4948 768394f3 2019-01-24 stsp last = last_displayed_entry;
4949 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4950 56e0773d 2019-11-28 stsp last = got_tree_entry_get_next(tree, last);
4951 768394f3 2019-01-24 stsp if (last) {
4952 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4953 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, next);
4959 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4960 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4961 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4963 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4964 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4965 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4967 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4968 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4969 56e0773d 2019-11-28 stsp + 1 /* slash */;
4971 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4973 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4974 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4975 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4977 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4978 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4979 d9765a41 2018-06-23 stsp while (pt) {
4980 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4981 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4982 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4983 cb2ebc8a 2018-06-23 stsp goto done;
4985 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4986 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4987 cb2ebc8a 2018-06-23 stsp goto done;
4989 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4992 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4993 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4994 ce52c690 2018-06-23 stsp goto done;
4998 ce52c690 2018-06-23 stsp if (err) {
4999 ce52c690 2018-06-23 stsp free(*path);
5000 ce52c690 2018-06-23 stsp *path = NULL;
5002 ce52c690 2018-06-23 stsp return err;
5005 ce52c690 2018-06-23 stsp static const struct got_error *
5006 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5007 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5008 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
5009 8b473291 2019-02-21 stsp struct got_repository *repo)
5011 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5012 ce52c690 2018-06-23 stsp char *path;
5013 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5015 a0de39f3 2019-08-09 stsp *new_view = NULL;
5017 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5019 ce52c690 2018-06-23 stsp return err;
5021 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5022 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5023 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5024 83ce39e3 2019-08-12 stsp goto done;
5027 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
5028 e5a0f69f 2018-08-18 stsp if (err) {
5029 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5030 fc06ba56 2019-08-22 stsp err = NULL;
5031 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5033 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5035 83ce39e3 2019-08-12 stsp free(path);
5036 69efd4c4 2018-07-18 stsp return err;
5039 69efd4c4 2018-07-18 stsp static const struct got_error *
5040 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
5041 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5042 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
5043 8b473291 2019-02-21 stsp struct got_repository *repo)
5045 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5046 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5047 69efd4c4 2018-07-18 stsp char *path;
5049 a0de39f3 2019-08-09 stsp *new_view = NULL;
5051 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5052 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5053 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5055 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
5057 69efd4c4 2018-07-18 stsp return err;
5059 f135c941 2020-02-20 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
5061 e5a0f69f 2018-08-18 stsp view_close(log_view);
5063 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5064 cb2ebc8a 2018-06-23 stsp free(path);
5065 cb2ebc8a 2018-06-23 stsp return err;
5068 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5069 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
5070 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
5071 8b473291 2019-02-21 stsp struct got_repository *repo)
5073 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5074 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5075 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5077 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5079 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5080 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5081 ffd1d5e5 2018-06-23 stsp goto done;
5083 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5084 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5085 ffd1d5e5 2018-06-23 stsp goto done;
5088 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
5089 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5090 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5091 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
5092 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
5093 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5094 6484ec90 2018-09-29 stsp goto done;
5096 8b473291 2019-02-21 stsp s->refs = refs;
5097 fb2756b9 2018-08-04 stsp s->repo = repo;
5099 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
5101 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5102 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5103 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5104 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5106 c0b01bdb 2019-11-08 stsp goto done;
5107 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5108 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5109 c0b01bdb 2019-11-08 stsp if (err) {
5110 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5111 c0b01bdb 2019-11-08 stsp goto done;
5113 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5114 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5115 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5116 c0b01bdb 2019-11-08 stsp if (err) {
5117 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5118 c0b01bdb 2019-11-08 stsp goto done;
5121 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5122 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5123 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5124 11b20872 2019-11-08 stsp if (err) {
5125 11b20872 2019-11-08 stsp free_colors(&s->colors);
5126 11b20872 2019-11-08 stsp goto done;
5129 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5130 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5131 c0b01bdb 2019-11-08 stsp if (err) {
5132 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5133 c0b01bdb 2019-11-08 stsp goto done;
5137 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5138 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5139 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5140 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5141 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5143 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5144 6484ec90 2018-09-29 stsp if (err) {
5145 fb2756b9 2018-08-04 stsp free(s->tree_label);
5146 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5148 ad80ab7b 2018-08-04 stsp return err;
5151 e5a0f69f 2018-08-18 stsp static const struct got_error *
5152 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5154 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5156 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5157 fb2756b9 2018-08-04 stsp free(s->tree_label);
5158 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5159 6484ec90 2018-09-29 stsp free(s->commit_id);
5160 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5161 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5162 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5163 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5164 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5165 ad80ab7b 2018-08-04 stsp free(parent);
5168 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
5169 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5170 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
5172 7c32bd05 2019-06-22 stsp return NULL;
5175 7c32bd05 2019-06-22 stsp static const struct got_error *
5176 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5178 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5180 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5181 7c32bd05 2019-06-22 stsp return NULL;
5184 7c32bd05 2019-06-22 stsp static int
5185 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5187 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5189 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5193 7c32bd05 2019-06-22 stsp static const struct got_error *
5194 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5196 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5197 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5199 7c32bd05 2019-06-22 stsp if (!view->searching) {
5200 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5201 7c32bd05 2019-06-22 stsp return NULL;
5204 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5205 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5206 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5207 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5208 56e0773d 2019-11-28 stsp s->selected_entry);
5210 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5212 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5213 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5215 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5216 56e0773d 2019-11-28 stsp s->selected_entry);
5219 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5220 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5222 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5225 7c32bd05 2019-06-22 stsp while (1) {
5226 56e0773d 2019-11-28 stsp if (te == NULL) {
5227 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5228 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5229 ac66afb8 2019-06-24 stsp return NULL;
5231 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5232 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5234 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5237 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5238 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5239 56e0773d 2019-11-28 stsp s->matched_entry = te;
5243 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5244 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5246 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5249 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5250 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5251 7c32bd05 2019-06-22 stsp s->selected = 0;
5254 e5a0f69f 2018-08-18 stsp return NULL;
5257 ad80ab7b 2018-08-04 stsp static const struct got_error *
5258 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5260 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5261 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5262 e5a0f69f 2018-08-18 stsp char *parent_path;
5264 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5266 e5a0f69f 2018-08-18 stsp return err;
5268 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
5269 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
5270 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5271 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
5272 0d6c6ee3 2020-05-20 stsp &s->colors, s->repo);
5273 e5a0f69f 2018-08-18 stsp free(parent_path);
5275 669b5ffa 2018-10-07 stsp view_vborder(view);
5276 e5a0f69f 2018-08-18 stsp return err;
5279 e5a0f69f 2018-08-18 stsp static const struct got_error *
5280 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5281 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
5283 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5284 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5285 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
5286 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
5288 e5a0f69f 2018-08-18 stsp switch (ch) {
5290 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5293 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5295 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5296 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5297 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
5298 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
5299 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
5300 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5301 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5303 1e37a5c2 2019-05-12 jcs return err;
5304 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
5306 1e37a5c2 2019-05-12 jcs view_close(log_view);
5309 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
5310 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
5312 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5315 1e37a5c2 2019-05-12 jcs case KEY_UP:
5316 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5317 1e37a5c2 2019-05-12 jcs s->selected--;
5318 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
5321 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
5323 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
5324 56e0773d 2019-11-28 stsp s->tree, s->tree == s->root);
5326 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5327 ea025d1d 2020-02-22 naddy case CTRL('b'):
5328 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
5329 56e0773d 2019-11-28 stsp MAX(0, view->nlines - 4 - s->selected), s->tree,
5330 1e37a5c2 2019-05-12 jcs s->tree == s->root);
5331 1e37a5c2 2019-05-12 jcs s->selected = 0;
5332 56e0773d 2019-11-28 stsp if (got_object_tree_get_first_entry(s->tree) ==
5333 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
5334 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
5337 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5338 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5339 1e37a5c2 2019-05-12 jcs s->selected++;
5342 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5344 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5346 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
5347 56e0773d 2019-11-28 stsp s->last_displayed_entry, s->tree);
5349 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5350 ea025d1d 2020-02-22 naddy case CTRL('f'):
5351 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5353 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5354 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5355 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5358 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
5359 56e0773d 2019-11-28 stsp view->nlines, s->last_displayed_entry, s->tree);
5360 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
5361 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
5362 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
5363 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
5365 1e37a5c2 2019-05-12 jcs ndisplayed++;
5366 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5367 1e37a5c2 2019-05-12 jcs } while (te);
5368 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
5371 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5373 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5374 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5375 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5376 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5377 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5379 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5380 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5382 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5383 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5384 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5385 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5386 1e37a5c2 2019-05-12 jcs s->selected_entry =
5387 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5388 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5389 1e37a5c2 2019-05-12 jcs free(parent);
5390 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5391 56e0773d 2019-11-28 stsp s->selected_entry))) {
5392 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5393 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5394 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5397 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
5398 941e9f74 2019-05-21 stsp if (err) {
5399 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5402 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5403 56e0773d 2019-11-28 stsp s->selected_entry))) {
5404 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5405 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5406 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5408 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5409 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5410 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
5413 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5414 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5416 669b5ffa 2018-10-07 stsp return err;
5417 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
5418 669b5ffa 2018-10-07 stsp if (err) {
5419 1e37a5c2 2019-05-12 jcs view_close(blame_view);
5422 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
5423 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
5425 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5428 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5429 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
5430 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5436 ffd1d5e5 2018-06-23 stsp return err;
5439 ffd1d5e5 2018-06-23 stsp __dead static void
5440 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5443 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5444 ffd1d5e5 2018-06-23 stsp getprogname());
5448 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5449 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5451 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5452 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5453 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5454 8b473291 2019-02-21 stsp struct got_reflist_head refs;
5455 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5456 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5457 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
5458 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5459 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5461 5221c383 2018-08-01 stsp struct tog_view *view;
5463 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
5465 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
5466 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5467 d188b9a6 2019-01-04 stsp NULL) == -1)
5468 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
5471 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5472 ffd1d5e5 2018-06-23 stsp switch (ch) {
5474 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5477 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5478 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5479 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5483 17020d27 2019-03-07 stsp usage_tree();
5484 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5488 ffd1d5e5 2018-06-23 stsp argc -= optind;
5489 ffd1d5e5 2018-06-23 stsp argv += optind;
5491 55cccc34 2020-02-20 stsp if (argc > 1)
5492 74283ab8 2020-02-07 stsp usage_tree();
5494 55cccc34 2020-02-20 stsp cwd = getcwd(NULL, 0);
5495 55cccc34 2020-02-20 stsp if (cwd == NULL)
5496 55cccc34 2020-02-20 stsp return got_error_from_errno("getcwd");
5498 55cccc34 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
5499 55cccc34 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5500 55cccc34 2020-02-20 stsp goto done;
5502 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5503 55cccc34 2020-02-20 stsp if (worktree)
5504 52185f70 2019-02-05 stsp repo_path =
5505 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5507 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5509 55cccc34 2020-02-20 stsp if (repo_path == NULL) {
5510 55cccc34 2020-02-20 stsp error = got_error_from_errno("strdup");
5511 55cccc34 2020-02-20 stsp goto done;
5514 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5515 c02c541e 2019-03-29 stsp if (error != NULL)
5516 52185f70 2019-02-05 stsp goto done;
5518 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5519 55cccc34 2020-02-20 stsp repo, worktree);
5520 55cccc34 2020-02-20 stsp if (error)
5521 55cccc34 2020-02-20 stsp goto done;
5523 55cccc34 2020-02-20 stsp init_curses();
5525 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5526 c02c541e 2019-03-29 stsp if (error)
5527 52185f70 2019-02-05 stsp goto done;
5529 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5530 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5531 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5532 55cccc34 2020-02-20 stsp if (error)
5533 ffd1d5e5 2018-06-23 stsp goto done;
5535 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5536 55cccc34 2020-02-20 stsp if (error)
5537 ffd1d5e5 2018-06-23 stsp goto done;
5539 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5540 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5541 55cccc34 2020-02-20 stsp if (error)
5542 8b473291 2019-02-21 stsp goto done;
5544 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5545 8b473291 2019-02-21 stsp if (error)
5546 ffd1d5e5 2018-06-23 stsp goto done;
5548 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5549 5221c383 2018-08-01 stsp if (view == NULL) {
5550 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5551 5221c383 2018-08-01 stsp goto done;
5553 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
5554 ad80ab7b 2018-08-04 stsp if (error)
5555 ad80ab7b 2018-08-04 stsp goto done;
5556 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5557 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5558 55cccc34 2020-02-20 stsp in_repo_path, repo);
5559 55cccc34 2020-02-20 stsp if (error)
5560 55cccc34 2020-02-20 stsp goto done;
5563 55cccc34 2020-02-20 stsp if (worktree) {
5564 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5565 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5566 55cccc34 2020-02-20 stsp worktree = NULL;
5568 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5570 52185f70 2019-02-05 stsp free(repo_path);
5571 e4a0e26d 2020-02-20 stsp free(cwd);
5572 ffd1d5e5 2018-06-23 stsp free(commit_id);
5573 ffd1d5e5 2018-06-23 stsp if (commit)
5574 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5576 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5578 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5579 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5580 ffd1d5e5 2018-06-23 stsp return error;
5583 ce5b7c56 2019-07-09 stsp static void
5584 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
5588 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
5589 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
5590 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
5591 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
5593 6879ba42 2020-10-01 naddy fputc('\n', fp);
5596 4ed7e80c 2018-05-20 stsp __dead static void
5597 6879ba42 2020-10-01 naddy usage(int hflag, int status)
5599 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
5601 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5602 6879ba42 2020-10-01 naddy getprogname());
5603 6879ba42 2020-10-01 naddy if (hflag) {
5604 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
5605 6879ba42 2020-10-01 naddy list_commands(fp);
5607 6879ba42 2020-10-01 naddy exit(status);
5610 c2301be8 2018-04-30 stsp static char **
5611 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
5613 ee85c5e8 2020-02-29 stsp va_list ap;
5614 c2301be8 2018-04-30 stsp char **argv;
5617 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
5619 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
5620 c2301be8 2018-04-30 stsp if (argv == NULL)
5621 c2301be8 2018-04-30 stsp err(1, "calloc");
5622 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
5623 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
5624 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
5625 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5628 ee85c5e8 2020-02-29 stsp va_end(ap);
5629 c2301be8 2018-04-30 stsp return argv;
5633 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
5634 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
5635 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
5636 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
5637 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
5639 ee85c5e8 2020-02-29 stsp static const struct got_error *
5640 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
5642 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
5643 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
5644 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
5645 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
5646 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
5647 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5648 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
5650 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
5651 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
5652 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
5654 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
5655 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5656 ee85c5e8 2020-02-29 stsp goto done;
5658 ee85c5e8 2020-02-29 stsp if (worktree)
5659 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
5661 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
5662 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
5663 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
5664 ee85c5e8 2020-02-29 stsp goto done;
5667 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
5668 ee85c5e8 2020-02-29 stsp if (error != NULL)
5669 ee85c5e8 2020-02-29 stsp goto done;
5671 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5672 ee85c5e8 2020-02-29 stsp repo, worktree);
5673 ee85c5e8 2020-02-29 stsp if (error)
5674 ee85c5e8 2020-02-29 stsp goto done;
5676 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5677 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5678 ee85c5e8 2020-02-29 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5679 ee85c5e8 2020-02-29 stsp if (error)
5680 ee85c5e8 2020-02-29 stsp goto done;
5682 ee85c5e8 2020-02-29 stsp if (worktree) {
5683 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
5684 ee85c5e8 2020-02-29 stsp worktree = NULL;
5687 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5688 ee85c5e8 2020-02-29 stsp if (error) {
5689 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
5690 ee85c5e8 2020-02-29 stsp goto done;
5691 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
5692 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
5693 6879ba42 2020-10-01 naddy usage(1, 1);
5694 ee85c5e8 2020-02-29 stsp /* not reached */
5697 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
5698 ee85c5e8 2020-02-29 stsp repo = NULL;
5700 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
5701 ee85c5e8 2020-02-29 stsp if (error)
5702 ee85c5e8 2020-02-29 stsp goto done;
5704 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
5706 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5707 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
5710 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
5711 ee85c5e8 2020-02-29 stsp if (worktree)
5712 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
5714 ee85c5e8 2020-02-29 stsp free(commit_id_str);
5715 ee85c5e8 2020-02-29 stsp free(commit_id);
5716 ee85c5e8 2020-02-29 stsp free(cwd);
5717 ee85c5e8 2020-02-29 stsp free(repo_path);
5718 ee85c5e8 2020-02-29 stsp free(in_repo_path);
5719 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
5721 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
5722 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
5723 ee85c5e8 2020-02-29 stsp free(cmd_argv);
5725 ee85c5e8 2020-02-29 stsp return error;
5729 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
5731 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
5732 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
5733 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
5734 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
5735 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
5736 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
5737 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
5740 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
5742 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5743 9f7d7167 2018-04-29 stsp switch (ch) {
5745 9f7d7167 2018-04-29 stsp hflag = 1;
5748 53ccebc2 2019-07-30 stsp Vflag = 1;
5751 6879ba42 2020-10-01 naddy usage(hflag, 1);
5752 9f7d7167 2018-04-29 stsp /* NOTREACHED */
5756 9f7d7167 2018-04-29 stsp argc -= optind;
5757 9f7d7167 2018-04-29 stsp argv += optind;
5758 9814e6a3 2020-09-27 naddy optind = 1;
5759 c2301be8 2018-04-30 stsp optreset = 1;
5761 53ccebc2 2019-07-30 stsp if (Vflag) {
5762 53ccebc2 2019-07-30 stsp got_version_print_str();
5763 6879ba42 2020-10-01 naddy return 0;
5766 c2301be8 2018-04-30 stsp if (argc == 0) {
5767 f29d3e89 2018-06-23 stsp if (hflag)
5768 6879ba42 2020-10-01 naddy usage(hflag, 0);
5769 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
5770 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
5772 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
5776 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
5777 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
5778 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
5779 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
5780 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
5786 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
5787 ee85c5e8 2020-02-29 stsp if (argc != 1)
5788 6879ba42 2020-10-01 naddy usage(0, 1);
5789 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
5790 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
5792 ee85c5e8 2020-02-29 stsp if (hflag)
5793 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
5795 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5799 b46c1e04 2020-09-20 naddy putchar('\n');
5800 a2f4a359 2020-02-28 stsp if (cmd_argv) {
5802 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
5803 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
5804 a2f4a359 2020-02-28 stsp free(cmd_argv);
5807 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
5808 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);