2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 31120ada 2018-04-30 stsp #include <errno.h>
22 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
23 9f7d7167 2018-04-29 stsp #include <curses.h>
24 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 61266923 2020-01-14 stsp #include <signal.h>
28 9f7d7167 2018-04-29 stsp #include <stdlib.h>
29 26ed57b2 2018-05-19 stsp #include <stdio.h>
30 9f7d7167 2018-04-29 stsp #include <getopt.h>
31 9f7d7167 2018-04-29 stsp #include <string.h>
32 9f7d7167 2018-04-29 stsp #include <err.h>
33 80ddbec8 2018-04-29 stsp #include <unistd.h>
34 26ed57b2 2018-05-19 stsp #include <util.h>
35 26ed57b2 2018-05-19 stsp #include <limits.h>
36 61e69b96 2018-05-20 stsp #include <wchar.h>
37 788c352e 2018-06-16 stsp #include <time.h>
38 84451b3e 2018-07-10 stsp #include <pthread.h>
39 5036bf37 2018-09-24 stsp #include <libgen.h>
40 60493ae3 2019-06-20 stsp #include <regex.h>
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 9f7d7167 2018-04-29 stsp #include "got_error.h"
44 80ddbec8 2018-04-29 stsp #include "got_object.h"
45 80ddbec8 2018-04-29 stsp #include "got_reference.h"
46 80ddbec8 2018-04-29 stsp #include "got_repository.h"
47 80ddbec8 2018-04-29 stsp #include "got_diff.h"
48 511a516b 2018-05-19 stsp #include "got_opentemp.h"
49 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
50 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
51 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
52 a70480e0 2018-06-23 stsp #include "got_blame.h"
53 c2db6724 2019-01-04 stsp #include "got_privsep.h"
54 1dd54920 2019-05-11 stsp #include "got_path.h"
55 b7165be3 2019-02-05 stsp #include "got_worktree.h"
58 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
67 9f7d7167 2018-04-29 stsp #ifndef nitems
68 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 9f7d7167 2018-04-29 stsp struct tog_cmd {
72 c2301be8 2018-04-30 stsp const char *name;
73 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
74 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
77 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
78 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
79 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
81 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
83 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
84 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
85 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
86 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
89 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
90 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
91 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
92 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
95 d6b05b5b 2018-08-04 stsp enum tog_view_type {
96 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
97 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
98 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
99 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
102 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
104 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
105 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
106 ba4f502b 2018-08-04 stsp struct got_object_id *id;
107 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
110 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
111 ba4f502b 2018-08-04 stsp struct commit_queue {
112 ba4f502b 2018-08-04 stsp int ncommits;
113 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
116 f26dddb7 2019-11-08 stsp struct tog_color {
117 f26dddb7 2019-11-08 stsp SIMPLEQ_ENTRY(tog_color) entry;
118 6d17833f 2019-11-08 stsp regex_t regex;
119 6d17833f 2019-11-08 stsp short colorpair;
121 f26dddb7 2019-11-08 stsp SIMPLEQ_HEAD(tog_colors, tog_color);
123 11b20872 2019-11-08 stsp static const struct got_error *
124 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
125 11b20872 2019-11-08 stsp int idx, short color)
127 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
128 11b20872 2019-11-08 stsp struct tog_color *tc;
129 11b20872 2019-11-08 stsp int regerr = 0;
131 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
132 11b20872 2019-11-08 stsp return NULL;
134 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
136 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
137 11b20872 2019-11-08 stsp if (tc == NULL)
138 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
139 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
140 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
141 11b20872 2019-11-08 stsp if (regerr) {
142 11b20872 2019-11-08 stsp static char regerr_msg[512];
143 11b20872 2019-11-08 stsp static char err_msg[512];
144 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
145 11b20872 2019-11-08 stsp sizeof(regerr_msg));
146 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
147 11b20872 2019-11-08 stsp regerr_msg);
148 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
150 11b20872 2019-11-08 stsp return err;
152 11b20872 2019-11-08 stsp tc->colorpair = idx;
153 11b20872 2019-11-08 stsp SIMPLEQ_INSERT_HEAD(colors, tc, entry);
154 11b20872 2019-11-08 stsp return NULL;
157 11b20872 2019-11-08 stsp static void
158 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
160 11b20872 2019-11-08 stsp struct tog_color *tc;
162 11b20872 2019-11-08 stsp while (!SIMPLEQ_EMPTY(colors)) {
163 11b20872 2019-11-08 stsp tc = SIMPLEQ_FIRST(colors);
164 11b20872 2019-11-08 stsp SIMPLEQ_REMOVE_HEAD(colors, entry);
165 11b20872 2019-11-08 stsp regfree(&tc->regex);
170 11b20872 2019-11-08 stsp struct tog_color *
171 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
173 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
175 11b20872 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
176 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
180 11b20872 2019-11-08 stsp return NULL;
184 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
186 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
187 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
188 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
189 11b20872 2019-11-08 stsp return COLOR_CYAN;
190 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
191 11b20872 2019-11-08 stsp return COLOR_YELLOW;
192 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
193 11b20872 2019-11-08 stsp return COLOR_GREEN;
194 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
195 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
196 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
197 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
198 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
199 91b8c405 2020-01-25 stsp return COLOR_CYAN;
200 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
201 11b20872 2019-11-08 stsp return COLOR_GREEN;
202 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
203 11b20872 2019-11-08 stsp return COLOR_GREEN;
204 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
205 11b20872 2019-11-08 stsp return COLOR_CYAN;
206 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
207 11b20872 2019-11-08 stsp return COLOR_YELLOW;
213 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
215 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
217 11b20872 2019-11-08 stsp if (val == NULL)
218 11b20872 2019-11-08 stsp return default_color_value(envvar);
220 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
221 11b20872 2019-11-08 stsp return COLOR_BLACK;
222 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
223 11b20872 2019-11-08 stsp return COLOR_RED;
224 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
225 11b20872 2019-11-08 stsp return COLOR_GREEN;
226 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
227 11b20872 2019-11-08 stsp return COLOR_YELLOW;
228 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
229 11b20872 2019-11-08 stsp return COLOR_BLUE;
230 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
231 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
232 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
233 11b20872 2019-11-08 stsp return COLOR_CYAN;
234 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
235 11b20872 2019-11-08 stsp return COLOR_WHITE;
236 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
239 11b20872 2019-11-08 stsp return default_color_value(envvar);
243 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
244 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
246 15a087fe 2019-02-21 stsp int first_displayed_line;
247 15a087fe 2019-02-21 stsp int last_displayed_line;
249 15a087fe 2019-02-21 stsp int diff_context;
250 15a087fe 2019-02-21 stsp struct got_repository *repo;
251 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
252 bddb1296 2019-11-08 stsp struct tog_colors colors;
253 f44b1f58 2020-02-02 tracey int nlines;
254 f44b1f58 2020-02-02 tracey off_t *line_offsets;
255 f44b1f58 2020-02-02 tracey int matched_line;
256 f44b1f58 2020-02-02 tracey int selected_line;
257 f44b1f58 2020-02-02 tracey size_t filesize;
259 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
260 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
263 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
265 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
266 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
267 1a76625f 2018-10-22 stsp int commits_needed;
268 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
269 1a76625f 2018-10-22 stsp struct commit_queue *commits;
270 1a76625f 2018-10-22 stsp const char *in_repo_path;
271 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
272 1a76625f 2018-10-22 stsp struct got_repository *repo;
273 1a76625f 2018-10-22 stsp int log_complete;
274 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
275 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
276 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
277 13add988 2019-10-15 stsp int *searching;
278 13add988 2019-10-15 stsp int *search_next_done;
279 13add988 2019-10-15 stsp regex_t *regex;
282 1a76625f 2018-10-22 stsp struct tog_log_view_state {
283 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
284 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
285 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
286 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
287 b01e7d3b 2018-08-04 stsp int selected;
288 b01e7d3b 2018-08-04 stsp char *in_repo_path;
289 d01904d4 2019-06-25 stsp const char *head_ref_name;
290 b672a97a 2020-01-27 stsp int log_branches;
291 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
292 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
293 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
294 1a76625f 2018-10-22 stsp sig_atomic_t quit;
295 1a76625f 2018-10-22 stsp pthread_t thread;
296 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
297 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
298 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
299 11b20872 2019-11-08 stsp struct tog_colors colors;
302 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
303 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
304 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
305 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
306 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
307 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
308 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
309 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
310 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
311 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
312 11b20872 2019-11-08 stsp #define TOG_COLOR_DATE 11
314 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
315 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
316 e9424729 2018-08-04 stsp int nlines;
318 e9424729 2018-08-04 stsp struct tog_view *view;
319 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
323 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
324 e9424729 2018-08-04 stsp const char *path;
325 e9424729 2018-08-04 stsp struct got_repository *repo;
326 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
327 e9424729 2018-08-04 stsp int *complete;
328 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
329 fc06ba56 2019-08-22 stsp void *cancel_arg;
332 e9424729 2018-08-04 stsp struct tog_blame {
334 e9424729 2018-08-04 stsp size_t filesize;
335 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
336 6fcac457 2018-11-19 stsp int nlines;
337 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
338 e9424729 2018-08-04 stsp pthread_t thread;
339 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
340 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
341 e9424729 2018-08-04 stsp const char *path;
344 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
345 7cbe629d 2018-08-04 stsp int first_displayed_line;
346 7cbe629d 2018-08-04 stsp int last_displayed_line;
347 7cbe629d 2018-08-04 stsp int selected_line;
348 7cbe629d 2018-08-04 stsp int blame_complete;
351 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
352 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
353 e5a0f69f 2018-08-18 stsp char *path;
354 7cbe629d 2018-08-04 stsp struct got_repository *repo;
355 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
356 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
357 e9424729 2018-08-04 stsp struct tog_blame blame;
358 6c4c42e0 2019-06-24 stsp int matched_line;
359 11b20872 2019-11-08 stsp struct tog_colors colors;
362 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
363 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
364 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
365 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
366 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
367 ad80ab7b 2018-08-04 stsp int selected;
370 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
372 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
373 ad80ab7b 2018-08-04 stsp char *tree_label;
374 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
375 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
376 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
377 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
378 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
379 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
380 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
381 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
382 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
383 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
384 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
385 bddb1296 2019-11-08 stsp struct tog_colors colors;
389 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
391 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
392 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
393 669b5ffa 2018-10-07 stsp * there is enough screen estate.
395 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
396 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
398 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
399 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
400 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
402 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
403 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
405 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
407 cc3c9aac 2018-08-01 stsp struct tog_view {
408 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
409 26ed57b2 2018-05-19 stsp WINDOW *window;
410 26ed57b2 2018-05-19 stsp PANEL *panel;
411 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
412 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
413 1004088d 2018-09-29 stsp int focussed;
414 669b5ffa 2018-10-07 stsp struct tog_view *parent;
415 669b5ffa 2018-10-07 stsp struct tog_view *child;
416 669b5ffa 2018-10-07 stsp int child_focussed;
418 5dc9f4bc 2018-08-04 stsp /* type-specific state */
419 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
421 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
422 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
423 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
424 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
427 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
428 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
429 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
430 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
432 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
433 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
434 60493ae3 2019-06-20 stsp int searching;
435 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
436 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
437 60493ae3 2019-06-20 stsp int search_next_done;
438 1803e47f 2019-06-22 stsp regex_t regex;
441 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
442 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
443 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
444 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
445 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
446 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
447 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
448 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
449 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
451 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
452 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
453 b672a97a 2020-01-27 stsp struct got_repository *, const char *, const char *, int, int);
454 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
455 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
456 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
457 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
458 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
459 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
461 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
462 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
463 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
464 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
465 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
466 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
467 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
468 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
470 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
471 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
472 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
473 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
474 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
475 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
476 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
477 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
478 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
480 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
481 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
482 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
484 25791caa 2018-10-24 stsp static void
485 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
487 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
490 83baff54 2019-08-12 stsp static void
491 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
493 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
496 61266923 2020-01-14 stsp static void
497 61266923 2020-01-14 stsp tog_sigcont(int signo)
499 61266923 2020-01-14 stsp tog_sigcont_received = 1;
502 e5a0f69f 2018-08-18 stsp static const struct got_error *
503 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
505 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
507 669b5ffa 2018-10-07 stsp if (view->child) {
508 669b5ffa 2018-10-07 stsp view_close(view->child);
509 669b5ffa 2018-10-07 stsp view->child = NULL;
511 e5a0f69f 2018-08-18 stsp if (view->close)
512 e5a0f69f 2018-08-18 stsp err = view->close(view);
513 ea5e7bb5 2018-08-01 stsp if (view->panel)
514 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
515 ea5e7bb5 2018-08-01 stsp if (view->window)
516 ea5e7bb5 2018-08-01 stsp delwin(view->window);
517 ea5e7bb5 2018-08-01 stsp free(view);
518 e5a0f69f 2018-08-18 stsp return err;
521 ea5e7bb5 2018-08-01 stsp static struct tog_view *
522 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
523 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
525 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
527 ea5e7bb5 2018-08-01 stsp if (view == NULL)
528 ea5e7bb5 2018-08-01 stsp return NULL;
530 d6b05b5b 2018-08-04 stsp view->type = type;
531 f7d12f7e 2018-08-01 stsp view->lines = LINES;
532 f7d12f7e 2018-08-01 stsp view->cols = COLS;
533 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
534 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
535 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
536 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
537 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
538 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
539 96a765a8 2018-08-04 stsp view_close(view);
540 ea5e7bb5 2018-08-01 stsp return NULL;
542 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
543 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
544 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
545 96a765a8 2018-08-04 stsp view_close(view);
546 ea5e7bb5 2018-08-01 stsp return NULL;
549 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
550 ea5e7bb5 2018-08-01 stsp return view;
554 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
556 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
558 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
561 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
563 5c60c32a 2018-10-18 stsp static const struct got_error *
564 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
566 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
568 5c60c32a 2018-10-18 stsp view->begin_y = 0;
569 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
570 5c60c32a 2018-10-18 stsp view->nlines = LINES;
571 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
572 5c60c32a 2018-10-18 stsp view->lines = LINES;
573 5c60c32a 2018-10-18 stsp view->cols = COLS;
574 5c60c32a 2018-10-18 stsp err = view_resize(view);
576 5c60c32a 2018-10-18 stsp return err;
578 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
579 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
581 5c60c32a 2018-10-18 stsp return NULL;
584 5c60c32a 2018-10-18 stsp static const struct got_error *
585 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
587 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
589 5c60c32a 2018-10-18 stsp view->begin_x = 0;
590 5c60c32a 2018-10-18 stsp view->begin_y = 0;
591 5c60c32a 2018-10-18 stsp view->nlines = LINES;
592 5c60c32a 2018-10-18 stsp view->ncols = COLS;
593 5c60c32a 2018-10-18 stsp view->lines = LINES;
594 5c60c32a 2018-10-18 stsp view->cols = COLS;
595 5c60c32a 2018-10-18 stsp err = view_resize(view);
597 5c60c32a 2018-10-18 stsp return err;
599 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
600 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
602 5c60c32a 2018-10-18 stsp return NULL;
606 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
608 5c60c32a 2018-10-18 stsp return view->parent == NULL;
611 4d8c2215 2018-08-19 stsp static const struct got_error *
612 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
614 f7d12f7e 2018-08-01 stsp int nlines, ncols;
616 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
617 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
619 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
621 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
622 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
624 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
626 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
627 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
628 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
629 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
630 25791caa 2018-10-24 stsp wclear(view->window);
632 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
633 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
634 0cf4efb1 2018-09-29 stsp view->lines = LINES;
635 0cf4efb1 2018-09-29 stsp view->cols = COLS;
637 6e3e5d9c 2018-10-18 stsp if (view->child) {
638 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
639 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
640 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
641 5c60c32a 2018-10-18 stsp if (view->child->focussed)
642 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
644 5c60c32a 2018-10-18 stsp show_panel(view->panel);
646 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
647 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
651 5c60c32a 2018-10-18 stsp return NULL;
654 669b5ffa 2018-10-07 stsp static const struct got_error *
655 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
657 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
659 669b5ffa 2018-10-07 stsp if (view->child == NULL)
660 669b5ffa 2018-10-07 stsp return NULL;
662 669b5ffa 2018-10-07 stsp err = view_close(view->child);
663 669b5ffa 2018-10-07 stsp view->child = NULL;
664 669b5ffa 2018-10-07 stsp return err;
667 669b5ffa 2018-10-07 stsp static const struct got_error *
668 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
670 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
672 669b5ffa 2018-10-07 stsp view->child = child;
673 669b5ffa 2018-10-07 stsp child->parent = view;
674 669b5ffa 2018-10-07 stsp return err;
678 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
680 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
683 34bc9ec9 2019-02-22 stsp static void
684 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
686 25791caa 2018-10-24 stsp int cols, lines;
687 25791caa 2018-10-24 stsp struct winsize size;
689 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
690 25791caa 2018-10-24 stsp cols = 80; /* Default */
691 25791caa 2018-10-24 stsp lines = 24;
693 25791caa 2018-10-24 stsp cols = size.ws_col;
694 25791caa 2018-10-24 stsp lines = size.ws_row;
696 25791caa 2018-10-24 stsp resize_term(lines, cols);
699 2b49a8ae 2019-06-22 stsp static const struct got_error *
700 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
702 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
703 2b49a8ae 2019-06-22 stsp char pattern[1024];
705 7d049c18 2019-08-21 stsp int begin_x = 0;
707 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
708 2b49a8ae 2019-06-22 stsp return NULL;
710 7d049c18 2019-08-21 stsp if (!view_is_parent_view(view))
711 7d049c18 2019-08-21 stsp begin_x = view_split_begin_x(view->begin_x);
712 2b49a8ae 2019-06-22 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
713 7d049c18 2019-08-21 stsp begin_x, "/");
714 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
716 2b49a8ae 2019-06-22 stsp nocbreak();
718 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
721 2b49a8ae 2019-06-22 stsp if (ret == ERR)
722 2b49a8ae 2019-06-22 stsp return NULL;
724 2b49a8ae 2019-06-22 stsp if (view->searching) {
725 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
726 2b49a8ae 2019-06-22 stsp view->searching = 0;
729 2b49a8ae 2019-06-22 stsp if (regcomp(&view->regex, pattern,
730 f5daf9b1 2019-06-24 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
731 7c32bd05 2019-06-22 stsp err = view->search_start(view);
733 7c32bd05 2019-06-22 stsp regfree(&view->regex);
734 7c32bd05 2019-06-22 stsp return err;
736 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
737 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
738 2b49a8ae 2019-06-22 stsp view->search_next(view);
741 2b49a8ae 2019-06-22 stsp return NULL;
744 0cf4efb1 2018-09-29 stsp static const struct got_error *
745 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
746 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
747 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
749 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
750 669b5ffa 2018-10-07 stsp struct tog_view *v;
751 1a76625f 2018-10-22 stsp int ch, errcode;
753 e5a0f69f 2018-08-18 stsp *new = NULL;
754 e5a0f69f 2018-08-18 stsp *dead = NULL;
755 0cf4efb1 2018-09-29 stsp *focus = NULL;
757 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
758 60493ae3 2019-06-20 stsp view->search_next(view);
759 60493ae3 2019-06-20 stsp return NULL;
762 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
763 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
764 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
765 1a76625f 2018-10-22 stsp if (errcode)
766 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
767 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
768 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
769 1a76625f 2018-10-22 stsp if (errcode)
770 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
771 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
773 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
774 25791caa 2018-10-24 stsp tog_resizeterm();
775 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
776 61266923 2020-01-14 stsp tog_sigcont_received = 0;
777 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
778 25791caa 2018-10-24 stsp err = view_resize(v);
780 25791caa 2018-10-24 stsp return err;
781 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
783 25791caa 2018-10-24 stsp return err;
787 e5a0f69f 2018-08-18 stsp switch (ch) {
791 1e37a5c2 2019-05-12 jcs if (view->child) {
792 1e37a5c2 2019-05-12 jcs *focus = view->child;
793 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
794 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
795 1e37a5c2 2019-05-12 jcs *focus = view->parent;
796 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
800 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
801 1e37a5c2 2019-05-12 jcs *dead = view;
807 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
808 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
810 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
811 669b5ffa 2018-10-07 stsp *focus = view->child;
812 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
813 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
815 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
818 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
819 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
821 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
822 1e37a5c2 2019-05-12 jcs *focus = view;
823 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
824 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
826 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
830 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
831 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
834 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
837 60493ae3 2019-06-20 stsp if (view->search_start)
838 2b49a8ae 2019-06-22 stsp view_search_start(view);
840 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
844 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
845 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
846 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
847 60493ae3 2019-06-20 stsp view->search_next_done = 0;
848 60493ae3 2019-06-20 stsp view->search_next(view);
850 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
853 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
857 e5a0f69f 2018-08-18 stsp return err;
861 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
863 0cf4efb1 2018-09-29 stsp PANEL *panel;
864 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
866 669b5ffa 2018-10-07 stsp if (view->parent)
867 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
869 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
870 0cf4efb1 2018-09-29 stsp if (panel == NULL)
873 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
874 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
875 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
879 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
881 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
882 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
884 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
886 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
889 669b5ffa 2018-10-07 stsp return view->focussed;
892 bcbd79e2 2018-08-19 stsp static const struct got_error *
893 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
895 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
896 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
897 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
898 fd823528 2018-10-22 stsp int fast_refresh = 10;
899 1a76625f 2018-10-22 stsp int done = 0, errcode;
901 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
902 1a76625f 2018-10-22 stsp if (errcode)
903 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
905 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
906 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
908 a81bf10d 2018-09-29 stsp main_view = view;
909 1004088d 2018-09-29 stsp view->focussed = 1;
910 878940b7 2018-09-29 stsp err = view->show(view);
912 0cf4efb1 2018-09-29 stsp return err;
913 0cf4efb1 2018-09-29 stsp update_panels();
914 0cf4efb1 2018-09-29 stsp doupdate();
915 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
916 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
917 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
918 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
920 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
921 e4197bf9 2018-08-18 stsp view, &views);
924 e5a0f69f 2018-08-18 stsp if (dead_view) {
925 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
927 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
928 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
929 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
930 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
931 669b5ffa 2018-10-07 stsp prev = view->parent;
933 669b5ffa 2018-10-07 stsp if (dead_view->parent)
934 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
936 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
938 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
939 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
942 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
943 0cf4efb1 2018-09-29 stsp if (focus_view)
944 0cf4efb1 2018-09-29 stsp view = focus_view;
945 669b5ffa 2018-10-07 stsp else if (prev)
946 669b5ffa 2018-10-07 stsp view = prev;
947 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
948 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
949 0cf4efb1 2018-09-29 stsp tog_view_list_head);
951 0cf4efb1 2018-09-29 stsp view = NULL;
952 669b5ffa 2018-10-07 stsp if (view) {
953 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
954 669b5ffa 2018-10-07 stsp focus_view = view->child;
956 669b5ffa 2018-10-07 stsp focus_view = view;
960 bcbd79e2 2018-08-19 stsp if (new_view) {
961 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
962 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
963 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
964 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
966 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
967 86c66b02 2018-10-18 stsp err = view_close(v);
972 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
973 fed7eaa8 2018-10-24 stsp view = new_view;
974 878940b7 2018-09-29 stsp if (focus_view == NULL)
975 878940b7 2018-09-29 stsp focus_view = new_view;
977 0cf4efb1 2018-09-29 stsp if (focus_view) {
978 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
980 0cf4efb1 2018-09-29 stsp view->focussed = 0;
981 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
982 0cf4efb1 2018-09-29 stsp view = focus_view;
983 878940b7 2018-09-29 stsp if (new_view)
984 878940b7 2018-09-29 stsp show_panel(new_view->panel);
985 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
986 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
988 669b5ffa 2018-10-07 stsp if (view) {
989 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
990 758194b5 2018-10-24 stsp view->focussed = 1;
991 758194b5 2018-10-24 stsp show_panel(view->panel);
992 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
993 758194b5 2018-10-24 stsp show_panel(view->child->panel);
994 1a76625f 2018-10-22 stsp focus_view = view;
996 669b5ffa 2018-10-07 stsp if (view->parent) {
997 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1001 669b5ffa 2018-10-07 stsp err = view->show(view);
1003 1a76625f 2018-10-22 stsp goto done;
1004 669b5ffa 2018-10-07 stsp if (view->child) {
1005 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1007 1a76625f 2018-10-22 stsp goto done;
1009 1a76625f 2018-10-22 stsp update_panels();
1010 1a76625f 2018-10-22 stsp doupdate();
1014 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1015 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1016 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1017 e5a0f69f 2018-08-18 stsp view_close(view);
1020 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1021 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1022 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1024 e5a0f69f 2018-08-18 stsp return err;
1027 4ed7e80c 2018-05-20 stsp __dead static void
1028 9f7d7167 2018-04-29 stsp usage_log(void)
1031 c70c5802 2018-08-01 stsp fprintf(stderr,
1032 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1033 9f7d7167 2018-04-29 stsp getprogname());
1037 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1038 80ddbec8 2018-04-29 stsp static const struct got_error *
1039 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1041 00dfcb92 2018-06-11 stsp char *vis = NULL;
1042 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1044 963b370f 2018-05-20 stsp *ws = NULL;
1045 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1046 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1047 00dfcb92 2018-06-11 stsp int vislen;
1048 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1049 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1051 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1052 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1054 00dfcb92 2018-06-11 stsp return err;
1055 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1056 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1057 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1058 a7f50699 2018-06-11 stsp goto done;
1062 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1063 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1064 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1065 a7f50699 2018-06-11 stsp goto done;
1068 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1069 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1071 00dfcb92 2018-06-11 stsp free(vis);
1072 963b370f 2018-05-20 stsp if (err) {
1073 963b370f 2018-05-20 stsp free(*ws);
1074 963b370f 2018-05-20 stsp *ws = NULL;
1075 963b370f 2018-05-20 stsp *wlen = 0;
1077 963b370f 2018-05-20 stsp return err;
1080 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1081 963b370f 2018-05-20 stsp static const struct got_error *
1082 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1083 27a741e5 2019-09-11 stsp int col_tab_align)
1085 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1086 963b370f 2018-05-20 stsp int cols = 0;
1087 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1088 963b370f 2018-05-20 stsp size_t wlen;
1091 963b370f 2018-05-20 stsp *wlinep = NULL;
1092 b700b5d6 2018-07-10 stsp *widthp = 0;
1094 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1096 963b370f 2018-05-20 stsp return err;
1099 27a741e5 2019-09-11 stsp while (i < wlen) {
1100 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1102 27a741e5 2019-09-11 stsp if (width == 0) {
1107 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1108 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1110 27a741e5 2019-09-11 stsp cols += width;
1112 27a741e5 2019-09-11 stsp } else if (width == -1) {
1113 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1114 27a741e5 2019-09-11 stsp width = TABSIZE -
1115 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1116 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1118 27a741e5 2019-09-11 stsp cols += width;
1122 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1123 963b370f 2018-05-20 stsp goto done;
1126 963b370f 2018-05-20 stsp wline[i] = L'\0';
1127 b700b5d6 2018-07-10 stsp if (widthp)
1128 b700b5d6 2018-07-10 stsp *widthp = cols;
1131 963b370f 2018-05-20 stsp free(wline);
1133 963b370f 2018-05-20 stsp *wlinep = wline;
1134 963b370f 2018-05-20 stsp return err;
1137 8b473291 2019-02-21 stsp static const struct got_error*
1138 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1139 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1141 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1142 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1144 8b473291 2019-02-21 stsp const char *name;
1146 8b473291 2019-02-21 stsp *refs_str = NULL;
1148 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1149 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1152 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1153 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1155 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1156 8b473291 2019-02-21 stsp name += 5;
1157 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1159 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1160 8b473291 2019-02-21 stsp name += 6;
1161 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
1162 8b473291 2019-02-21 stsp name += 8;
1163 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1164 52b5abe1 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1165 5d844a1e 2019-08-13 stsp if (err) {
1166 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1168 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1169 5d844a1e 2019-08-13 stsp err = NULL;
1170 5d844a1e 2019-08-13 stsp tag = NULL;
1173 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1174 52b5abe1 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1176 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1177 52b5abe1 2019-08-13 stsp if (cmp != 0)
1179 8b473291 2019-02-21 stsp s = *refs_str;
1180 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1181 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1184 8b473291 2019-02-21 stsp *refs_str = NULL;
1190 8b473291 2019-02-21 stsp return err;
1193 963b370f 2018-05-20 stsp static const struct got_error *
1194 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1195 27a741e5 2019-09-11 stsp int col_tab_align)
1197 5813d178 2019-03-09 stsp char *smallerthan, *at;
1199 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1200 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1201 5813d178 2019-03-09 stsp author = smallerthan + 1;
1202 5813d178 2019-03-09 stsp at = strchr(author, '@');
1204 5813d178 2019-03-09 stsp *at = '\0';
1205 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1208 5813d178 2019-03-09 stsp static const struct got_error *
1209 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1210 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1211 11b20872 2019-11-08 stsp const size_t date_display_cols, int author_display_cols,
1212 11b20872 2019-11-08 stsp struct tog_colors *colors)
1214 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1215 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1216 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1217 5813d178 2019-03-09 stsp char *author = NULL;
1218 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1219 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1220 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1221 bb737323 2018-05-20 stsp int col, limit;
1222 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1223 ccb26ccd 2018-11-05 stsp struct tm tm;
1224 45d799e2 2018-12-23 stsp time_t committer_time;
1225 11b20872 2019-11-08 stsp struct tog_color *tc;
1227 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1228 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1229 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1230 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1231 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1232 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1234 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1235 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1237 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1238 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_DATE);
1240 11b20872 2019-11-08 stsp wattr_on(view->window,
1241 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1242 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1244 11b20872 2019-11-08 stsp wattr_off(view->window,
1245 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1246 27a741e5 2019-09-11 stsp col = limit;
1247 b39d25c7 2018-07-10 stsp if (col > avail)
1248 b39d25c7 2018-07-10 stsp goto done;
1250 6570a66d 2019-11-08 stsp if (avail >= 120) {
1251 6570a66d 2019-11-08 stsp char *id_str;
1252 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1254 6570a66d 2019-11-08 stsp goto done;
1255 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1257 11b20872 2019-11-08 stsp wattr_on(view->window,
1258 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1259 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1261 11b20872 2019-11-08 stsp wattr_off(view->window,
1262 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1263 6570a66d 2019-11-08 stsp free(id_str);
1265 6570a66d 2019-11-08 stsp if (col > avail)
1266 6570a66d 2019-11-08 stsp goto done;
1269 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1270 5813d178 2019-03-09 stsp if (author == NULL) {
1271 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1272 80ddbec8 2018-04-29 stsp goto done;
1274 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1276 bb737323 2018-05-20 stsp goto done;
1277 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_AUTHOR);
1279 11b20872 2019-11-08 stsp wattr_on(view->window,
1280 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1281 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1283 11b20872 2019-11-08 stsp wattr_off(view->window,
1284 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1285 bb737323 2018-05-20 stsp col += author_width;
1286 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1287 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1289 bb737323 2018-05-20 stsp author_width++;
1291 9c2eaf34 2018-05-20 stsp if (col > avail)
1292 9c2eaf34 2018-05-20 stsp goto done;
1294 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1296 6d9fbc00 2018-04-29 stsp goto done;
1297 bb737323 2018-05-20 stsp logmsg = logmsg0;
1298 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1300 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1301 bb737323 2018-05-20 stsp if (newline)
1302 bb737323 2018-05-20 stsp *newline = '\0';
1303 bb737323 2018-05-20 stsp limit = avail - col;
1304 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1306 bb737323 2018-05-20 stsp goto done;
1307 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1308 bb737323 2018-05-20 stsp col += logmsg_width;
1309 27a741e5 2019-09-11 stsp while (col < avail) {
1310 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1314 80ddbec8 2018-04-29 stsp free(logmsg0);
1315 bb737323 2018-05-20 stsp free(wlogmsg);
1316 5813d178 2019-03-09 stsp free(author);
1317 bb737323 2018-05-20 stsp free(wauthor);
1318 80ddbec8 2018-04-29 stsp free(line);
1319 80ddbec8 2018-04-29 stsp return err;
1322 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1323 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1324 899d86c2 2018-05-10 stsp struct got_object_id *id)
1326 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1328 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1329 80ddbec8 2018-04-29 stsp if (entry == NULL)
1330 899d86c2 2018-05-10 stsp return NULL;
1332 899d86c2 2018-05-10 stsp entry->id = id;
1333 99db9666 2018-05-07 stsp entry->commit = commit;
1334 899d86c2 2018-05-10 stsp return entry;
1337 99db9666 2018-05-07 stsp static void
1338 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1340 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1342 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1343 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1344 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1345 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1346 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1347 99db9666 2018-05-07 stsp free(entry);
1350 99db9666 2018-05-07 stsp static void
1351 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1353 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1354 99db9666 2018-05-07 stsp pop_commit(commits);
1357 c4972b91 2018-05-07 stsp static const struct got_error *
1358 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1359 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1361 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1362 13add988 2019-10-15 stsp regmatch_t regmatch;
1363 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1365 13add988 2019-10-15 stsp *have_match = 0;
1367 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1369 13add988 2019-10-15 stsp return err;
1371 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1373 13add988 2019-10-15 stsp goto done;
1375 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1376 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1377 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1378 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1379 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1380 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1381 13add988 2019-10-15 stsp *have_match = 1;
1383 13add988 2019-10-15 stsp free(id_str);
1384 13add988 2019-10-15 stsp free(logmsg);
1385 13add988 2019-10-15 stsp return err;
1388 13add988 2019-10-15 stsp static const struct got_error *
1389 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1390 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1391 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1393 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1394 13add988 2019-10-15 stsp int nqueued = 0, have_match = 0;
1397 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1398 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1399 1a76625f 2018-10-22 stsp * while updating the display.
1401 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1402 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1403 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1404 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1405 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1406 1a76625f 2018-10-22 stsp int errcode;
1408 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1409 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1412 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1415 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1416 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1417 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1421 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1422 1a76625f 2018-10-22 stsp if (errcode) {
1423 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1424 13add988 2019-10-15 stsp "pthread_mutex_lock");
1428 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1429 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1430 ecb28ae0 2018-07-16 stsp nqueued++;
1431 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1433 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1434 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1437 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1438 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1439 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1440 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1442 a93813fa 2020-01-20 mpi if (err || have_match)
1446 9ba79e04 2018-06-11 stsp return err;
1449 0553a4e3 2018-04-30 stsp static const struct got_error *
1450 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1451 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1452 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1453 11b20872 2019-11-08 stsp struct got_reflist_head *refs, const char *path, int commits_needed,
1454 11b20872 2019-11-08 stsp struct tog_colors *colors)
1456 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1457 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1458 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1459 60493ae3 2019-06-20 stsp int width;
1460 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1461 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1462 8b473291 2019-02-21 stsp char *refs_str = NULL;
1463 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1464 11b20872 2019-11-08 stsp struct tog_color *tc;
1465 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1467 e0d42f60 2018-07-22 stsp entry = first;
1468 e0d42f60 2018-07-22 stsp ncommits = 0;
1469 e0d42f60 2018-07-22 stsp while (entry) {
1470 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1471 e0d42f60 2018-07-22 stsp *selected = entry;
1474 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1475 e0d42f60 2018-07-22 stsp ncommits++;
1478 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1479 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1481 ecb28ae0 2018-07-16 stsp return err;
1482 8b473291 2019-02-21 stsp if (refs) {
1483 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1486 8b473291 2019-02-21 stsp goto done;
1490 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1491 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1493 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1494 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1495 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1496 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1497 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1498 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1499 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1500 8b473291 2019-02-21 stsp goto done;
1503 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1504 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1505 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1506 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1507 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1508 1a76625f 2018-10-22 stsp header = NULL;
1509 1a76625f 2018-10-22 stsp goto done;
1511 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1512 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1513 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1514 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1515 1a76625f 2018-10-22 stsp header = NULL;
1516 1a76625f 2018-10-22 stsp goto done;
1518 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1520 1a76625f 2018-10-22 stsp goto done;
1522 2814baeb 2018-08-01 stsp werase(view->window);
1524 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1525 a3404814 2018-09-02 stsp wstandout(view->window);
1526 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1528 11b20872 2019-11-08 stsp wattr_on(view->window,
1529 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1530 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1532 11b20872 2019-11-08 stsp wattr_off(view->window,
1533 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1534 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1535 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1538 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1539 a3404814 2018-09-02 stsp wstandend(view->window);
1540 ecb28ae0 2018-07-16 stsp free(wline);
1541 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1542 1a76625f 2018-10-22 stsp goto done;
1544 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1545 899d86c2 2018-05-10 stsp entry = first;
1546 5813d178 2019-03-09 stsp ncommits = 0;
1547 5813d178 2019-03-09 stsp while (entry) {
1548 5813d178 2019-03-09 stsp char *author;
1549 5813d178 2019-03-09 stsp wchar_t *wauthor;
1550 5813d178 2019-03-09 stsp int width;
1551 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1553 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1554 5813d178 2019-03-09 stsp if (author == NULL) {
1555 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1556 5813d178 2019-03-09 stsp goto done;
1558 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1559 27a741e5 2019-09-11 stsp date_display_cols);
1560 5813d178 2019-03-09 stsp if (author_cols < width)
1561 5813d178 2019-03-09 stsp author_cols = width;
1562 5813d178 2019-03-09 stsp free(wauthor);
1563 5813d178 2019-03-09 stsp free(author);
1564 7ca04879 2019-10-19 stsp ncommits++;
1565 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1568 5813d178 2019-03-09 stsp entry = first;
1569 899d86c2 2018-05-10 stsp *last = first;
1570 867c6645 2018-07-10 stsp ncommits = 0;
1571 899d86c2 2018-05-10 stsp while (entry) {
1572 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1574 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1575 2814baeb 2018-08-01 stsp wstandout(view->window);
1576 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1577 11b20872 2019-11-08 stsp date_display_cols, author_cols, colors);
1578 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1579 2814baeb 2018-08-01 stsp wstandend(view->window);
1581 60493ae3 2019-06-20 stsp goto done;
1582 0553a4e3 2018-04-30 stsp ncommits++;
1583 899d86c2 2018-05-10 stsp *last = entry;
1584 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1587 1a57306a 2018-09-02 stsp view_vborder(view);
1589 1a76625f 2018-10-22 stsp free(id_str);
1590 8b473291 2019-02-21 stsp free(refs_str);
1591 1a76625f 2018-10-22 stsp free(ncommits_str);
1592 1a76625f 2018-10-22 stsp free(header);
1593 80ddbec8 2018-04-29 stsp return err;
1596 07b55e75 2018-05-10 stsp static void
1597 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1598 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1599 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1601 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1602 07b55e75 2018-05-10 stsp int nscrolled = 0;
1604 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1605 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1608 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1609 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1610 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1611 07b55e75 2018-05-10 stsp if (entry) {
1612 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1613 07b55e75 2018-05-10 stsp nscrolled++;
1618 aa075928 2018-05-10 stsp static const struct got_error *
1619 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1620 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1622 5e224a3e 2019-02-22 stsp int errcode;
1623 8a42fca8 2019-02-22 stsp int max_wait = 20;
1625 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1627 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1628 5e224a3e 2019-02-22 stsp if (*log_complete)
1631 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1632 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1633 7aafa0d1 2019-02-22 stsp if (errcode)
1634 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1635 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1636 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1638 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1639 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1640 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1641 5e224a3e 2019-02-22 stsp * with few commits in history.
1643 7aafa0d1 2019-02-22 stsp return NULL;
1647 5e224a3e 2019-02-22 stsp return NULL;
1650 5e224a3e 2019-02-22 stsp static const struct got_error *
1651 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1652 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1653 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1654 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1655 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1657 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1658 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1659 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1661 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1662 5e224a3e 2019-02-22 stsp return NULL;
1664 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1665 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1667 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1668 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1670 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1671 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1672 5e224a3e 2019-02-22 stsp need_commits);
1674 5e224a3e 2019-02-22 stsp return err;
1678 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1679 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1682 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1684 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1685 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1687 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1688 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1690 dd0a52c1 2018-05-20 stsp return err;
1693 cd0acaa7 2018-05-20 stsp static const struct got_error *
1694 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1695 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1696 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1697 8b473291 2019-02-21 stsp struct got_repository *repo)
1699 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1700 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1701 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1703 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1704 15a94983 2018-12-23 stsp if (diff_view == NULL)
1705 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1707 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1708 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1709 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1710 e5a0f69f 2018-08-18 stsp if (err == NULL)
1711 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1712 cd0acaa7 2018-05-20 stsp return err;
1715 80ddbec8 2018-04-29 stsp static const struct got_error *
1716 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1717 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1719 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1721 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1722 941e9f74 2019-05-21 stsp if (parent == NULL)
1723 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1725 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1726 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1727 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1728 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1729 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1730 941e9f74 2019-05-21 stsp s->tree = subtree;
1731 941e9f74 2019-05-21 stsp s->selected = 0;
1732 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1733 941e9f74 2019-05-21 stsp return NULL;
1737 941e9f74 2019-05-21 stsp static const struct got_error *
1738 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1739 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1740 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1742 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1743 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1744 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1745 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1746 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1747 941e9f74 2019-05-21 stsp const char *p;
1749 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1750 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1752 9343a5fb 2018-06-23 stsp return err;
1754 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1755 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1756 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1758 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1759 941e9f74 2019-05-21 stsp if (err) {
1760 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1761 941e9f74 2019-05-21 stsp return err;
1763 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1765 941e9f74 2019-05-21 stsp *new_view = tree_view;
1767 33cbf02b 2020-01-12 stsp if (got_path_is_root_dir(path))
1768 33cbf02b 2020-01-12 stsp return NULL;
1770 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1772 941e9f74 2019-05-21 stsp while (*p) {
1773 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1774 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1775 56e0773d 2019-11-28 stsp char *te_name;
1777 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1780 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1781 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1782 941e9f74 2019-05-21 stsp if (slash == NULL)
1783 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1785 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1786 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1787 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1790 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1791 56e0773d 2019-11-28 stsp if (te == NULL) {
1792 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1793 56e0773d 2019-11-28 stsp free(te_name);
1796 56e0773d 2019-11-28 stsp free(te_name);
1797 56e0773d 2019-11-28 stsp s->selected_entry = te;
1798 56e0773d 2019-11-28 stsp s->selected = got_tree_entry_get_index(te);
1799 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1800 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1802 56e0773d 2019-11-28 stsp if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1803 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1804 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1805 67409a31 2019-05-24 stsp s->selected = 0;
1809 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1810 941e9f74 2019-05-21 stsp if (slash)
1811 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1813 941e9f74 2019-05-21 stsp subpath = strdup(path);
1814 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1815 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1819 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1824 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1825 941e9f74 2019-05-21 stsp free(tree_id);
1829 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1830 941e9f74 2019-05-21 stsp if (err) {
1831 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1834 941e9f74 2019-05-21 stsp if (slash == NULL)
1836 941e9f74 2019-05-21 stsp free(subpath);
1837 941e9f74 2019-05-21 stsp subpath = NULL;
1838 941e9f74 2019-05-21 stsp p = slash;
1841 941e9f74 2019-05-21 stsp free(subpath);
1842 1a76625f 2018-10-22 stsp return err;
1845 61266923 2020-01-14 stsp static const struct got_error *
1846 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1848 61266923 2020-01-14 stsp sigset_t sigset;
1849 61266923 2020-01-14 stsp int errcode;
1851 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1852 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1854 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1855 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1856 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1857 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1858 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1860 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1861 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1862 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1864 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1865 61266923 2020-01-14 stsp if (errcode)
1866 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1868 61266923 2020-01-14 stsp return NULL;
1871 1a76625f 2018-10-22 stsp static void *
1872 1a76625f 2018-10-22 stsp log_thread(void *arg)
1874 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1875 1a76625f 2018-10-22 stsp int errcode = 0;
1876 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1877 1a76625f 2018-10-22 stsp int done = 0;
1879 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1881 61266923 2020-01-14 stsp return (void *)err;
1883 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1884 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1885 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1886 13add988 2019-10-15 stsp a->regex);
1887 1a76625f 2018-10-22 stsp if (err) {
1888 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1889 1a76625f 2018-10-22 stsp return (void *)err;
1890 1a76625f 2018-10-22 stsp err = NULL;
1892 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1893 1a76625f 2018-10-22 stsp a->commits_needed--;
1895 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1896 3abe8080 2019-04-10 stsp if (errcode) {
1897 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1898 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1900 3abe8080 2019-04-10 stsp } else if (*a->quit)
1902 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1903 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1904 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1905 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1909 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1910 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1911 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1912 1a76625f 2018-10-22 stsp &tog_mutex);
1913 1a76625f 2018-10-22 stsp if (errcode)
1914 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1915 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1918 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1919 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1920 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1921 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1923 3abe8080 2019-04-10 stsp a->log_complete = 1;
1924 1a76625f 2018-10-22 stsp return (void *)err;
1927 1a76625f 2018-10-22 stsp static const struct got_error *
1928 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1930 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1931 1a76625f 2018-10-22 stsp int errcode;
1933 1a76625f 2018-10-22 stsp if (s->thread) {
1934 1a76625f 2018-10-22 stsp s->quit = 1;
1935 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1936 1a76625f 2018-10-22 stsp if (errcode)
1937 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1938 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1939 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1940 1a76625f 2018-10-22 stsp if (errcode)
1941 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1942 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1943 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1944 1a76625f 2018-10-22 stsp if (errcode)
1945 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1946 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1947 1a76625f 2018-10-22 stsp if (errcode)
1948 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1949 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1950 1a76625f 2018-10-22 stsp s->thread = NULL;
1953 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1954 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1955 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1957 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1958 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1959 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1962 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1963 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1964 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1967 9343a5fb 2018-06-23 stsp return err;
1970 9343a5fb 2018-06-23 stsp static const struct got_error *
1971 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1973 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1974 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1976 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1977 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1978 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1979 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1980 1a76625f 2018-10-22 stsp free(s->start_id);
1981 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1982 1a76625f 2018-10-22 stsp return err;
1985 1a76625f 2018-10-22 stsp static const struct got_error *
1986 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1988 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1990 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1991 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1992 60493ae3 2019-06-20 stsp return NULL;
1995 60493ae3 2019-06-20 stsp static const struct got_error *
1996 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1998 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1999 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2000 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2002 60493ae3 2019-06-20 stsp if (!view->searching) {
2003 60493ae3 2019-06-20 stsp view->search_next_done = 1;
2004 60493ae3 2019-06-20 stsp return NULL;
2007 96e2b566 2019-07-08 stsp if (s->search_entry) {
2008 13add988 2019-10-15 stsp int errcode, ch;
2009 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2010 13add988 2019-10-15 stsp if (errcode)
2011 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2012 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2013 13add988 2019-10-15 stsp ch = wgetch(view->window);
2014 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2015 13add988 2019-10-15 stsp if (errcode)
2016 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2017 13add988 2019-10-15 stsp "pthread_mutex_lock");
2018 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2019 678cbce5 2019-07-28 stsp view->search_next_done = 1;
2020 678cbce5 2019-07-28 stsp return NULL;
2022 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2023 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2025 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2026 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2027 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2028 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2029 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2031 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
2032 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2034 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2035 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2037 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2040 60493ae3 2019-06-20 stsp while (1) {
2041 13add988 2019-10-15 stsp int have_match = 0;
2043 60493ae3 2019-06-20 stsp if (entry == NULL) {
2044 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2045 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2046 f801134a 2019-06-25 stsp view->search_next_done = 1;
2047 f801134a 2019-06-25 stsp return NULL;
2050 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2051 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2052 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2054 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2055 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
2056 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
2057 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
2058 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
2061 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2062 13add988 2019-10-15 stsp &view->regex);
2065 13add988 2019-10-15 stsp if (have_match) {
2066 60493ae3 2019-06-20 stsp view->search_next_done = 1;
2067 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2071 96e2b566 2019-07-08 stsp s->search_entry = entry;
2072 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2073 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2075 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2078 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2079 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2080 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2081 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2083 60493ae3 2019-06-20 stsp return err;
2086 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2087 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2089 60493ae3 2019-06-20 stsp return err;
2094 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2096 60493ae3 2019-06-20 stsp return NULL;
2099 60493ae3 2019-06-20 stsp static const struct got_error *
2100 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2101 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
2102 b672a97a 2020-01-27 stsp const char *head_ref_name, const char *path, int check_disk,
2103 b672a97a 2020-01-27 stsp int log_branches)
2105 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2106 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2107 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2108 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2109 1a76625f 2018-10-22 stsp int errcode;
2111 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2112 ecb28ae0 2018-07-16 stsp if (err != NULL)
2113 ecb28ae0 2018-07-16 stsp goto done;
2115 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2116 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2117 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2119 8b473291 2019-02-21 stsp s->refs = refs;
2120 fb2756b9 2018-08-04 stsp s->repo = repo;
2121 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2122 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2123 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2124 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2125 5036bf37 2018-09-24 stsp goto done;
2127 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2129 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2130 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2131 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2132 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2134 11b20872 2019-11-08 stsp goto done;
2135 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2136 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2137 11b20872 2019-11-08 stsp if (err) {
2138 11b20872 2019-11-08 stsp free_colors(&s->colors);
2139 11b20872 2019-11-08 stsp goto done;
2141 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2142 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2143 11b20872 2019-11-08 stsp if (err) {
2144 11b20872 2019-11-08 stsp free_colors(&s->colors);
2145 11b20872 2019-11-08 stsp goto done;
2149 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2150 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2151 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2152 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2153 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2155 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2157 1a76625f 2018-10-22 stsp goto done;
2158 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2159 b672a97a 2020-01-27 stsp !s->log_branches);
2161 1a76625f 2018-10-22 stsp goto done;
2162 c5b78334 2020-01-12 stsp err = got_commit_graph_iter_start(thread_graph,
2163 c5b78334 2020-01-12 stsp s->start_id, s->repo, NULL, NULL);
2165 c5b78334 2020-01-12 stsp goto done;
2167 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2168 1a76625f 2018-10-22 stsp if (errcode) {
2169 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2170 1a76625f 2018-10-22 stsp goto done;
2173 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2174 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2175 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2176 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2177 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2178 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2179 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2180 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2181 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2182 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2183 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2184 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2185 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2188 1a76625f 2018-10-22 stsp close_log_view(view);
2189 ba4f502b 2018-08-04 stsp return err;
2192 e5a0f69f 2018-08-18 stsp static const struct got_error *
2193 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2195 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2197 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2198 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2199 2b380cc8 2018-10-24 stsp &s->thread_args);
2200 2b380cc8 2018-10-24 stsp if (errcode)
2201 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2204 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2205 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2206 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2207 11b20872 2019-11-08 stsp s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2210 e5a0f69f 2018-08-18 stsp static const struct got_error *
2211 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2212 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2214 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2215 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2216 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2217 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2218 669b5ffa 2018-10-07 stsp int begin_x = 0;
2219 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2221 e5a0f69f 2018-08-18 stsp switch (ch) {
2223 1e37a5c2 2019-05-12 jcs s->quit = 1;
2226 1e37a5c2 2019-05-12 jcs case KEY_UP:
2229 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2231 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2232 1e37a5c2 2019-05-12 jcs s->selected--;
2234 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2235 1144d21a 2019-06-21 stsp &s->commits);
2237 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2238 a4292ac5 2019-05-12 jcs case CTRL('b'):
2239 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2241 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2242 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2243 1e37a5c2 2019-05-12 jcs s->selected = 0;
2246 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2247 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
2250 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2253 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2255 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2256 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2257 1e37a5c2 2019-05-12 jcs s->selected++;
2260 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2261 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2262 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2263 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2264 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2266 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2267 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2268 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2269 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2270 1e37a5c2 2019-05-12 jcs if (first == NULL)
2272 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2273 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
2274 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2275 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2276 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2279 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2280 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2281 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2282 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2283 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2284 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2286 1e37a5c2 2019-05-12 jcs err = NULL;
2289 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2290 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2291 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2292 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2293 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2295 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2298 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2300 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2301 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2302 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2303 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2304 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2307 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2308 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2310 1e37a5c2 2019-05-12 jcs return err;
2311 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2313 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2316 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2317 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2319 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2322 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2324 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2325 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2326 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2327 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2330 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2331 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2333 1e37a5c2 2019-05-12 jcs return err;
2334 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2336 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2339 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2340 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2342 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2344 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2345 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2347 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2348 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2349 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2351 1e37a5c2 2019-05-12 jcs return err;
2352 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2353 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2354 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2355 638f9024 2019-05-13 stsp return got_error_from_errno(
2356 1e37a5c2 2019-05-12 jcs "view_open");
2357 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2358 b672a97a 2020-01-27 stsp s->repo, s->head_ref_name, parent_path, 0,
2359 b672a97a 2020-01-27 stsp s->log_branches);
2361 1e37a5c2 2019-05-12 jcs return err;;
2362 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2363 1e37a5c2 2019-05-12 jcs *new_view = lv;
2365 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2366 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2368 1e37a5c2 2019-05-12 jcs return NULL;
2371 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2372 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2374 d01904d4 2019-06-25 stsp return err;
2375 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2376 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2377 d01904d4 2019-06-25 stsp if (lv == NULL)
2378 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2379 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2380 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2381 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2382 ef129c5e 2019-08-03 stsp if (err) {
2383 ef129c5e 2019-08-03 stsp view_close(lv);
2384 d01904d4 2019-06-25 stsp return err;
2386 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2387 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2388 e6b23735 2019-10-04 stsp free(start_id);
2389 e6b23735 2019-10-04 stsp view_close(lv);
2390 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2392 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2393 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2394 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2395 e6b23735 2019-10-04 stsp if (err) {
2396 d01904d4 2019-06-25 stsp free(start_id);
2397 ef129c5e 2019-08-03 stsp view_close(lv);
2398 50284065 2019-10-04 stsp return err;
2400 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2401 b672a97a 2020-01-27 stsp s->head_ref_name, in_repo_path, 0, s->log_branches);
2402 ef129c5e 2019-08-03 stsp if (err) {
2403 ef129c5e 2019-08-03 stsp free(start_id);
2404 b672a97a 2020-01-27 stsp view_close(lv);
2405 b672a97a 2020-01-27 stsp return err;;
2407 b672a97a 2020-01-27 stsp *dead_view = view;
2408 b672a97a 2020-01-27 stsp *new_view = lv;
2411 b672a97a 2020-01-27 stsp s->log_branches = !s->log_branches;
2412 b672a97a 2020-01-27 stsp err = stop_log_thread(s);
2414 b672a97a 2020-01-27 stsp return err;
2415 b672a97a 2020-01-27 stsp lv = view_open(view->nlines, view->ncols,
2416 b672a97a 2020-01-27 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2417 b672a97a 2020-01-27 stsp if (lv == NULL)
2418 b672a97a 2020-01-27 stsp return got_error_from_errno("view_open");
2419 b672a97a 2020-01-27 stsp err = open_log_view(lv, s->start_id, s->refs, s->repo,
2420 b672a97a 2020-01-27 stsp s->head_ref_name, s->in_repo_path, 0, s->log_branches);
2421 b672a97a 2020-01-27 stsp if (err) {
2422 ef129c5e 2019-08-03 stsp view_close(lv);
2423 d01904d4 2019-06-25 stsp return err;;
2425 d01904d4 2019-06-25 stsp *dead_view = view;
2426 d01904d4 2019-06-25 stsp *new_view = lv;
2432 80ddbec8 2018-04-29 stsp return err;
2435 4ed7e80c 2018-05-20 stsp static const struct got_error *
2436 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2438 c2db6724 2019-01-04 stsp const struct got_error *error;
2440 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2441 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2442 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2444 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2445 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2447 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2448 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2450 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2451 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2453 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2454 c2db6724 2019-01-04 stsp if (error != NULL)
2455 c2db6724 2019-01-04 stsp return error;
2457 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2458 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2460 c2db6724 2019-01-04 stsp return NULL;
2463 a915003a 2019-02-05 stsp static void
2464 a915003a 2019-02-05 stsp init_curses(void)
2466 a915003a 2019-02-05 stsp initscr();
2468 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2471 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2472 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2473 a915003a 2019-02-05 stsp curs_set(0);
2474 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2475 6d17833f 2019-11-08 stsp start_color();
2476 6d17833f 2019-11-08 stsp use_default_colors();
2478 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2479 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2480 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2483 c2db6724 2019-01-04 stsp static const struct got_error *
2484 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2486 80ddbec8 2018-04-29 stsp const struct got_error *error;
2487 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2488 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2489 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2490 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2491 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2492 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2493 f43793a4 2020-01-27 stsp int ch, log_branches = 0, check_disk = 1;
2494 04cc582a 2018-08-01 stsp struct tog_view *view;
2496 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2498 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2499 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2500 c2db6724 2019-01-04 stsp NULL) == -1)
2501 80ddbec8 2018-04-29 stsp err(1, "pledge");
2504 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2505 80ddbec8 2018-04-29 stsp switch (ch) {
2507 b672a97a 2020-01-27 stsp log_branches = 1;
2510 80ddbec8 2018-04-29 stsp start_commit = optarg;
2513 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2514 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2515 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2519 17020d27 2019-03-07 stsp usage_log();
2520 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2524 80ddbec8 2018-04-29 stsp argc -= optind;
2525 80ddbec8 2018-04-29 stsp argv += optind;
2527 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2528 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2529 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2530 ecb28ae0 2018-07-16 stsp goto done;
2532 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2533 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2534 963f97a1 2019-03-18 stsp goto done;
2535 963f97a1 2019-03-18 stsp error = NULL;
2537 963f97a1 2019-03-18 stsp if (argc == 0) {
2538 963f97a1 2019-03-18 stsp path = strdup("");
2539 963f97a1 2019-03-18 stsp if (path == NULL) {
2540 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2541 ecb28ae0 2018-07-16 stsp goto done;
2543 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2544 963f97a1 2019-03-18 stsp if (worktree) {
2545 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2547 963f97a1 2019-03-18 stsp if (error)
2548 963f97a1 2019-03-18 stsp goto done;
2550 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2551 963f97a1 2019-03-18 stsp if (path == NULL) {
2552 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2553 963f97a1 2019-03-18 stsp goto done;
2557 963f97a1 2019-03-18 stsp usage_log();
2559 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2560 a1fbf39a 2019-08-11 stsp if (worktree)
2561 b9d7675a 2019-08-11 stsp repo_path = strdup(
2562 b9d7675a 2019-08-11 stsp got_worktree_get_repo_path(worktree));
2564 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2566 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2567 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2568 963f97a1 2019-03-18 stsp goto done;
2571 a915003a 2019-02-05 stsp init_curses();
2573 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2574 80ddbec8 2018-04-29 stsp if (error != NULL)
2575 ecb28ae0 2018-07-16 stsp goto done;
2577 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2578 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2579 c02c541e 2019-03-29 stsp if (error)
2580 c02c541e 2019-03-29 stsp goto done;
2582 15a94983 2018-12-23 stsp if (start_commit == NULL)
2583 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2584 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2585 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2587 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2588 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2589 80ddbec8 2018-04-29 stsp if (error != NULL)
2590 8b473291 2019-02-21 stsp goto done;
2592 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2593 8b473291 2019-02-21 stsp if (error)
2594 ecb28ae0 2018-07-16 stsp goto done;
2596 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2597 04cc582a 2018-08-01 stsp if (view == NULL) {
2598 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2599 04cc582a 2018-08-01 stsp goto done;
2601 2fc00ff4 2019-08-31 stsp if (worktree) {
2602 f43793a4 2020-01-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2604 f43793a4 2020-01-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2605 f43793a4 2020-01-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2606 f43793a4 2020-01-27 stsp error = got_error_from_errno("asprintf");
2607 f43793a4 2020-01-27 stsp goto done;
2609 f43793a4 2020-01-27 stsp free(path);
2611 f43793a4 2020-01-27 stsp check_disk = 0;
2613 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2614 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2615 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2616 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2617 2fc00ff4 2019-08-31 stsp goto done;
2620 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2621 f43793a4 2020-01-27 stsp path, check_disk, log_branches);
2622 ba4f502b 2018-08-04 stsp if (error)
2623 ba4f502b 2018-08-04 stsp goto done;
2624 2fc00ff4 2019-08-31 stsp if (worktree) {
2625 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2626 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2627 2fc00ff4 2019-08-31 stsp worktree = NULL;
2629 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2631 ecb28ae0 2018-07-16 stsp free(repo_path);
2632 ecb28ae0 2018-07-16 stsp free(cwd);
2633 ecb28ae0 2018-07-16 stsp free(path);
2634 899d86c2 2018-05-10 stsp free(start_id);
2635 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2637 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2638 ec142235 2019-03-07 stsp if (worktree)
2639 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2640 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2641 80ddbec8 2018-04-29 stsp return error;
2644 4ed7e80c 2018-05-20 stsp __dead static void
2645 9f7d7167 2018-04-29 stsp usage_diff(void)
2648 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2649 9f7d7167 2018-04-29 stsp getprogname());
2653 b304db33 2018-05-20 stsp static char *
2654 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2656 b304db33 2018-05-20 stsp char *line;
2657 b304db33 2018-05-20 stsp size_t linelen;
2658 b304db33 2018-05-20 stsp size_t lineno;
2659 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2661 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2663 b304db33 2018-05-20 stsp *len = linelen;
2664 b304db33 2018-05-20 stsp return line;
2667 6d17833f 2019-11-08 stsp static int
2668 6d17833f 2019-11-08 stsp match_line(const char *line, regex_t *regex)
2670 6d17833f 2019-11-08 stsp regmatch_t regmatch;
2672 6d17833f 2019-11-08 stsp return regexec(regex, line, 1, ®match, 0) == 0;
2675 f26dddb7 2019-11-08 stsp struct tog_color *
2676 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2678 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2680 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2681 6d17833f 2019-11-08 stsp if (match_line(line, &tc->regex))
2682 6d17833f 2019-11-08 stsp return tc;
2685 6d17833f 2019-11-08 stsp return NULL;
2688 4ed7e80c 2018-05-20 stsp static const struct got_error *
2689 f44b1f58 2020-02-02 tracey draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2690 f44b1f58 2020-02-02 tracey int selected_line, int max_lines, int *last_displayed_line, int *eof,
2691 f44b1f58 2020-02-02 tracey char *header, struct tog_colors *colors)
2693 61e69b96 2018-05-20 stsp const struct got_error *err;
2694 f44b1f58 2020-02-02 tracey int lineno = 0, nprinted = 0;
2695 b304db33 2018-05-20 stsp char *line;
2696 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2697 b304db33 2018-05-20 stsp size_t len;
2698 61e69b96 2018-05-20 stsp wchar_t *wline;
2699 e0b650dd 2018-05-20 stsp int width;
2701 26ed57b2 2018-05-19 stsp rewind(f);
2702 f7d12f7e 2018-08-01 stsp werase(view->window);
2704 a3404814 2018-09-02 stsp if (header) {
2705 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
2706 a3404814 2018-09-02 stsp if (err) {
2707 a3404814 2018-09-02 stsp return err;
2710 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2711 a3404814 2018-09-02 stsp wstandout(view->window);
2712 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2713 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2714 a3404814 2018-09-02 stsp wstandend(view->window);
2715 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2716 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2718 a3404814 2018-09-02 stsp if (max_lines <= 1)
2719 a3404814 2018-09-02 stsp return NULL;
2720 a3404814 2018-09-02 stsp max_lines--;
2724 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2725 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2726 26ed57b2 2018-05-19 stsp if (line == NULL) {
2730 f44b1f58 2020-02-02 tracey if (++lineno < *first_displayed_line) {
2731 26ed57b2 2018-05-19 stsp free(line);
2735 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2736 61e69b96 2018-05-20 stsp if (err) {
2737 61e69b96 2018-05-20 stsp free(line);
2738 61e69b96 2018-05-20 stsp return err;
2741 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2743 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2744 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2745 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2747 6d17833f 2019-11-08 stsp wattr_off(view->window,
2748 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2749 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2750 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2751 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2752 f44b1f58 2020-02-02 tracey *first_displayed_line = lineno;
2753 26ed57b2 2018-05-19 stsp free(line);
2754 2550e4c3 2018-07-13 stsp free(wline);
2755 2550e4c3 2018-07-13 stsp wline = NULL;
2757 f44b1f58 2020-02-02 tracey *last_displayed_line = lineno;
2759 1a57306a 2018-09-02 stsp view_vborder(view);
2761 c3e9aa98 2019-05-13 jcs if (*eof) {
2762 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2763 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2764 c3e9aa98 2019-05-13 jcs nprinted++;
2767 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2769 c3e9aa98 2019-05-13 jcs return err;
2772 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2773 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2774 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2777 26ed57b2 2018-05-19 stsp return NULL;
2780 abd2672a 2018-12-23 stsp static char *
2781 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2783 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2784 09867e48 2019-08-13 stsp char *p, *s;
2786 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2787 09867e48 2019-08-13 stsp if (tm == NULL)
2788 09867e48 2019-08-13 stsp return NULL;
2789 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2790 09867e48 2019-08-13 stsp if (s == NULL)
2791 09867e48 2019-08-13 stsp return NULL;
2792 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2794 abd2672a 2018-12-23 stsp *p = '\0';
2798 4ed7e80c 2018-05-20 stsp static const struct got_error *
2799 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2800 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2802 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2803 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2804 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2805 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2806 45d799e2 2018-12-23 stsp time_t committer_time;
2807 45d799e2 2018-12-23 stsp const char *author, *committer;
2808 8b473291 2019-02-21 stsp char *refs_str = NULL;
2810 8b473291 2019-02-21 stsp if (refs) {
2811 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2813 8b473291 2019-02-21 stsp return err;
2816 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2818 abd2672a 2018-12-23 stsp return err;
2820 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2821 15a94983 2018-12-23 stsp if (err) {
2822 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2823 15a94983 2018-12-23 stsp goto done;
2826 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2827 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2828 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2829 abd2672a 2018-12-23 stsp goto done;
2831 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2832 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2833 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2834 abd2672a 2018-12-23 stsp goto done;
2836 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2837 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2838 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2839 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2840 abd2672a 2018-12-23 stsp goto done;
2842 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2843 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2844 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2845 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2846 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2847 abd2672a 2018-12-23 stsp goto done;
2849 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2851 5943eee2 2019-08-13 stsp goto done;
2852 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2853 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2854 abd2672a 2018-12-23 stsp goto done;
2857 abd2672a 2018-12-23 stsp free(id_str);
2858 5943eee2 2019-08-13 stsp free(logmsg);
2859 8b473291 2019-02-21 stsp free(refs_str);
2860 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2861 abd2672a 2018-12-23 stsp return err;
2864 f44b1f58 2020-02-02 tracey const struct got_error *
2865 f44b1f58 2020-02-02 tracey get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2866 f44b1f58 2020-02-02 tracey FILE *infile)
2868 f44b1f58 2020-02-02 tracey size_t len;
2869 f44b1f58 2020-02-02 tracey char *buf = NULL;
2871 f44b1f58 2020-02-02 tracey size_t noffsets = 0;
2872 f44b1f58 2020-02-02 tracey off_t off = 0;
2874 f44b1f58 2020-02-02 tracey if (line_offsets)
2875 f44b1f58 2020-02-02 tracey *line_offsets = NULL;
2876 f44b1f58 2020-02-02 tracey if (filesize)
2877 f44b1f58 2020-02-02 tracey *filesize = 0;
2878 f44b1f58 2020-02-02 tracey if (nlines)
2879 f44b1f58 2020-02-02 tracey *nlines = 0;
2881 f44b1f58 2020-02-02 tracey if (fseek(infile, 0, SEEK_END) == -1)
2882 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseek");
2883 f44b1f58 2020-02-02 tracey len = ftell(infile) + 1;
2884 f44b1f58 2020-02-02 tracey if (ferror(infile))
2885 f44b1f58 2020-02-02 tracey return got_error_from_errno("ftell");
2886 f44b1f58 2020-02-02 tracey if (fseek(infile, 0, SEEK_SET) == -1)
2887 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseek");
2889 f44b1f58 2020-02-02 tracey if (len == 0)
2890 f44b1f58 2020-02-02 tracey return NULL;
2891 f44b1f58 2020-02-02 tracey if ((buf = calloc(len, sizeof(char *))) == NULL)
2892 f44b1f58 2020-02-02 tracey return got_error_from_errno("calloc");
2894 f44b1f58 2020-02-02 tracey fread(buf, 1, len, infile);
2895 f44b1f58 2020-02-02 tracey if (ferror(infile))
2896 f44b1f58 2020-02-02 tracey return got_error_from_errno("fread");
2899 f44b1f58 2020-02-02 tracey if (line_offsets && nlines) {
2900 f44b1f58 2020-02-02 tracey if (*line_offsets == NULL) {
2901 f44b1f58 2020-02-02 tracey /* Have some data but perhaps no '\n'. */
2902 f44b1f58 2020-02-02 tracey noffsets = 1;
2903 f44b1f58 2020-02-02 tracey *nlines = 1;
2904 f44b1f58 2020-02-02 tracey *line_offsets = calloc(1, sizeof(**line_offsets));
2905 f44b1f58 2020-02-02 tracey if (*line_offsets == NULL)
2906 f44b1f58 2020-02-02 tracey return got_error_from_errno("calloc");
2907 f44b1f58 2020-02-02 tracey /* Skip forward over end of first line. */
2908 f44b1f58 2020-02-02 tracey while (i < len) {
2909 f44b1f58 2020-02-02 tracey if (buf[i] == '\n')
2914 f44b1f58 2020-02-02 tracey /* Scan '\n' offsets in remaining chunk of data. */
2915 f44b1f58 2020-02-02 tracey while (i < len) {
2916 f44b1f58 2020-02-02 tracey if (buf[i] != '\n') {
2918 f44b1f58 2020-02-02 tracey continue;
2920 f44b1f58 2020-02-02 tracey (*nlines)++;
2921 f44b1f58 2020-02-02 tracey if (noffsets < *nlines) {
2922 f44b1f58 2020-02-02 tracey off_t *o = recallocarray(*line_offsets,
2923 f44b1f58 2020-02-02 tracey noffsets, *nlines,
2924 f44b1f58 2020-02-02 tracey sizeof(**line_offsets));
2925 f44b1f58 2020-02-02 tracey if (o == NULL) {
2926 f44b1f58 2020-02-02 tracey free(*line_offsets);
2927 f44b1f58 2020-02-02 tracey *line_offsets = NULL;
2928 f44b1f58 2020-02-02 tracey return got_error_from_errno(
2929 f44b1f58 2020-02-02 tracey "recallocarray");
2931 f44b1f58 2020-02-02 tracey *line_offsets = o;
2932 f44b1f58 2020-02-02 tracey noffsets = *nlines;
2934 f44b1f58 2020-02-02 tracey off = i + 1;
2935 f44b1f58 2020-02-02 tracey (*line_offsets)[*nlines - 1] = off;
2940 f44b1f58 2020-02-02 tracey if (fflush(infile) != 0)
2941 f44b1f58 2020-02-02 tracey return got_error_from_errno("fflush");
2942 f44b1f58 2020-02-02 tracey rewind(infile);
2944 f44b1f58 2020-02-02 tracey if (filesize)
2945 f44b1f58 2020-02-02 tracey *filesize = len;
2947 f44b1f58 2020-02-02 tracey return NULL;
2950 abd2672a 2018-12-23 stsp static const struct got_error *
2951 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2953 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2954 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2955 15a94983 2018-12-23 stsp int obj_type;
2957 511a516b 2018-05-19 stsp f = got_opentemp();
2958 48ae06ee 2018-10-18 stsp if (f == NULL) {
2959 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2960 48ae06ee 2018-10-18 stsp goto done;
2962 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2963 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2964 fb43ecf1 2019-02-11 stsp goto done;
2968 15a94983 2018-12-23 stsp if (s->id1)
2969 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2971 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2973 15a94983 2018-12-23 stsp goto done;
2975 15a94983 2018-12-23 stsp switch (obj_type) {
2976 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2977 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2978 f44b1f58 2020-02-02 tracey s->diff_context, 0, s->repo, s->f);
2980 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2981 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2982 f44b1f58 2020-02-02 tracey s->diff_context, 0, s->repo, s->f);
2984 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2985 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2986 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2987 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2989 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2991 3ffacbe1 2020-02-02 tracey goto done;
2992 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2993 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
2994 f44b1f58 2020-02-02 tracey err =write_commit_info(s->id2, s->refs, s->repo, s->f);
2995 f44b1f58 2020-02-02 tracey if (err)
2996 f44b1f58 2020-02-02 tracey goto done;
2997 f44b1f58 2020-02-02 tracey } else {
2998 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2999 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3000 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3001 f44b1f58 2020-02-02 tracey err = write_commit_info(s->id2, s->refs,
3002 f44b1f58 2020-02-02 tracey s->repo, s->f);
3003 f44b1f58 2020-02-02 tracey if (err)
3004 f44b1f58 2020-02-02 tracey goto done;
3009 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3011 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
3012 f44b1f58 2020-02-02 tracey s->diff_context, 0, s->repo, s->f);
3016 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3019 f44b1f58 2020-02-02 tracey if (err)
3020 f44b1f58 2020-02-02 tracey goto done;
3021 f44b1f58 2020-02-02 tracey err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3024 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3025 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3026 48ae06ee 2018-10-18 stsp return err;
3029 f5215bb9 2019-02-22 stsp static void
3030 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3032 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3033 f5215bb9 2019-02-22 stsp update_panels();
3034 f5215bb9 2019-02-22 stsp doupdate();
3037 f44b1f58 2020-02-02 tracey static const struct got_error *
3038 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3040 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3042 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3043 f44b1f58 2020-02-02 tracey return NULL;
3046 f44b1f58 2020-02-02 tracey static const struct got_error *
3047 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3049 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3050 f44b1f58 2020-02-02 tracey int lineno;
3052 f44b1f58 2020-02-02 tracey if (!view->searching) {
3053 f44b1f58 2020-02-02 tracey view->search_next_done = 1;
3054 f44b1f58 2020-02-02 tracey return NULL;
3057 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3058 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3059 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3061 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3062 f44b1f58 2020-02-02 tracey } else {
3063 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3064 f44b1f58 2020-02-02 tracey lineno = 1;
3066 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3069 f44b1f58 2020-02-02 tracey while (1) {
3070 f44b1f58 2020-02-02 tracey char *line = NULL;
3071 f44b1f58 2020-02-02 tracey off_t offset;
3072 f44b1f58 2020-02-02 tracey size_t len;
3074 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3075 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3076 f44b1f58 2020-02-02 tracey view->search_next_done = 1;
3077 f44b1f58 2020-02-02 tracey free(line);
3081 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3082 f44b1f58 2020-02-02 tracey lineno = 1;
3084 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3087 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3088 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3089 f44b1f58 2020-02-02 tracey free(line);
3090 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3092 f44b1f58 2020-02-02 tracey free(line);
3093 f44b1f58 2020-02-02 tracey line = parse_next_line(s->f, &len);
3094 f44b1f58 2020-02-02 tracey if (line && match_line(line, &view->regex)) {
3095 f44b1f58 2020-02-02 tracey view->search_next_done = 1;
3096 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3097 f44b1f58 2020-02-02 tracey free(line);
3100 f44b1f58 2020-02-02 tracey free(line);
3101 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3102 f44b1f58 2020-02-02 tracey lineno++;
3104 f44b1f58 2020-02-02 tracey lineno--;
3107 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3108 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3109 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3112 f44b1f58 2020-02-02 tracey return NULL;
3115 48ae06ee 2018-10-18 stsp static const struct got_error *
3116 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3117 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
3118 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
3120 48ae06ee 2018-10-18 stsp const struct got_error *err;
3121 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3123 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3124 15a94983 2018-12-23 stsp int type1, type2;
3125 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3127 15a94983 2018-12-23 stsp return err;
3128 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3130 15a94983 2018-12-23 stsp return err;
3132 15a94983 2018-12-23 stsp if (type1 != type2)
3133 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3135 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3136 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3137 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3138 f44b1f58 2020-02-02 tracey s->repo = repo;
3139 f44b1f58 2020-02-02 tracey s->refs = refs;
3140 f44b1f58 2020-02-02 tracey s->id1 = id1;
3141 f44b1f58 2020-02-02 tracey s->id2 = id2;
3143 15a94983 2018-12-23 stsp if (id1) {
3144 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3145 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3146 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3148 5465d566 2020-02-01 tracey s->id1 = NULL;
3150 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3151 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3152 5465d566 2020-02-01 tracey free(s->id1);
3153 5465d566 2020-02-01 tracey s->id1 = NULL;
3154 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3156 5465d566 2020-02-01 tracey s->f = NULL;
3157 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3158 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3159 5465d566 2020-02-01 tracey s->diff_context = 3;
3160 5465d566 2020-02-01 tracey s->log_view = log_view;
3161 5465d566 2020-02-01 tracey s->repo = repo;
3162 5465d566 2020-02-01 tracey s->refs = refs;
3164 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3165 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3166 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3167 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3168 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3170 6d17833f 2019-11-08 stsp return err;
3171 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3172 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3173 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3174 6d17833f 2019-11-08 stsp if (err) {
3175 5465d566 2020-02-01 tracey free_colors(&s->colors);
3176 6d17833f 2019-11-08 stsp return err;
3178 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3179 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3180 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3181 6d17833f 2019-11-08 stsp if (err) {
3182 5465d566 2020-02-01 tracey free_colors(&s->colors);
3183 6d17833f 2019-11-08 stsp return err;
3186 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3187 11b20872 2019-11-08 stsp "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3188 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3189 6d17833f 2019-11-08 stsp if (err) {
3190 5465d566 2020-02-01 tracey free_colors(&s->colors);
3191 6d17833f 2019-11-08 stsp return err;
3194 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3195 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3196 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3197 11b20872 2019-11-08 stsp if (err) {
3198 5465d566 2020-02-01 tracey free_colors(&s->colors);
3199 11b20872 2019-11-08 stsp return err;
3202 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3203 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3204 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3205 11b20872 2019-11-08 stsp if (err) {
3206 5465d566 2020-02-01 tracey free_colors(&s->colors);
3207 11b20872 2019-11-08 stsp return err;
3211 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3212 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3213 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3215 5465d566 2020-02-01 tracey err = create_diff(s);
3216 48ae06ee 2018-10-18 stsp if (err) {
3217 5465d566 2020-02-01 tracey free(s->id1);
3218 5465d566 2020-02-01 tracey s->id1 = NULL;
3219 5465d566 2020-02-01 tracey free(s->id2);
3220 5465d566 2020-02-01 tracey s->id2 = NULL;
3221 48ae06ee 2018-10-18 stsp return err;
3224 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3225 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3226 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3227 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3228 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3230 5dc9f4bc 2018-08-04 stsp return NULL;
3233 e5a0f69f 2018-08-18 stsp static const struct got_error *
3234 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3236 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3237 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3239 5465d566 2020-02-01 tracey free(s->id1);
3240 5465d566 2020-02-01 tracey s->id1 = NULL;
3241 5465d566 2020-02-01 tracey free(s->id2);
3242 5465d566 2020-02-01 tracey s->id2 = NULL;
3243 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3244 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3245 5465d566 2020-02-01 tracey free_colors(&s->colors);
3246 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3247 e5a0f69f 2018-08-18 stsp return err;
3250 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3251 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3253 a3404814 2018-09-02 stsp const struct got_error *err;
3254 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3255 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3257 a3404814 2018-09-02 stsp if (s->id1) {
3258 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3260 a3404814 2018-09-02 stsp return err;
3262 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3264 a3404814 2018-09-02 stsp return err;
3266 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
3267 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3268 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3269 a3404814 2018-09-02 stsp free(id_str1);
3270 a3404814 2018-09-02 stsp free(id_str2);
3271 a3404814 2018-09-02 stsp return err;
3273 a3404814 2018-09-02 stsp free(id_str1);
3274 a3404814 2018-09-02 stsp free(id_str2);
3276 f44b1f58 2020-02-02 tracey return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3277 f44b1f58 2020-02-02 tracey s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3278 bddb1296 2019-11-08 stsp header, &s->colors);
3281 15a087fe 2019-02-21 stsp static const struct got_error *
3282 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3283 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3285 d7a04538 2019-02-21 stsp const struct got_error *err;
3286 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3287 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3288 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3290 15a087fe 2019-02-21 stsp free(s->id2);
3291 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3292 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3293 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3295 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3297 d7a04538 2019-02-21 stsp return err;
3298 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3299 15a087fe 2019-02-21 stsp free(s->id1);
3300 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3301 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3302 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3303 15a087fe 2019-02-21 stsp return NULL;
3306 0cf4efb1 2018-09-29 stsp static const struct got_error *
3307 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3308 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3310 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3311 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3312 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3313 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3316 e5a0f69f 2018-08-18 stsp switch (ch) {
3318 1e37a5c2 2019-05-12 jcs case KEY_UP:
3319 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3320 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3322 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3323 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3324 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3327 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3328 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3329 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3332 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3333 1e37a5c2 2019-05-12 jcs if (!s->eof)
3334 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3336 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3337 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3339 00ba99a7 2019-05-12 jcs if (s->eof)
3342 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3343 1e37a5c2 2019-05-12 jcs char *line;
3344 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3345 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3346 1e37a5c2 2019-05-12 jcs if (line == NULL)
3351 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3352 1e37a5c2 2019-05-12 jcs s->diff_context--;
3353 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3354 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3358 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3359 1e37a5c2 2019-05-12 jcs s->diff_context++;
3360 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3361 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3366 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3368 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3369 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3370 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3371 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3374 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3379 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3383 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3384 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3386 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3387 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3391 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3393 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3395 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3396 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3398 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
3399 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
3400 1e37a5c2 2019-05-12 jcs update_panels();
3401 1e37a5c2 2019-05-12 jcs doupdate();
3403 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
3404 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
3405 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
3406 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
3410 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3415 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3416 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3419 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3423 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3424 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3426 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3427 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3433 bcbd79e2 2018-08-19 stsp return err;
3436 4ed7e80c 2018-05-20 stsp static const struct got_error *
3437 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3439 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3440 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3441 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3442 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3443 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
3444 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3446 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3448 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3450 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3451 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3452 eb6600df 2019-01-04 stsp NULL) == -1)
3453 26ed57b2 2018-05-19 stsp err(1, "pledge");
3456 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3457 26ed57b2 2018-05-19 stsp switch (ch) {
3459 17020d27 2019-03-07 stsp usage_diff();
3460 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3464 26ed57b2 2018-05-19 stsp argc -= optind;
3465 26ed57b2 2018-05-19 stsp argv += optind;
3467 26ed57b2 2018-05-19 stsp if (argc == 0) {
3468 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3469 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3470 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
3471 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3472 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
3473 15a94983 2018-12-23 stsp id_str1 = argv[0];
3474 15a94983 2018-12-23 stsp id_str2 = argv[1];
3475 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
3476 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
3477 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3478 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
3479 15a94983 2018-12-23 stsp id_str1 = argv[1];
3480 15a94983 2018-12-23 stsp id_str2 = argv[2];
3482 26ed57b2 2018-05-19 stsp usage_diff();
3484 a915003a 2019-02-05 stsp init_curses();
3486 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3487 eb6600df 2019-01-04 stsp if (error)
3488 eb6600df 2019-01-04 stsp goto done;
3490 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3491 26ed57b2 2018-05-19 stsp if (error)
3492 26ed57b2 2018-05-19 stsp goto done;
3494 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
3495 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3496 26ed57b2 2018-05-19 stsp if (error)
3497 26ed57b2 2018-05-19 stsp goto done;
3499 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
3500 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3501 26ed57b2 2018-05-19 stsp if (error)
3502 26ed57b2 2018-05-19 stsp goto done;
3504 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3505 8b473291 2019-02-21 stsp if (error)
3506 8b473291 2019-02-21 stsp goto done;
3508 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3509 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3510 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3511 ea5e7bb5 2018-08-01 stsp goto done;
3513 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3514 5dc9f4bc 2018-08-04 stsp if (error)
3515 5dc9f4bc 2018-08-04 stsp goto done;
3516 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3518 c02c541e 2019-03-29 stsp free(repo_path);
3520 921be706 2019-06-28 stsp got_repo_close(repo);
3521 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3522 26ed57b2 2018-05-19 stsp return error;
3525 4ed7e80c 2018-05-20 stsp __dead static void
3526 9f7d7167 2018-04-29 stsp usage_blame(void)
3529 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3530 9f7d7167 2018-04-29 stsp getprogname());
3534 84451b3e 2018-07-10 stsp struct tog_blame_line {
3535 84451b3e 2018-07-10 stsp int annotated;
3536 84451b3e 2018-07-10 stsp struct got_object_id *id;
3539 4ed7e80c 2018-05-20 stsp static const struct got_error *
3540 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3541 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3542 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3543 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3544 11b20872 2019-11-08 stsp struct tog_colors *colors)
3546 84451b3e 2018-07-10 stsp const struct got_error *err;
3547 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3548 84451b3e 2018-07-10 stsp char *line;
3549 84451b3e 2018-07-10 stsp size_t len;
3550 84451b3e 2018-07-10 stsp wchar_t *wline;
3551 27a741e5 2019-09-11 stsp int width;
3552 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3553 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3554 ab089a2a 2018-07-12 stsp char *id_str;
3555 11b20872 2019-11-08 stsp struct tog_color *tc;
3557 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3559 ab089a2a 2018-07-12 stsp return err;
3561 84451b3e 2018-07-10 stsp rewind(f);
3562 f7d12f7e 2018-08-01 stsp werase(view->window);
3564 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3566 ab089a2a 2018-07-12 stsp free(id_str);
3567 ab089a2a 2018-07-12 stsp return err;
3570 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3571 ab089a2a 2018-07-12 stsp free(line);
3572 2550e4c3 2018-07-13 stsp line = NULL;
3574 1cae65b4 2019-09-22 stsp return err;
3575 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3576 a3404814 2018-09-02 stsp wstandout(view->window);
3577 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3579 11b20872 2019-11-08 stsp wattr_on(view->window,
3580 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3581 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3583 11b20872 2019-11-08 stsp wattr_off(view->window,
3584 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3585 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3586 a3404814 2018-09-02 stsp wstandend(view->window);
3587 2550e4c3 2018-07-13 stsp free(wline);
3588 2550e4c3 2018-07-13 stsp wline = NULL;
3589 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3590 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3592 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3593 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3594 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3595 ab089a2a 2018-07-12 stsp free(id_str);
3596 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3598 ab089a2a 2018-07-12 stsp free(id_str);
3599 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3600 3f60a8ef 2018-07-10 stsp free(line);
3601 2550e4c3 2018-07-13 stsp line = NULL;
3603 3f60a8ef 2018-07-10 stsp return err;
3604 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3605 2550e4c3 2018-07-13 stsp free(wline);
3606 2550e4c3 2018-07-13 stsp wline = NULL;
3607 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3608 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3611 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3612 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3613 84451b3e 2018-07-10 stsp if (line == NULL) {
3617 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3618 84451b3e 2018-07-10 stsp free(line);
3622 27a741e5 2019-09-11 stsp if (view->ncols <= 9) {
3623 27a741e5 2019-09-11 stsp width = 9;
3624 27a741e5 2019-09-11 stsp wline = wcsdup(L"");
3625 27a741e5 2019-09-11 stsp if (wline == NULL)
3626 27a741e5 2019-09-11 stsp err = got_error_from_errno("wcsdup");
3628 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line,
3629 27a741e5 2019-09-11 stsp view->ncols - 9, 9);
3630 27a741e5 2019-09-11 stsp width += 9;
3632 84451b3e 2018-07-10 stsp if (err) {
3633 84451b3e 2018-07-10 stsp free(line);
3634 84451b3e 2018-07-10 stsp return err;
3637 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3638 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3640 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3641 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3642 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3643 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3644 27a741e5 2019-09-11 stsp !(view->focussed &&
3645 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3646 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3647 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3648 8d0fe45a 2019-08-12 stsp char *id_str;
3649 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3650 8d0fe45a 2019-08-12 stsp if (err) {
3651 8d0fe45a 2019-08-12 stsp free(line);
3652 8d0fe45a 2019-08-12 stsp free(wline);
3653 8d0fe45a 2019-08-12 stsp return err;
3655 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3657 11b20872 2019-11-08 stsp wattr_on(view->window,
3658 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3659 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3661 11b20872 2019-11-08 stsp wattr_off(view->window,
3662 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3663 8d0fe45a 2019-08-12 stsp free(id_str);
3664 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3666 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3667 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3670 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3671 ee41ec32 2018-07-10 stsp prev_id = NULL;
3674 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3675 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3676 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3678 27a741e5 2019-09-11 stsp waddwstr(view->window, wline);
3679 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3680 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3681 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3682 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3683 84451b3e 2018-07-10 stsp free(line);
3684 2550e4c3 2018-07-13 stsp free(wline);
3685 2550e4c3 2018-07-13 stsp wline = NULL;
3687 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3689 1a57306a 2018-09-02 stsp view_vborder(view);
3691 84451b3e 2018-07-10 stsp return NULL;
3694 84451b3e 2018-07-10 stsp static const struct got_error *
3695 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3697 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3698 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3699 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3700 1a76625f 2018-10-22 stsp int errcode;
3702 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3703 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3704 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3706 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3707 1a76625f 2018-10-22 stsp if (errcode)
3708 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3710 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3711 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3712 d68a0a7d 2018-07-10 stsp goto done;
3715 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3716 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3718 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3719 d68a0a7d 2018-07-10 stsp if (line->annotated)
3720 d68a0a7d 2018-07-10 stsp goto done;
3722 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3723 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3724 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3725 84451b3e 2018-07-10 stsp goto done;
3727 84451b3e 2018-07-10 stsp line->annotated = 1;
3729 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3730 1a76625f 2018-10-22 stsp if (errcode)
3731 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3732 84451b3e 2018-07-10 stsp return err;
3735 84451b3e 2018-07-10 stsp static void *
3736 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3738 18430de3 2018-07-10 stsp const struct got_error *err;
3739 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3740 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3741 1a76625f 2018-10-22 stsp int errcode;
3743 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
3745 61266923 2020-01-14 stsp return (void *)err;
3747 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3748 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3749 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3750 fc06ba56 2019-08-22 stsp err = NULL;
3752 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3753 1a76625f 2018-10-22 stsp if (errcode)
3754 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3755 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3757 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3758 c9beca56 2018-07-22 stsp ta->repo = NULL;
3759 c9beca56 2018-07-22 stsp *ta->complete = 1;
3761 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3762 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3763 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3765 18430de3 2018-07-10 stsp return (void *)err;
3768 245d91c1 2018-07-12 stsp static struct got_object_id *
3769 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3770 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3772 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3774 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3775 8d0fe45a 2019-08-12 stsp return NULL;
3777 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3778 245d91c1 2018-07-12 stsp if (!line->annotated)
3779 245d91c1 2018-07-12 stsp return NULL;
3781 245d91c1 2018-07-12 stsp return line->id;
3784 b880a918 2018-07-10 stsp static const struct got_error *
3785 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3787 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3790 245d91c1 2018-07-12 stsp if (blame->thread) {
3791 1a76625f 2018-10-22 stsp int errcode;
3792 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3793 1a76625f 2018-10-22 stsp if (errcode)
3794 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3795 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3796 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3797 1a76625f 2018-10-22 stsp if (errcode)
3798 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3799 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3800 1a76625f 2018-10-22 stsp if (errcode)
3801 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3802 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3803 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3804 245d91c1 2018-07-12 stsp err = NULL;
3805 245d91c1 2018-07-12 stsp blame->thread = NULL;
3807 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3808 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3809 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3811 245d91c1 2018-07-12 stsp if (blame->f) {
3812 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3813 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3814 245d91c1 2018-07-12 stsp blame->f = NULL;
3816 57670559 2018-12-23 stsp if (blame->lines) {
3817 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3818 57670559 2018-12-23 stsp free(blame->lines[i].id);
3819 57670559 2018-12-23 stsp free(blame->lines);
3820 57670559 2018-12-23 stsp blame->lines = NULL;
3822 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3823 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3825 245d91c1 2018-07-12 stsp return err;
3828 245d91c1 2018-07-12 stsp static const struct got_error *
3829 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
3831 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
3832 fc06ba56 2019-08-22 stsp int *done = arg;
3833 fc06ba56 2019-08-22 stsp int errcode;
3835 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3836 fc06ba56 2019-08-22 stsp if (errcode)
3837 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3838 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
3840 fc06ba56 2019-08-22 stsp if (*done)
3841 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
3843 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3844 fc06ba56 2019-08-22 stsp if (errcode)
3845 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3846 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
3848 fc06ba56 2019-08-22 stsp return err;
3851 fc06ba56 2019-08-22 stsp static const struct got_error *
3852 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3853 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3854 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3855 245d91c1 2018-07-12 stsp struct got_repository *repo)
3857 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3858 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3859 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3860 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3861 15a94983 2018-12-23 stsp int obj_type;
3863 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3865 15a94983 2018-12-23 stsp return err;
3867 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3869 84451b3e 2018-07-10 stsp goto done;
3871 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3872 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3873 84451b3e 2018-07-10 stsp goto done;
3876 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3878 a70480e0 2018-06-23 stsp goto done;
3879 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3880 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3881 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3882 84451b3e 2018-07-10 stsp goto done;
3884 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3885 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3886 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
3887 84451b3e 2018-07-10 stsp goto done;
3889 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3890 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3891 b02560ec 2019-08-19 stsp blame->nlines--;
3893 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3894 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3895 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3896 84451b3e 2018-07-10 stsp goto done;
3899 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3901 bd24772e 2018-07-11 stsp goto done;
3903 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3904 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3905 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3906 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3907 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3908 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3909 245d91c1 2018-07-12 stsp goto done;
3911 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3913 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3914 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3915 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3916 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3917 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
3918 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
3919 245d91c1 2018-07-12 stsp *blame_complete = 0;
3923 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3924 27d434c2 2018-09-15 stsp free(obj_id);
3926 245d91c1 2018-07-12 stsp stop_blame(blame);
3927 245d91c1 2018-07-12 stsp return err;
3930 245d91c1 2018-07-12 stsp static const struct got_error *
3931 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3932 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3933 8b473291 2019-02-21 stsp struct got_repository *repo)
3935 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3936 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3938 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3940 c4843652 2019-08-12 stsp s->path = strdup(path);
3941 c4843652 2019-08-12 stsp if (s->path == NULL)
3942 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
3944 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3945 c4843652 2019-08-12 stsp if (err) {
3946 c4843652 2019-08-12 stsp free(s->path);
3947 7cbe629d 2018-08-04 stsp return err;
3950 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3951 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3952 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3953 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3954 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3955 fb2756b9 2018-08-04 stsp s->repo = repo;
3956 8b473291 2019-02-21 stsp s->refs = refs;
3957 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3958 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3960 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
3961 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3962 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3963 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3965 11b20872 2019-11-08 stsp return err;
3968 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3969 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3970 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3971 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3972 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3974 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3975 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3976 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3977 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3980 e5a0f69f 2018-08-18 stsp static const struct got_error *
3981 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3983 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3984 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3986 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3987 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3989 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3990 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3991 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3992 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3993 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3996 e5a0f69f 2018-08-18 stsp free(s->path);
3997 11b20872 2019-11-08 stsp free_colors(&s->colors);
3999 e5a0f69f 2018-08-18 stsp return err;
4002 7cbe629d 2018-08-04 stsp static const struct got_error *
4003 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4005 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4007 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4008 6c4c42e0 2019-06-24 stsp return NULL;
4011 6c4c42e0 2019-06-24 stsp static const struct got_error *
4012 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4014 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4015 6c4c42e0 2019-06-24 stsp int lineno;
4017 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4018 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
4019 6c4c42e0 2019-06-24 stsp return NULL;
4022 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4023 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4024 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4026 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4028 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4029 6c4c42e0 2019-06-24 stsp lineno = 1;
4031 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4034 6c4c42e0 2019-06-24 stsp while (1) {
4035 6c4c42e0 2019-06-24 stsp char *line = NULL;
4036 6c4c42e0 2019-06-24 stsp off_t offset;
4037 6c4c42e0 2019-06-24 stsp size_t len;
4039 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4040 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4041 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
4042 6c4c42e0 2019-06-24 stsp free(line);
4046 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4047 6c4c42e0 2019-06-24 stsp lineno = 1;
4049 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4052 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4053 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4054 6c4c42e0 2019-06-24 stsp free(line);
4055 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4057 6c4c42e0 2019-06-24 stsp free(line);
4058 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
4059 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
4060 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
4061 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4062 6c4c42e0 2019-06-24 stsp free(line);
4065 6c4c42e0 2019-06-24 stsp free(line);
4066 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4072 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4073 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4074 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4077 6c4c42e0 2019-06-24 stsp return NULL;
4080 6c4c42e0 2019-06-24 stsp static const struct got_error *
4081 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4083 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4084 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4085 2b380cc8 2018-10-24 stsp int errcode;
4087 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4088 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4089 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4090 2b380cc8 2018-10-24 stsp if (errcode)
4091 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4093 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4096 51fe7530 2019-08-19 stsp if (s->blame_complete)
4097 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4099 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4100 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4101 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
4102 11b20872 2019-11-08 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4104 669b5ffa 2018-10-07 stsp view_vborder(view);
4105 e5a0f69f 2018-08-18 stsp return err;
4108 e5a0f69f 2018-08-18 stsp static const struct got_error *
4109 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4110 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4112 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4113 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4114 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4115 669b5ffa 2018-10-07 stsp int begin_x = 0;
4117 e5a0f69f 2018-08-18 stsp switch (ch) {
4119 1e37a5c2 2019-05-12 jcs s->done = 1;
4122 1e37a5c2 2019-05-12 jcs case KEY_UP:
4123 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4124 1e37a5c2 2019-05-12 jcs s->selected_line--;
4125 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4126 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4127 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4129 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4130 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4131 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4134 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4135 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4136 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4138 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4141 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4142 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4143 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4144 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4145 1e37a5c2 2019-05-12 jcs s->selected_line++;
4146 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4147 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4148 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4151 1e37a5c2 2019-05-12 jcs case 'p': {
4152 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4153 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4154 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4155 1e37a5c2 2019-05-12 jcs if (id == NULL)
4157 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4158 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4159 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4160 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4161 1e37a5c2 2019-05-12 jcs int obj_type;
4162 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4163 1e37a5c2 2019-05-12 jcs s->repo, id);
4166 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4167 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4168 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4169 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4172 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4173 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4174 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4175 e5a0f69f 2018-08-18 stsp if (err) {
4176 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4177 1e37a5c2 2019-05-12 jcs err = NULL;
4178 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4181 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4183 1e37a5c2 2019-05-12 jcs free(blob_id);
4184 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4185 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4186 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4189 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4191 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4193 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4194 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4196 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4201 1e37a5c2 2019-05-12 jcs s->done = 1;
4202 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4203 1e37a5c2 2019-05-12 jcs s->done = 0;
4204 1e37a5c2 2019-05-12 jcs if (thread_err)
4206 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4207 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4208 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4209 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4210 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
4211 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
4216 1e37a5c2 2019-05-12 jcs case 'B': {
4217 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4218 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4219 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4221 1e37a5c2 2019-05-12 jcs s->done = 1;
4222 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4223 1e37a5c2 2019-05-12 jcs s->done = 0;
4224 1e37a5c2 2019-05-12 jcs if (thread_err)
4226 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4227 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4228 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4229 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4230 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4231 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4232 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
4233 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
4238 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4239 1e37a5c2 2019-05-12 jcs case '\r': {
4240 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4241 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4242 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4243 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4244 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4245 1e37a5c2 2019-05-12 jcs if (id == NULL)
4247 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4250 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4251 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4252 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4253 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4254 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4255 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4256 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4257 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4260 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4261 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
4262 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4264 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4267 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4268 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4271 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4273 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4276 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4277 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4279 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4284 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4286 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4287 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4288 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4291 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4292 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4293 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4294 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4297 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4298 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4299 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4300 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4302 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4303 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4304 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4306 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4307 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4308 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4309 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4315 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4318 a70480e0 2018-06-23 stsp static const struct got_error *
4319 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4321 a70480e0 2018-06-23 stsp const struct got_error *error;
4322 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4323 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4324 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4325 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4326 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4327 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4329 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4331 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4333 a70480e0 2018-06-23 stsp #ifndef PROFILE
4334 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4335 8e94dd5b 2019-01-04 stsp NULL) == -1)
4336 a70480e0 2018-06-23 stsp err(1, "pledge");
4339 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4340 a70480e0 2018-06-23 stsp switch (ch) {
4342 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4345 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4346 69069811 2018-08-02 stsp if (repo_path == NULL)
4347 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4351 17020d27 2019-03-07 stsp usage_blame();
4352 a70480e0 2018-06-23 stsp /* NOTREACHED */
4356 a70480e0 2018-06-23 stsp argc -= optind;
4357 a70480e0 2018-06-23 stsp argv += optind;
4359 69069811 2018-08-02 stsp if (argc == 1)
4360 69069811 2018-08-02 stsp path = argv[0];
4362 a70480e0 2018-06-23 stsp usage_blame();
4364 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4365 69069811 2018-08-02 stsp if (cwd == NULL) {
4366 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4367 69069811 2018-08-02 stsp goto done;
4369 69069811 2018-08-02 stsp if (repo_path == NULL) {
4370 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4371 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4372 69069811 2018-08-02 stsp goto done;
4374 eb41ed75 2019-02-05 stsp error = NULL;
4375 eb41ed75 2019-02-05 stsp if (worktree) {
4376 eb41ed75 2019-02-05 stsp repo_path =
4377 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4378 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
4379 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4380 eb41ed75 2019-02-05 stsp if (error)
4381 eb41ed75 2019-02-05 stsp goto done;
4383 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4384 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
4385 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4386 eb41ed75 2019-02-05 stsp goto done;
4391 a915003a 2019-02-05 stsp init_curses();
4393 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4394 c02c541e 2019-03-29 stsp if (error != NULL)
4395 8e94dd5b 2019-01-04 stsp goto done;
4397 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4398 c02c541e 2019-03-29 stsp if (error)
4399 92205607 2019-01-04 stsp goto done;
4401 eb41ed75 2019-02-05 stsp if (worktree) {
4402 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4403 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4404 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4405 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4406 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4407 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4408 eb41ed75 2019-02-05 stsp path) == -1) {
4409 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4410 eb41ed75 2019-02-05 stsp goto done;
4412 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4415 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4417 eb41ed75 2019-02-05 stsp if (error)
4418 69069811 2018-08-02 stsp goto done;
4420 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4421 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4422 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4423 a70480e0 2018-06-23 stsp if (error != NULL)
4424 66b4983c 2018-06-23 stsp goto done;
4425 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4426 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4428 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4429 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4431 a19e88aa 2018-06-23 stsp if (error != NULL)
4432 8b473291 2019-02-21 stsp goto done;
4434 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4435 8b473291 2019-02-21 stsp if (error)
4436 a19e88aa 2018-06-23 stsp goto done;
4438 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4439 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4440 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4441 e1cd8fed 2018-08-01 stsp goto done;
4443 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4444 7cbe629d 2018-08-04 stsp if (error)
4445 7cbe629d 2018-08-04 stsp goto done;
4446 12314ad4 2019-08-31 stsp if (worktree) {
4447 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4448 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4449 12314ad4 2019-08-31 stsp worktree = NULL;
4451 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4453 69069811 2018-08-02 stsp free(repo_path);
4454 69069811 2018-08-02 stsp free(cwd);
4455 a70480e0 2018-06-23 stsp free(commit_id);
4456 eb41ed75 2019-02-05 stsp if (worktree)
4457 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4459 a70480e0 2018-06-23 stsp got_repo_close(repo);
4460 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4461 a70480e0 2018-06-23 stsp return error;
4464 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4465 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4466 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4467 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4468 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4469 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4470 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4471 f26dddb7 2019-11-08 stsp int isroot, struct tog_colors *colors)
4473 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4474 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4475 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4476 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4477 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4479 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4481 f7d12f7e 2018-08-01 stsp werase(view->window);
4483 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4484 ffd1d5e5 2018-06-23 stsp return NULL;
4486 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4488 ffd1d5e5 2018-06-23 stsp return err;
4489 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4490 a3404814 2018-09-02 stsp wstandout(view->window);
4491 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4493 11b20872 2019-11-08 stsp wattr_on(view->window,
4494 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4495 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4497 11b20872 2019-11-08 stsp wattr_off(view->window,
4498 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4499 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4500 a3404814 2018-09-02 stsp wstandend(view->window);
4501 2550e4c3 2018-07-13 stsp free(wline);
4502 2550e4c3 2018-07-13 stsp wline = NULL;
4503 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4504 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4505 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4506 ffd1d5e5 2018-06-23 stsp return NULL;
4507 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4509 ce52c690 2018-06-23 stsp return err;
4510 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4511 2550e4c3 2018-07-13 stsp free(wline);
4512 2550e4c3 2018-07-13 stsp wline = NULL;
4513 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4514 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4515 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4516 ffd1d5e5 2018-06-23 stsp return NULL;
4517 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4518 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4519 a1eca9bb 2018-06-23 stsp return NULL;
4521 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4522 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4523 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4524 0cf4efb1 2018-09-29 stsp if (view->focussed)
4525 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4526 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4528 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4529 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4530 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4531 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4532 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4533 ffd1d5e5 2018-06-23 stsp return NULL;
4537 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4540 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4541 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4542 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
4543 848d6979 2019-08-12 stsp const char *modestr = "";
4544 56e0773d 2019-11-28 stsp mode_t mode;
4546 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4547 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4549 1d13200f 2018-07-12 stsp if (show_ids) {
4550 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4551 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4553 638f9024 2019-05-13 stsp return got_error_from_errno(
4554 230a42bd 2019-05-11 jcs "got_object_id_str");
4556 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4557 63c5ca5d 2019-08-24 stsp modestr = "$";
4558 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
4559 848d6979 2019-08-12 stsp modestr = "@";
4560 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4561 848d6979 2019-08-12 stsp modestr = "/";
4562 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4563 848d6979 2019-08-12 stsp modestr = "*";
4564 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4565 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te), modestr) == -1) {
4566 1d13200f 2018-07-12 stsp free(id_str);
4567 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4569 1d13200f 2018-07-12 stsp free(id_str);
4570 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4571 ffd1d5e5 2018-06-23 stsp if (err) {
4572 ffd1d5e5 2018-06-23 stsp free(line);
4575 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4576 0cf4efb1 2018-09-29 stsp if (view->focussed)
4577 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4578 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4580 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4582 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4583 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4584 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4586 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4587 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4588 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4589 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4590 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4591 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4592 ffd1d5e5 2018-06-23 stsp free(line);
4593 2550e4c3 2018-07-13 stsp free(wline);
4594 2550e4c3 2018-07-13 stsp wline = NULL;
4596 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4597 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4598 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4602 ffd1d5e5 2018-06-23 stsp return err;
4605 ffd1d5e5 2018-06-23 stsp static void
4606 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4607 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4608 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int isroot)
4610 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4613 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4616 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, 0);
4617 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4618 ffd1d5e5 2018-06-23 stsp if (!isroot)
4619 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4624 56e0773d 2019-11-28 stsp while (*first_displayed_entry && i < maxscroll) {
4625 56e0773d 2019-11-28 stsp *first_displayed_entry = got_tree_entry_get_prev(tree,
4626 56e0773d 2019-11-28 stsp *first_displayed_entry);
4629 56e0773d 2019-11-28 stsp if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4630 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4633 768394f3 2019-01-24 stsp static int
4634 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4635 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4636 56e0773d 2019-11-28 stsp struct got_tree_object *tree)
4638 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4639 ffd1d5e5 2018-06-23 stsp int n = 0;
4641 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4642 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, *first_displayed_entry);
4644 56e0773d 2019-11-28 stsp next = got_object_tree_get_first_entry(tree);
4646 768394f3 2019-01-24 stsp last = last_displayed_entry;
4647 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4648 56e0773d 2019-11-28 stsp last = got_tree_entry_get_next(tree, last);
4649 768394f3 2019-01-24 stsp if (last) {
4650 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4651 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, next);
4657 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4658 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4659 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4661 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4662 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4663 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4665 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4666 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4667 56e0773d 2019-11-28 stsp + 1 /* slash */;
4669 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4671 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4672 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4673 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4675 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4676 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4677 d9765a41 2018-06-23 stsp while (pt) {
4678 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4679 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4680 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4681 cb2ebc8a 2018-06-23 stsp goto done;
4683 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4684 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4685 cb2ebc8a 2018-06-23 stsp goto done;
4687 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4690 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4691 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4692 ce52c690 2018-06-23 stsp goto done;
4696 ce52c690 2018-06-23 stsp if (err) {
4697 ce52c690 2018-06-23 stsp free(*path);
4698 ce52c690 2018-06-23 stsp *path = NULL;
4700 ce52c690 2018-06-23 stsp return err;
4703 ce52c690 2018-06-23 stsp static const struct got_error *
4704 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4705 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4706 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4707 8b473291 2019-02-21 stsp struct got_repository *repo)
4709 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4710 ce52c690 2018-06-23 stsp char *path;
4711 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4713 a0de39f3 2019-08-09 stsp *new_view = NULL;
4715 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4717 ce52c690 2018-06-23 stsp return err;
4719 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4720 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4721 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4722 83ce39e3 2019-08-12 stsp goto done;
4725 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4726 e5a0f69f 2018-08-18 stsp if (err) {
4727 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4728 fc06ba56 2019-08-22 stsp err = NULL;
4729 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4731 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4733 83ce39e3 2019-08-12 stsp free(path);
4734 69efd4c4 2018-07-18 stsp return err;
4737 69efd4c4 2018-07-18 stsp static const struct got_error *
4738 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4739 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4740 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4741 8b473291 2019-02-21 stsp struct got_repository *repo)
4743 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4744 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4745 69efd4c4 2018-07-18 stsp char *path;
4747 a0de39f3 2019-08-09 stsp *new_view = NULL;
4749 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4750 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4751 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4753 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4755 69efd4c4 2018-07-18 stsp return err;
4757 b672a97a 2020-01-27 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0, 0);
4759 e5a0f69f 2018-08-18 stsp view_close(log_view);
4761 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4762 cb2ebc8a 2018-06-23 stsp free(path);
4763 cb2ebc8a 2018-06-23 stsp return err;
4766 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4767 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4768 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4769 8b473291 2019-02-21 stsp struct got_repository *repo)
4771 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4772 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4773 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4775 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4777 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4778 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4779 ffd1d5e5 2018-06-23 stsp goto done;
4781 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4782 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4783 ffd1d5e5 2018-06-23 stsp goto done;
4786 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4787 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4788 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4789 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4790 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4791 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4792 6484ec90 2018-09-29 stsp goto done;
4794 8b473291 2019-02-21 stsp s->refs = refs;
4795 fb2756b9 2018-08-04 stsp s->repo = repo;
4797 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4799 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4800 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
4801 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
4802 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4804 c0b01bdb 2019-11-08 stsp goto done;
4805 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4806 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
4807 c0b01bdb 2019-11-08 stsp if (err) {
4808 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4809 c0b01bdb 2019-11-08 stsp goto done;
4811 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
4812 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
4813 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4814 c0b01bdb 2019-11-08 stsp if (err) {
4815 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4816 c0b01bdb 2019-11-08 stsp goto done;
4819 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
4820 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
4821 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4822 11b20872 2019-11-08 stsp if (err) {
4823 11b20872 2019-11-08 stsp free_colors(&s->colors);
4824 11b20872 2019-11-08 stsp goto done;
4827 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4828 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4829 c0b01bdb 2019-11-08 stsp if (err) {
4830 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4831 c0b01bdb 2019-11-08 stsp goto done;
4835 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4836 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4837 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4838 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4839 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4841 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4842 6484ec90 2018-09-29 stsp if (err) {
4843 fb2756b9 2018-08-04 stsp free(s->tree_label);
4844 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4846 ad80ab7b 2018-08-04 stsp return err;
4849 e5a0f69f 2018-08-18 stsp static const struct got_error *
4850 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4852 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4854 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4855 fb2756b9 2018-08-04 stsp free(s->tree_label);
4856 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4857 6484ec90 2018-09-29 stsp free(s->commit_id);
4858 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4859 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4860 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4861 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4862 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4863 ad80ab7b 2018-08-04 stsp free(parent);
4866 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4867 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4868 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4870 7c32bd05 2019-06-22 stsp return NULL;
4873 7c32bd05 2019-06-22 stsp static const struct got_error *
4874 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4876 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4878 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4879 7c32bd05 2019-06-22 stsp return NULL;
4882 7c32bd05 2019-06-22 stsp static int
4883 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4885 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4887 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4891 7c32bd05 2019-06-22 stsp static const struct got_error *
4892 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4894 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4895 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
4897 7c32bd05 2019-06-22 stsp if (!view->searching) {
4898 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4899 7c32bd05 2019-06-22 stsp return NULL;
4902 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4903 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4904 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4905 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
4906 56e0773d 2019-11-28 stsp s->selected_entry);
4908 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4910 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
4911 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4913 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
4914 56e0773d 2019-11-28 stsp s->selected_entry);
4917 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4918 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4920 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4923 7c32bd05 2019-06-22 stsp while (1) {
4924 56e0773d 2019-11-28 stsp if (te == NULL) {
4925 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4926 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4927 ac66afb8 2019-06-24 stsp return NULL;
4929 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4930 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4932 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4935 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
4936 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4937 56e0773d 2019-11-28 stsp s->matched_entry = te;
4941 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4942 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
4944 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
4947 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4948 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4949 7c32bd05 2019-06-22 stsp s->selected = 0;
4952 e5a0f69f 2018-08-18 stsp return NULL;
4955 ad80ab7b 2018-08-04 stsp static const struct got_error *
4956 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4958 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4959 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4960 e5a0f69f 2018-08-18 stsp char *parent_path;
4962 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4964 e5a0f69f 2018-08-18 stsp return err;
4966 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4967 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4968 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4969 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
4970 bddb1296 2019-11-08 stsp &s->colors);
4971 e5a0f69f 2018-08-18 stsp free(parent_path);
4973 669b5ffa 2018-10-07 stsp view_vborder(view);
4974 e5a0f69f 2018-08-18 stsp return err;
4977 e5a0f69f 2018-08-18 stsp static const struct got_error *
4978 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4979 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4981 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4982 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4983 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4984 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4986 e5a0f69f 2018-08-18 stsp switch (ch) {
4988 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4991 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4993 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4994 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4995 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4996 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4997 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4998 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4999 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5001 1e37a5c2 2019-05-12 jcs return err;
5002 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
5004 1e37a5c2 2019-05-12 jcs view_close(log_view);
5007 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
5008 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
5010 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5013 1e37a5c2 2019-05-12 jcs case KEY_UP:
5014 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5015 1e37a5c2 2019-05-12 jcs s->selected--;
5016 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
5019 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
5021 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
5022 56e0773d 2019-11-28 stsp s->tree, s->tree == s->root);
5024 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5025 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
5026 56e0773d 2019-11-28 stsp MAX(0, view->nlines - 4 - s->selected), s->tree,
5027 1e37a5c2 2019-05-12 jcs s->tree == s->root);
5028 1e37a5c2 2019-05-12 jcs s->selected = 0;
5029 56e0773d 2019-11-28 stsp if (got_object_tree_get_first_entry(s->tree) ==
5030 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
5031 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
5034 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5035 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5036 1e37a5c2 2019-05-12 jcs s->selected++;
5039 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5041 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5043 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
5044 56e0773d 2019-11-28 stsp s->last_displayed_entry, s->tree);
5046 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5047 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5049 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5050 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5051 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5054 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
5055 56e0773d 2019-11-28 stsp view->nlines, s->last_displayed_entry, s->tree);
5056 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
5057 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
5058 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
5059 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
5061 1e37a5c2 2019-05-12 jcs ndisplayed++;
5062 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5063 1e37a5c2 2019-05-12 jcs } while (te);
5064 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
5067 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5069 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5070 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5071 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5072 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5073 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5075 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5076 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5078 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5079 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5080 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5081 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5082 1e37a5c2 2019-05-12 jcs s->selected_entry =
5083 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5084 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5085 1e37a5c2 2019-05-12 jcs free(parent);
5086 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5087 56e0773d 2019-11-28 stsp s->selected_entry))) {
5088 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5089 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5090 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5093 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
5094 941e9f74 2019-05-21 stsp if (err) {
5095 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5098 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5099 56e0773d 2019-11-28 stsp s->selected_entry))) {
5100 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5101 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5102 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5104 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5105 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5106 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
5109 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5110 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5112 669b5ffa 2018-10-07 stsp return err;
5113 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
5114 669b5ffa 2018-10-07 stsp if (err) {
5115 1e37a5c2 2019-05-12 jcs view_close(blame_view);
5118 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
5119 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
5121 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5124 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5125 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
5126 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5132 ffd1d5e5 2018-06-23 stsp return err;
5135 ffd1d5e5 2018-06-23 stsp __dead static void
5136 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5139 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
5140 ffd1d5e5 2018-06-23 stsp getprogname());
5144 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5145 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5147 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5148 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5149 8b473291 2019-02-21 stsp struct got_reflist_head refs;
5150 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
5151 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5152 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
5153 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5154 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5156 5221c383 2018-08-01 stsp struct tog_view *view;
5158 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
5160 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
5161 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5162 d188b9a6 2019-01-04 stsp NULL) == -1)
5163 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
5166 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
5167 ffd1d5e5 2018-06-23 stsp switch (ch) {
5169 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5172 17020d27 2019-03-07 stsp usage_tree();
5173 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5177 ffd1d5e5 2018-06-23 stsp argc -= optind;
5178 ffd1d5e5 2018-06-23 stsp argv += optind;
5180 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
5181 52185f70 2019-02-05 stsp struct got_worktree *worktree;
5182 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
5183 52185f70 2019-02-05 stsp if (cwd == NULL)
5184 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
5185 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5186 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5187 52185f70 2019-02-05 stsp goto done;
5188 52185f70 2019-02-05 stsp if (worktree) {
5189 52185f70 2019-02-05 stsp free(cwd);
5190 52185f70 2019-02-05 stsp repo_path =
5191 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5192 52185f70 2019-02-05 stsp got_worktree_close(worktree);
5194 52185f70 2019-02-05 stsp repo_path = cwd;
5195 52185f70 2019-02-05 stsp if (repo_path == NULL) {
5196 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5197 52185f70 2019-02-05 stsp goto done;
5199 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
5200 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
5201 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
5202 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
5204 f13374f4 2020-01-14 stsp usage_tree();
5206 a915003a 2019-02-05 stsp init_curses();
5208 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5209 c02c541e 2019-03-29 stsp if (error != NULL)
5210 52185f70 2019-02-05 stsp goto done;
5212 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5213 c02c541e 2019-03-29 stsp if (error)
5214 52185f70 2019-02-05 stsp goto done;
5216 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5217 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5218 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5219 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5220 ffd1d5e5 2018-06-23 stsp goto done;
5222 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5223 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5224 ffd1d5e5 2018-06-23 stsp goto done;
5226 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5227 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5228 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5229 8b473291 2019-02-21 stsp goto done;
5231 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5232 8b473291 2019-02-21 stsp if (error)
5233 ffd1d5e5 2018-06-23 stsp goto done;
5235 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5236 5221c383 2018-08-01 stsp if (view == NULL) {
5237 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5238 5221c383 2018-08-01 stsp goto done;
5240 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
5241 ad80ab7b 2018-08-04 stsp if (error)
5242 ad80ab7b 2018-08-04 stsp goto done;
5243 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5245 52185f70 2019-02-05 stsp free(repo_path);
5246 ffd1d5e5 2018-06-23 stsp free(commit_id);
5247 ffd1d5e5 2018-06-23 stsp if (commit)
5248 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5250 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5252 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5253 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5254 ffd1d5e5 2018-06-23 stsp return error;
5257 ce5b7c56 2019-07-09 stsp static void
5258 ce5b7c56 2019-07-09 stsp list_commands(void)
5262 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
5263 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
5264 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
5265 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
5267 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
5270 4ed7e80c 2018-05-20 stsp __dead static void
5271 ce5b7c56 2019-07-09 stsp usage(int hflag)
5273 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5274 53ccebc2 2019-07-30 stsp getprogname());
5275 ce5b7c56 2019-07-09 stsp if (hflag)
5276 ce5b7c56 2019-07-09 stsp list_commands();
5280 c2301be8 2018-04-30 stsp static char **
5281 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
5283 c2301be8 2018-04-30 stsp char **argv;
5284 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
5286 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
5287 c2301be8 2018-04-30 stsp if (argv == NULL)
5288 c2301be8 2018-04-30 stsp err(1, "calloc");
5289 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
5290 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
5291 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5292 c2301be8 2018-04-30 stsp if (arg1) {
5293 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
5294 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
5295 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5298 c2301be8 2018-04-30 stsp return argv;
5302 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
5304 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
5305 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
5306 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
5307 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
5308 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
5309 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
5310 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
5313 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
5315 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5316 9f7d7167 2018-04-29 stsp switch (ch) {
5318 9f7d7167 2018-04-29 stsp hflag = 1;
5321 53ccebc2 2019-07-30 stsp Vflag = 1;
5324 ce5b7c56 2019-07-09 stsp usage(hflag);
5325 9f7d7167 2018-04-29 stsp /* NOTREACHED */
5329 9f7d7167 2018-04-29 stsp argc -= optind;
5330 9f7d7167 2018-04-29 stsp argv += optind;
5331 9f7d7167 2018-04-29 stsp optind = 0;
5332 c2301be8 2018-04-30 stsp optreset = 1;
5334 53ccebc2 2019-07-30 stsp if (Vflag) {
5335 53ccebc2 2019-07-30 stsp got_version_print_str();
5339 c2301be8 2018-04-30 stsp if (argc == 0) {
5340 f29d3e89 2018-06-23 stsp if (hflag)
5341 ce5b7c56 2019-07-09 stsp usage(hflag);
5342 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
5343 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
5344 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
5349 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
5350 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
5351 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
5352 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
5353 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
5358 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
5359 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
5360 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
5361 ce5b7c56 2019-07-09 stsp list_commands();
5366 3642c4c6 2019-07-09 stsp if (hflag)
5367 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
5369 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5372 c2301be8 2018-04-30 stsp free(cmd_argv);
5373 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
5374 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);