2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/ioctl.h>
22 #if defined(__FreeBSD__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
52 #include "got_opentemp.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
59 #include "got_worktree.h"
61 //#define update_panels() (0)
62 //#define doupdate() (0)
65 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
69 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
73 #define CTRL(x) ((x) & 0x1f)
77 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
82 const struct got_error
*(*cmd_main
)(int, char *[]);
83 void (*cmd_usage
)(void);
86 __dead
static void usage(int, int);
87 __dead
static void usage_log(void);
88 __dead
static void usage_diff(void);
89 __dead
static void usage_blame(void);
90 __dead
static void usage_tree(void);
91 __dead
static void usage_ref(void);
93 static const struct got_error
* cmd_log(int, char *[]);
94 static const struct got_error
* cmd_diff(int, char *[]);
95 static const struct got_error
* cmd_blame(int, char *[]);
96 static const struct got_error
* cmd_tree(int, char *[]);
97 static const struct got_error
* cmd_ref(int, char *[]);
99 static struct tog_cmd tog_commands
[] = {
100 { "log", cmd_log
, usage_log
},
101 { "diff", cmd_diff
, usage_diff
},
102 { "blame", cmd_blame
, usage_blame
},
103 { "tree", cmd_tree
, usage_tree
},
104 { "ref", cmd_ref
, usage_ref
},
115 #define TOG_EOF_STRING "(END)"
117 struct commit_queue_entry
{
118 TAILQ_ENTRY(commit_queue_entry
) entry
;
119 struct got_object_id
*id
;
120 struct got_commit_object
*commit
;
123 TAILQ_HEAD(commit_queue_head
, commit_queue_entry
);
124 struct commit_queue
{
126 struct commit_queue_head head
;
130 STAILQ_ENTRY(tog_color
) entry
;
134 STAILQ_HEAD(tog_colors
, tog_color
);
136 static struct got_reflist_head tog_refs
= TAILQ_HEAD_INITIALIZER(tog_refs
);
137 static struct got_reflist_object_id_map
*tog_refs_idmap
;
139 static const struct got_error
*
140 tog_load_refs(struct got_repository
*repo
)
142 const struct got_error
*err
;
144 err
= got_ref_list(&tog_refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
148 return got_reflist_object_id_map_create(&tog_refs_idmap
, &tog_refs
,
155 if (tog_refs_idmap
) {
156 got_reflist_object_id_map_free(tog_refs_idmap
);
157 tog_refs_idmap
= NULL
;
159 got_ref_list_free(&tog_refs
);
162 static const struct got_error
*
163 add_color(struct tog_colors
*colors
, const char *pattern
,
164 int idx
, short color
)
166 const struct got_error
*err
= NULL
;
167 struct tog_color
*tc
;
170 if (idx
< 1 || idx
> COLOR_PAIRS
- 1)
173 init_pair(idx
, color
, -1);
175 tc
= calloc(1, sizeof(*tc
));
177 return got_error_from_errno("calloc");
178 regerr
= regcomp(&tc
->regex
, pattern
,
179 REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
);
181 static char regerr_msg
[512];
182 static char err_msg
[512];
183 regerror(regerr
, &tc
->regex
, regerr_msg
,
185 snprintf(err_msg
, sizeof(err_msg
), "regcomp: %s",
187 err
= got_error_msg(GOT_ERR_REGEX
, err_msg
);
192 STAILQ_INSERT_HEAD(colors
, tc
, entry
);
197 free_colors(struct tog_colors
*colors
)
199 struct tog_color
*tc
;
201 while (!STAILQ_EMPTY(colors
)) {
202 tc
= STAILQ_FIRST(colors
);
203 STAILQ_REMOVE_HEAD(colors
, entry
);
210 get_color(struct tog_colors
*colors
, int colorpair
)
212 struct tog_color
*tc
= NULL
;
214 STAILQ_FOREACH(tc
, colors
, entry
) {
215 if (tc
->colorpair
== colorpair
)
223 default_color_value(const char *envvar
)
225 if (strcmp(envvar
, "TOG_COLOR_DIFF_MINUS") == 0)
226 return COLOR_MAGENTA
;
227 if (strcmp(envvar
, "TOG_COLOR_DIFF_PLUS") == 0)
229 if (strcmp(envvar
, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
231 if (strcmp(envvar
, "TOG_COLOR_DIFF_META") == 0)
233 if (strcmp(envvar
, "TOG_COLOR_TREE_SUBMODULE") == 0)
234 return COLOR_MAGENTA
;
235 if (strcmp(envvar
, "TOG_COLOR_TREE_SYMLINK") == 0)
236 return COLOR_MAGENTA
;
237 if (strcmp(envvar
, "TOG_COLOR_TREE_DIRECTORY") == 0)
239 if (strcmp(envvar
, "TOG_COLOR_TREE_EXECUTABLE") == 0)
241 if (strcmp(envvar
, "TOG_COLOR_COMMIT") == 0)
243 if (strcmp(envvar
, "TOG_COLOR_AUTHOR") == 0)
245 if (strcmp(envvar
, "TOG_COLOR_DATE") == 0)
247 if (strcmp(envvar
, "TOG_COLOR_REFS_HEADS") == 0)
249 if (strcmp(envvar
, "TOG_COLOR_REFS_TAGS") == 0)
250 return COLOR_MAGENTA
;
251 if (strcmp(envvar
, "TOG_COLOR_REFS_REMOTES") == 0)
258 get_color_value(const char *envvar
)
260 const char *val
= getenv(envvar
);
263 return default_color_value(envvar
);
265 if (strcasecmp(val
, "black") == 0)
267 if (strcasecmp(val
, "red") == 0)
269 if (strcasecmp(val
, "green") == 0)
271 if (strcasecmp(val
, "yellow") == 0)
273 if (strcasecmp(val
, "blue") == 0)
275 if (strcasecmp(val
, "magenta") == 0)
276 return COLOR_MAGENTA
;
277 if (strcasecmp(val
, "cyan") == 0)
279 if (strcasecmp(val
, "white") == 0)
281 if (strcasecmp(val
, "default") == 0)
284 return default_color_value(envvar
);
288 struct tog_diff_view_state
{
289 struct got_object_id
*id1
, *id2
;
290 const char *label1
, *label2
;
292 int first_displayed_line
;
293 int last_displayed_line
;
296 int ignore_whitespace
;
298 struct got_repository
*repo
;
299 struct tog_colors colors
;
305 /* passed from log view; may be NULL */
306 struct tog_view
*log_view
;
309 pthread_mutex_t tog_mutex
= PTHREAD_MUTEX_INITIALIZER
;
311 struct tog_log_thread_args
{
312 pthread_cond_t need_commits
;
313 pthread_cond_t commit_loaded
;
316 struct got_commit_graph
*graph
;
317 struct commit_queue
*commits
;
318 const char *in_repo_path
;
319 struct got_object_id
*start_id
;
320 struct got_repository
*repo
;
323 struct commit_queue_entry
**first_displayed_entry
;
324 struct commit_queue_entry
**selected_entry
;
326 int *search_next_done
;
330 struct tog_log_view_state
{
331 struct commit_queue commits
;
332 struct commit_queue_entry
*first_displayed_entry
;
333 struct commit_queue_entry
*last_displayed_entry
;
334 struct commit_queue_entry
*selected_entry
;
339 struct got_repository
*repo
;
340 struct got_object_id
*start_id
;
343 struct tog_log_thread_args thread_args
;
344 struct commit_queue_entry
*matched_entry
;
345 struct commit_queue_entry
*search_entry
;
346 struct tog_colors colors
;
349 #define TOG_COLOR_DIFF_MINUS 1
350 #define TOG_COLOR_DIFF_PLUS 2
351 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
352 #define TOG_COLOR_DIFF_META 4
353 #define TOG_COLOR_TREE_SUBMODULE 5
354 #define TOG_COLOR_TREE_SYMLINK 6
355 #define TOG_COLOR_TREE_DIRECTORY 7
356 #define TOG_COLOR_TREE_EXECUTABLE 8
357 #define TOG_COLOR_COMMIT 9
358 #define TOG_COLOR_AUTHOR 10
359 #define TOG_COLOR_DATE 11
360 #define TOG_COLOR_REFS_HEADS 12
361 #define TOG_COLOR_REFS_TAGS 13
362 #define TOG_COLOR_REFS_REMOTES 14
364 struct tog_blame_cb_args
{
365 struct tog_blame_line
*lines
; /* one per line */
368 struct tog_view
*view
;
369 struct got_object_id
*commit_id
;
373 struct tog_blame_thread_args
{
375 struct got_repository
*repo
;
376 struct tog_blame_cb_args
*cb_args
;
378 got_cancel_cb cancel_cb
;
385 struct tog_blame_line
*lines
;
389 struct tog_blame_thread_args thread_args
;
390 struct tog_blame_cb_args cb_args
;
394 struct tog_blame_view_state
{
395 int first_displayed_line
;
396 int last_displayed_line
;
401 struct got_object_id_queue blamed_commits
;
402 struct got_object_qid
*blamed_commit
;
404 struct got_repository
*repo
;
405 struct got_object_id
*commit_id
;
406 struct tog_blame blame
;
408 struct tog_colors colors
;
411 struct tog_parent_tree
{
412 TAILQ_ENTRY(tog_parent_tree
) entry
;
413 struct got_tree_object
*tree
;
414 struct got_tree_entry
*first_displayed_entry
;
415 struct got_tree_entry
*selected_entry
;
419 TAILQ_HEAD(tog_parent_trees
, tog_parent_tree
);
421 struct tog_tree_view_state
{
423 struct got_object_id
*commit_id
;/* commit which this tree belongs to */
424 struct got_tree_object
*root
; /* the commit's root tree entry */
425 struct got_tree_object
*tree
; /* currently displayed (sub-)tree */
426 struct got_tree_entry
*first_displayed_entry
;
427 struct got_tree_entry
*last_displayed_entry
;
428 struct got_tree_entry
*selected_entry
;
429 int ndisplayed
, selected
, show_ids
;
430 struct tog_parent_trees parents
; /* parent trees of current sub-tree */
432 struct got_repository
*repo
;
433 struct got_tree_entry
*matched_entry
;
434 struct tog_colors colors
;
437 struct tog_reflist_entry
{
438 TAILQ_ENTRY(tog_reflist_entry
) entry
;
439 struct got_reference
*ref
;
443 TAILQ_HEAD(tog_reflist_head
, tog_reflist_entry
);
445 struct tog_ref_view_state
{
446 struct tog_reflist_head refs
;
447 struct tog_reflist_entry
*first_displayed_entry
;
448 struct tog_reflist_entry
*last_displayed_entry
;
449 struct tog_reflist_entry
*selected_entry
;
450 int nrefs
, ndisplayed
, selected
, show_ids
;
451 struct got_repository
*repo
;
452 struct tog_reflist_entry
*matched_entry
;
453 struct tog_colors colors
;
457 * We implement two types of views: parent views and child views.
459 * The 'Tab' key switches focus between a parent view and its child view.
460 * Child views are shown side-by-side to their parent view, provided
461 * there is enough screen estate.
463 * When a new view is opened from within a parent view, this new view
464 * becomes a child view of the parent view, replacing any existing child.
466 * When a new view is opened from within a child view, this new view
467 * becomes a parent view which will obscure the views below until the
468 * user quits the new parent view by typing 'q'.
470 * This list of views contains parent views only.
471 * Child views are only pointed to by their parent view.
473 TAILQ_HEAD(tog_view_list_head
, tog_view
);
476 TAILQ_ENTRY(tog_view
) entry
;
479 int nlines
, ncols
, begin_y
, begin_x
;
480 int lines
, cols
; /* copies of LINES and COLS */
481 int focussed
; /* Only set on one parent or child view at a time. */
483 struct tog_view
*parent
;
484 struct tog_view
*child
;
487 * This flag is initially set on parent views when a new child view
488 * is created. It gets toggled when the 'Tab' key switches focus
489 * between parent and child.
490 * The flag indicates whether focus should be passed on to our child
491 * view if this parent view gets picked for focus after another parent
492 * view was closed. This prevents child views from losing focus in such
497 /* type-specific state */
498 enum tog_view_type type
;
500 struct tog_diff_view_state diff
;
501 struct tog_log_view_state log
;
502 struct tog_blame_view_state blame
;
503 struct tog_tree_view_state tree
;
504 struct tog_ref_view_state ref
;
507 const struct got_error
*(*show
)(struct tog_view
*);
508 const struct got_error
*(*input
)(struct tog_view
**,
509 struct tog_view
*, int);
510 const struct got_error
*(*close
)(struct tog_view
*);
512 const struct got_error
*(*search_start
)(struct tog_view
*);
513 const struct got_error
*(*search_next
)(struct tog_view
*);
516 #define TOG_SEARCH_FORWARD 1
517 #define TOG_SEARCH_BACKWARD 2
518 int search_next_done
;
519 #define TOG_SEARCH_HAVE_MORE 1
520 #define TOG_SEARCH_NO_MORE 2
521 #define TOG_SEARCH_HAVE_NONE 3
526 static const struct got_error
*open_diff_view(struct tog_view
*,
527 struct got_object_id
*, struct got_object_id
*,
528 const char *, const char *, int, int, int, struct tog_view
*,
529 struct got_repository
*);
530 static const struct got_error
*show_diff_view(struct tog_view
*);
531 static const struct got_error
*input_diff_view(struct tog_view
**,
532 struct tog_view
*, int);
533 static const struct got_error
* close_diff_view(struct tog_view
*);
534 static const struct got_error
*search_start_diff_view(struct tog_view
*);
535 static const struct got_error
*search_next_diff_view(struct tog_view
*);
537 static const struct got_error
*open_log_view(struct tog_view
*,
538 struct got_object_id
*, struct got_repository
*,
539 const char *, const char *, int);
540 static const struct got_error
* show_log_view(struct tog_view
*);
541 static const struct got_error
*input_log_view(struct tog_view
**,
542 struct tog_view
*, int);
543 static const struct got_error
*close_log_view(struct tog_view
*);
544 static const struct got_error
*search_start_log_view(struct tog_view
*);
545 static const struct got_error
*search_next_log_view(struct tog_view
*);
547 static const struct got_error
*open_blame_view(struct tog_view
*, char *,
548 struct got_object_id
*, struct got_repository
*);
549 static const struct got_error
*show_blame_view(struct tog_view
*);
550 static const struct got_error
*input_blame_view(struct tog_view
**,
551 struct tog_view
*, int);
552 static const struct got_error
*close_blame_view(struct tog_view
*);
553 static const struct got_error
*search_start_blame_view(struct tog_view
*);
554 static const struct got_error
*search_next_blame_view(struct tog_view
*);
556 static const struct got_error
*open_tree_view(struct tog_view
*,
557 struct got_object_id
*, const char *, struct got_repository
*);
558 static const struct got_error
*show_tree_view(struct tog_view
*);
559 static const struct got_error
*input_tree_view(struct tog_view
**,
560 struct tog_view
*, int);
561 static const struct got_error
*close_tree_view(struct tog_view
*);
562 static const struct got_error
*search_start_tree_view(struct tog_view
*);
563 static const struct got_error
*search_next_tree_view(struct tog_view
*);
565 static const struct got_error
*open_ref_view(struct tog_view
*,
566 struct got_repository
*);
567 static const struct got_error
*show_ref_view(struct tog_view
*);
568 static const struct got_error
*input_ref_view(struct tog_view
**,
569 struct tog_view
*, int);
570 static const struct got_error
*close_ref_view(struct tog_view
*);
571 static const struct got_error
*search_start_ref_view(struct tog_view
*);
572 static const struct got_error
*search_next_ref_view(struct tog_view
*);
574 static volatile sig_atomic_t tog_sigwinch_received
;
575 static volatile sig_atomic_t tog_sigpipe_received
;
576 static volatile sig_atomic_t tog_sigcont_received
;
579 tog_sigwinch(int signo
)
581 tog_sigwinch_received
= 1;
585 tog_sigpipe(int signo
)
587 tog_sigpipe_received
= 1;
591 tog_sigcont(int signo
)
593 tog_sigcont_received
= 1;
596 static const struct got_error
*
597 view_close(struct tog_view
*view
)
599 const struct got_error
*err
= NULL
;
602 view_close(view
->child
);
606 err
= view
->close(view
);
608 del_panel(view
->panel
);
610 delwin(view
->window
);
615 static struct tog_view
*
616 view_open(int nlines
, int ncols
, int begin_y
, int begin_x
,
617 enum tog_view_type type
)
619 struct tog_view
*view
= calloc(1, sizeof(*view
));
627 view
->nlines
= nlines
? nlines
: LINES
- begin_y
;
628 view
->ncols
= ncols
? ncols
: COLS
- begin_x
;
629 view
->begin_y
= begin_y
;
630 view
->begin_x
= begin_x
;
631 view
->window
= newwin(nlines
, ncols
, begin_y
, begin_x
);
632 if (view
->window
== NULL
) {
636 view
->panel
= new_panel(view
->window
);
637 if (view
->panel
== NULL
||
638 set_panel_userptr(view
->panel
, view
) != OK
) {
643 keypad(view
->window
, TRUE
);
648 view_split_begin_x(int begin_x
)
650 if (begin_x
> 0 || COLS
< 120)
652 return (COLS
- MAX(COLS
/ 2, 80));
655 static const struct got_error
*view_resize(struct tog_view
*);
657 static const struct got_error
*
658 view_splitscreen(struct tog_view
*view
)
660 const struct got_error
*err
= NULL
;
663 view
->begin_x
= view_split_begin_x(0);
664 view
->nlines
= LINES
;
665 view
->ncols
= COLS
- view
->begin_x
;
668 err
= view_resize(view
);
672 if (mvwin(view
->window
, view
->begin_y
, view
->begin_x
) == ERR
)
673 return got_error_from_errno("mvwin");
678 static const struct got_error
*
679 view_fullscreen(struct tog_view
*view
)
681 const struct got_error
*err
= NULL
;
685 view
->nlines
= LINES
;
689 err
= view_resize(view
);
693 if (mvwin(view
->window
, view
->begin_y
, view
->begin_x
) == ERR
)
694 return got_error_from_errno("mvwin");
700 view_is_parent_view(struct tog_view
*view
)
702 return view
->parent
== NULL
;
705 static const struct got_error
*
706 view_resize(struct tog_view
*view
)
710 if (view
->lines
> LINES
)
711 nlines
= view
->nlines
- (view
->lines
- LINES
);
713 nlines
= view
->nlines
+ (LINES
- view
->lines
);
715 if (view
->cols
> COLS
)
716 ncols
= view
->ncols
- (view
->cols
- COLS
);
718 ncols
= view
->ncols
+ (COLS
- view
->cols
);
720 if (wresize(view
->window
, nlines
, ncols
) == ERR
)
721 return got_error_from_errno("wresize");
722 if (replace_panel(view
->panel
, view
->window
) == ERR
)
723 return got_error_from_errno("replace_panel");
724 wclear(view
->window
);
726 view
->nlines
= nlines
;
732 view
->child
->begin_x
= view_split_begin_x(view
->begin_x
);
733 if (view
->child
->begin_x
== 0) {
734 view_fullscreen(view
->child
);
735 if (view
->child
->focussed
)
736 show_panel(view
->child
->panel
);
738 show_panel(view
->panel
);
740 view_splitscreen(view
->child
);
741 show_panel(view
->child
->panel
);
748 static const struct got_error
*
749 view_close_child(struct tog_view
*view
)
751 const struct got_error
*err
= NULL
;
753 if (view
->child
== NULL
)
756 err
= view_close(view
->child
);
762 view_set_child(struct tog_view
*view
, struct tog_view
*child
)
765 child
->parent
= view
;
769 view_is_splitscreen(struct tog_view
*view
)
771 return view
->begin_x
> 0;
780 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &size
) < 0) {
781 cols
= 80; /* Default */
787 resize_term(lines
, cols
);
790 static const struct got_error
*
791 view_search_start(struct tog_view
*view
)
793 const struct got_error
*err
= NULL
;
797 if (view
->search_started
) {
798 regfree(&view
->regex
);
800 memset(&view
->regmatch
, 0, sizeof(view
->regmatch
));
802 view
->search_started
= 0;
804 if (view
->nlines
< 1)
807 mvwaddstr(view
->window
, view
->begin_y
+ view
->nlines
- 1, 0, "/");
808 wclrtoeol(view
->window
);
812 ret
= wgetnstr(view
->window
, pattern
, sizeof(pattern
));
818 if (regcomp(&view
->regex
, pattern
, REG_EXTENDED
| REG_NEWLINE
) == 0) {
819 err
= view
->search_start(view
);
821 regfree(&view
->regex
);
824 view
->search_started
= 1;
825 view
->searching
= TOG_SEARCH_FORWARD
;
826 view
->search_next_done
= 0;
827 view
->search_next(view
);
833 static const struct got_error
*
834 view_input(struct tog_view
**new, int *done
, struct tog_view
*view
,
835 struct tog_view_list_head
*views
)
837 const struct got_error
*err
= NULL
;
843 /* Clear "no matches" indicator. */
844 if (view
->search_next_done
== TOG_SEARCH_NO_MORE
||
845 view
->search_next_done
== TOG_SEARCH_HAVE_NONE
)
846 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
848 if (view
->searching
&& !view
->search_next_done
) {
849 errcode
= pthread_mutex_unlock(&tog_mutex
);
851 return got_error_set_errno(errcode
,
852 "pthread_mutex_unlock");
854 errcode
= pthread_mutex_lock(&tog_mutex
);
856 return got_error_set_errno(errcode
,
857 "pthread_mutex_lock");
858 view
->search_next(view
);
862 nodelay(stdscr
, FALSE
);
863 /* Allow threads to make progress while we are waiting for input. */
864 errcode
= pthread_mutex_unlock(&tog_mutex
);
866 return got_error_set_errno(errcode
, "pthread_mutex_unlock");
867 ch
= wgetch(view
->window
);
868 errcode
= pthread_mutex_lock(&tog_mutex
);
870 return got_error_set_errno(errcode
, "pthread_mutex_lock");
871 nodelay(stdscr
, TRUE
);
873 if (tog_sigwinch_received
|| tog_sigcont_received
) {
875 tog_sigwinch_received
= 0;
876 tog_sigcont_received
= 0;
877 TAILQ_FOREACH(v
, views
, entry
) {
878 err
= view_resize(v
);
881 err
= v
->input(new, v
, KEY_RESIZE
);
885 err
= view_resize(v
->child
);
888 err
= v
->child
->input(new, v
->child
,
900 view
->child
->focussed
= 1;
901 view
->focus_child
= 1;
902 } else if (view
->parent
) {
904 view
->parent
->focussed
= 1;
905 view
->parent
->focus_child
= 0;
909 err
= view
->input(new, view
, ch
);
916 if (view_is_parent_view(view
)) {
917 if (view
->child
== NULL
)
919 if (view_is_splitscreen(view
->child
)) {
921 view
->child
->focussed
= 1;
922 err
= view_fullscreen(view
->child
);
924 err
= view_splitscreen(view
->child
);
927 err
= view
->child
->input(new, view
->child
,
930 if (view_is_splitscreen(view
)) {
931 view
->parent
->focussed
= 0;
933 err
= view_fullscreen(view
);
935 err
= view_splitscreen(view
);
939 err
= view
->input(new, view
, KEY_RESIZE
);
945 if (view
->search_start
)
946 view_search_start(view
);
948 err
= view
->input(new, view
, ch
);
952 if (view
->search_started
&& view
->search_next
) {
953 view
->searching
= (ch
== 'n' ?
954 TOG_SEARCH_FORWARD
: TOG_SEARCH_BACKWARD
);
955 view
->search_next_done
= 0;
956 view
->search_next(view
);
958 err
= view
->input(new, view
, ch
);
961 err
= view
->input(new, view
, ch
);
969 view_vborder(struct tog_view
*view
)
972 const struct tog_view
*view_above
;
975 return view_vborder(view
->parent
);
977 panel
= panel_above(view
->panel
);
981 view_above
= panel_userptr(panel
);
982 mvwvline(view
->window
, view
->begin_y
, view_above
->begin_x
- 1,
983 got_locale_is_utf8() ? ACS_VLINE
: '|', view
->nlines
);
987 view_needs_focus_indication(struct tog_view
*view
)
989 if (view_is_parent_view(view
)) {
990 if (view
->child
== NULL
|| view
->child
->focussed
)
992 if (!view_is_splitscreen(view
->child
))
994 } else if (!view_is_splitscreen(view
))
997 return view
->focussed
;
1000 static const struct got_error
*
1001 view_loop(struct tog_view
*view
)
1003 const struct got_error
*err
= NULL
;
1004 struct tog_view_list_head views
;
1005 struct tog_view
*new_view
;
1006 int fast_refresh
= 10;
1007 int done
= 0, errcode
;
1009 errcode
= pthread_mutex_lock(&tog_mutex
);
1011 return got_error_set_errno(errcode
, "pthread_mutex_lock");
1014 TAILQ_INSERT_HEAD(&views
, view
, entry
);
1017 err
= view
->show(view
);
1022 while (!TAILQ_EMPTY(&views
) && !done
&& !tog_sigpipe_received
) {
1023 /* Refresh fast during initialization, then become slower. */
1024 if (fast_refresh
&& fast_refresh
-- == 0)
1025 halfdelay(10); /* switch to once per second */
1027 err
= view_input(&new_view
, &done
, view
, &views
);
1031 struct tog_view
*v
, *prev
= NULL
;
1033 if (view_is_parent_view(view
))
1034 prev
= TAILQ_PREV(view
, tog_view_list_head
,
1036 else if (view
->parent
)
1037 prev
= view
->parent
;
1040 view
->parent
->child
= NULL
;
1041 view
->parent
->focus_child
= 0;
1043 TAILQ_REMOVE(&views
, view
, entry
);
1045 err
= view_close(view
);
1050 TAILQ_FOREACH(v
, &views
, entry
) {
1054 if (view
== NULL
&& new_view
== NULL
) {
1055 /* No view has focus. Try to pick one. */
1058 else if (!TAILQ_EMPTY(&views
)) {
1059 view
= TAILQ_LAST(&views
,
1060 tog_view_list_head
);
1063 if (view
->focus_child
) {
1064 view
->child
->focussed
= 1;
1072 struct tog_view
*v
, *t
;
1073 /* Only allow one parent view per type. */
1074 TAILQ_FOREACH_SAFE(v
, &views
, entry
, t
) {
1075 if (v
->type
!= new_view
->type
)
1077 TAILQ_REMOVE(&views
, v
, entry
);
1078 err
= view_close(v
);
1083 TAILQ_INSERT_TAIL(&views
, new_view
, entry
);
1087 if (view_is_parent_view(view
)) {
1088 if (view
->child
&& view
->child
->focussed
)
1091 if (view
->parent
&& view
->parent
->focussed
)
1092 view
= view
->parent
;
1094 show_panel(view
->panel
);
1095 if (view
->child
&& view_is_splitscreen(view
->child
))
1096 show_panel(view
->child
->panel
);
1097 if (view
->parent
&& view_is_splitscreen(view
)) {
1098 err
= view
->parent
->show(view
->parent
);
1102 err
= view
->show(view
);
1106 err
= view
->child
->show(view
->child
);
1115 while (!TAILQ_EMPTY(&views
)) {
1116 view
= TAILQ_FIRST(&views
);
1117 TAILQ_REMOVE(&views
, view
, entry
);
1121 errcode
= pthread_mutex_unlock(&tog_mutex
);
1122 if (errcode
&& err
== NULL
)
1123 err
= got_error_set_errno(errcode
, "pthread_mutex_unlock");
1133 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1138 /* Create newly allocated wide-character string equivalent to a byte string. */
1139 static const struct got_error
*
1140 mbs2ws(wchar_t **ws
, size_t *wlen
, const char *s
)
1143 const struct got_error
*err
= NULL
;
1146 *wlen
= mbstowcs(NULL
, s
, 0);
1147 if (*wlen
== (size_t)-1) {
1149 if (errno
!= EILSEQ
)
1150 return got_error_from_errno("mbstowcs");
1152 /* byte string invalid in current encoding; try to "fix" it */
1153 err
= got_mbsavis(&vis
, &vislen
, s
);
1156 *wlen
= mbstowcs(NULL
, vis
, 0);
1157 if (*wlen
== (size_t)-1) {
1158 err
= got_error_from_errno("mbstowcs"); /* give up */
1163 *ws
= calloc(*wlen
+ 1, sizeof(**ws
));
1165 err
= got_error_from_errno("calloc");
1169 if (mbstowcs(*ws
, vis
? vis
: s
, *wlen
) != *wlen
)
1170 err
= got_error_from_errno("mbstowcs");
1181 /* Format a line for display, ensuring that it won't overflow a width limit. */
1182 static const struct got_error
*
1183 format_line(wchar_t **wlinep
, int *widthp
, const char *line
, int wlimit
,
1186 const struct got_error
*err
= NULL
;
1188 wchar_t *wline
= NULL
;
1195 err
= mbs2ws(&wline
, &wlen
, line
);
1199 if (wlen
> 0 && wline
[wlen
- 1] == L
'\n') {
1200 wline
[wlen
- 1] = L
'\0';
1203 if (wlen
> 0 && wline
[wlen
- 1] == L
'\r') {
1204 wline
[wlen
- 1] = L
'\0';
1210 int width
= wcwidth(wline
[i
]);
1217 if (width
== 1 || width
== 2) {
1218 if (cols
+ width
> wlimit
)
1222 } else if (width
== -1) {
1223 if (wline
[i
] == L
'\t') {
1225 ((cols
+ col_tab_align
) % TABSIZE
);
1230 if (cols
+ width
> wlimit
)
1235 err
= got_error_from_errno("wcwidth");
1250 static const struct got_error
*
1251 build_refs_str(char **refs_str
, struct got_reflist_head
*refs
,
1252 struct got_object_id
*id
, struct got_repository
*repo
)
1254 static const struct got_error
*err
= NULL
;
1255 struct got_reflist_entry
*re
;
1261 TAILQ_FOREACH(re
, refs
, entry
) {
1262 struct got_tag_object
*tag
= NULL
;
1263 struct got_object_id
*ref_id
;
1266 name
= got_ref_get_name(re
->ref
);
1267 if (strcmp(name
, GOT_REF_HEAD
) == 0)
1269 if (strncmp(name
, "refs/", 5) == 0)
1271 if (strncmp(name
, "got/", 4) == 0)
1273 if (strncmp(name
, "heads/", 6) == 0)
1275 if (strncmp(name
, "remotes/", 8) == 0) {
1277 s
= strstr(name
, "/" GOT_REF_HEAD
);
1278 if (s
!= NULL
&& s
[strlen(s
)] == '\0')
1281 err
= got_ref_resolve(&ref_id
, repo
, re
->ref
);
1284 if (strncmp(name
, "tags/", 5) == 0) {
1285 err
= got_object_open_as_tag(&tag
, repo
, ref_id
);
1287 if (err
->code
!= GOT_ERR_OBJ_TYPE
) {
1291 /* Ref points at something other than a tag. */
1296 cmp
= got_object_id_cmp(tag
?
1297 got_object_tag_get_object_id(tag
) : ref_id
, id
);
1300 got_object_tag_close(tag
);
1304 if (asprintf(refs_str
, "%s%s%s", s
? s
: "",
1305 s
? ", " : "", name
) == -1) {
1306 err
= got_error_from_errno("asprintf");
1317 static const struct got_error
*
1318 format_author(wchar_t **wauthor
, int *author_width
, char *author
, int limit
,
1323 smallerthan
= strchr(author
, '<');
1324 if (smallerthan
&& smallerthan
[1] != '\0')
1325 author
= smallerthan
+ 1;
1326 author
[strcspn(author
, "@>")] = '\0';
1327 return format_line(wauthor
, author_width
, author
, limit
, col_tab_align
);
1330 static const struct got_error
*
1331 draw_commit(struct tog_view
*view
, struct got_commit_object
*commit
,
1332 struct got_object_id
*id
, const size_t date_display_cols
,
1333 int author_display_cols
)
1335 struct tog_log_view_state
*s
= &view
->state
.log
;
1336 const struct got_error
*err
= NULL
;
1337 char datebuf
[12]; /* YYYY-MM-DD + SPACE + NUL */
1338 char *logmsg0
= NULL
, *logmsg
= NULL
;
1339 char *author
= NULL
;
1340 wchar_t *wlogmsg
= NULL
, *wauthor
= NULL
;
1341 int author_width
, logmsg_width
;
1342 char *newline
, *line
= NULL
;
1344 const int avail
= view
->ncols
;
1346 time_t committer_time
;
1347 struct tog_color
*tc
;
1349 committer_time
= got_object_commit_get_committer_time(commit
);
1350 if (gmtime_r(&committer_time
, &tm
) == NULL
)
1351 return got_error_from_errno("gmtime_r");
1352 if (strftime(datebuf
, sizeof(datebuf
), "%G-%m-%d ", &tm
) == 0)
1353 return got_error(GOT_ERR_NO_SPACE
);
1355 if (avail
<= date_display_cols
)
1356 limit
= MIN(sizeof(datebuf
) - 1, avail
);
1358 limit
= MIN(date_display_cols
, sizeof(datebuf
) - 1);
1359 tc
= get_color(&s
->colors
, TOG_COLOR_DATE
);
1361 wattr_on(view
->window
,
1362 COLOR_PAIR(tc
->colorpair
), NULL
);
1363 waddnstr(view
->window
, datebuf
, limit
);
1365 wattr_off(view
->window
,
1366 COLOR_PAIR(tc
->colorpair
), NULL
);
1373 err
= got_object_id_str(&id_str
, id
);
1376 tc
= get_color(&s
->colors
, TOG_COLOR_COMMIT
);
1378 wattr_on(view
->window
,
1379 COLOR_PAIR(tc
->colorpair
), NULL
);
1380 wprintw(view
->window
, "%.8s ", id_str
);
1382 wattr_off(view
->window
,
1383 COLOR_PAIR(tc
->colorpair
), NULL
);
1390 author
= strdup(got_object_commit_get_author(commit
));
1391 if (author
== NULL
) {
1392 err
= got_error_from_errno("strdup");
1395 err
= format_author(&wauthor
, &author_width
, author
, avail
- col
, col
);
1398 tc
= get_color(&s
->colors
, TOG_COLOR_AUTHOR
);
1400 wattr_on(view
->window
,
1401 COLOR_PAIR(tc
->colorpair
), NULL
);
1402 waddwstr(view
->window
, wauthor
);
1404 wattr_off(view
->window
,
1405 COLOR_PAIR(tc
->colorpair
), NULL
);
1406 col
+= author_width
;
1407 while (col
< avail
&& author_width
< author_display_cols
+ 2) {
1408 waddch(view
->window
, ' ');
1415 err
= got_object_commit_get_logmsg(&logmsg0
, commit
);
1419 while (*logmsg
== '\n')
1421 newline
= strchr(logmsg
, '\n');
1424 limit
= avail
- col
;
1425 err
= format_line(&wlogmsg
, &logmsg_width
, logmsg
, limit
, col
);
1428 waddwstr(view
->window
, wlogmsg
);
1429 col
+= logmsg_width
;
1430 while (col
< avail
) {
1431 waddch(view
->window
, ' ');
1443 static struct commit_queue_entry
*
1444 alloc_commit_queue_entry(struct got_commit_object
*commit
,
1445 struct got_object_id
*id
)
1447 struct commit_queue_entry
*entry
;
1449 entry
= calloc(1, sizeof(*entry
));
1454 entry
->commit
= commit
;
1459 pop_commit(struct commit_queue
*commits
)
1461 struct commit_queue_entry
*entry
;
1463 entry
= TAILQ_FIRST(&commits
->head
);
1464 TAILQ_REMOVE(&commits
->head
, entry
, entry
);
1465 got_object_commit_close(entry
->commit
);
1466 commits
->ncommits
--;
1467 /* Don't free entry->id! It is owned by the commit graph. */
1472 free_commits(struct commit_queue
*commits
)
1474 while (!TAILQ_EMPTY(&commits
->head
))
1475 pop_commit(commits
);
1478 static const struct got_error
*
1479 match_commit(int *have_match
, struct got_object_id
*id
,
1480 struct got_commit_object
*commit
, regex_t
*regex
)
1482 const struct got_error
*err
= NULL
;
1483 regmatch_t regmatch
;
1484 char *id_str
= NULL
, *logmsg
= NULL
;
1488 err
= got_object_id_str(&id_str
, id
);
1492 err
= got_object_commit_get_logmsg(&logmsg
, commit
);
1496 if (regexec(regex
, got_object_commit_get_author(commit
), 1,
1497 ®match
, 0) == 0 ||
1498 regexec(regex
, got_object_commit_get_committer(commit
), 1,
1499 ®match
, 0) == 0 ||
1500 regexec(regex
, id_str
, 1, ®match
, 0) == 0 ||
1501 regexec(regex
, logmsg
, 1, ®match
, 0) == 0)
1509 static const struct got_error
*
1510 queue_commits(struct tog_log_thread_args
*a
)
1512 const struct got_error
*err
= NULL
;
1515 * We keep all commits open throughout the lifetime of the log
1516 * view in order to avoid having to re-fetch commits from disk
1517 * while updating the display.
1520 struct got_object_id
*id
;
1521 struct got_commit_object
*commit
;
1522 struct commit_queue_entry
*entry
;
1525 err
= got_commit_graph_iter_next(&id
, a
->graph
, a
->repo
,
1527 if (err
|| id
== NULL
)
1530 err
= got_object_open_as_commit(&commit
, a
->repo
, id
);
1533 entry
= alloc_commit_queue_entry(commit
, id
);
1534 if (entry
== NULL
) {
1535 err
= got_error_from_errno("alloc_commit_queue_entry");
1539 errcode
= pthread_mutex_lock(&tog_mutex
);
1541 err
= got_error_set_errno(errcode
,
1542 "pthread_mutex_lock");
1546 entry
->idx
= a
->commits
->ncommits
;
1547 TAILQ_INSERT_TAIL(&a
->commits
->head
, entry
, entry
);
1548 a
->commits
->ncommits
++;
1550 if (*a
->searching
== TOG_SEARCH_FORWARD
&&
1551 !*a
->search_next_done
) {
1553 err
= match_commit(&have_match
, id
, commit
, a
->regex
);
1557 *a
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
1560 errcode
= pthread_mutex_unlock(&tog_mutex
);
1561 if (errcode
&& err
== NULL
)
1562 err
= got_error_set_errno(errcode
,
1563 "pthread_mutex_unlock");
1566 } while (*a
->searching
== TOG_SEARCH_FORWARD
&& !*a
->search_next_done
);
1572 select_commit(struct tog_log_view_state
*s
)
1574 struct commit_queue_entry
*entry
;
1577 entry
= s
->first_displayed_entry
;
1579 if (ncommits
== s
->selected
) {
1580 s
->selected_entry
= entry
;
1583 entry
= TAILQ_NEXT(entry
, entry
);
1588 static const struct got_error
*
1589 draw_commits(struct tog_view
*view
)
1591 const struct got_error
*err
= NULL
;
1592 struct tog_log_view_state
*s
= &view
->state
.log
;
1593 struct commit_queue_entry
*entry
= s
->selected_entry
;
1594 const int limit
= view
->nlines
;
1596 int ncommits
, author_cols
= 4;
1597 char *id_str
= NULL
, *header
= NULL
, *ncommits_str
= NULL
;
1598 char *refs_str
= NULL
;
1600 struct tog_color
*tc
;
1601 static const size_t date_display_cols
= 12;
1603 if (s
->selected_entry
&&
1604 !(view
->searching
&& view
->search_next_done
== 0)) {
1605 struct got_reflist_head
*refs
;
1606 err
= got_object_id_str(&id_str
, s
->selected_entry
->id
);
1609 refs
= got_reflist_object_id_map_lookup(tog_refs_idmap
,
1610 s
->selected_entry
->id
);
1612 err
= build_refs_str(&refs_str
, refs
,
1613 s
->selected_entry
->id
, s
->repo
);
1619 if (s
->thread_args
.commits_needed
== 0)
1620 halfdelay(10); /* disable fast refresh */
1622 if (s
->thread_args
.commits_needed
> 0 || s
->thread_args
.load_all
) {
1623 if (asprintf(&ncommits_str
, " [%d/%d] %s",
1624 entry
? entry
->idx
+ 1 : 0, s
->commits
.ncommits
,
1625 (view
->searching
&& !view
->search_next_done
) ?
1626 "searching..." : "loading...") == -1) {
1627 err
= got_error_from_errno("asprintf");
1631 const char *search_str
= NULL
;
1633 if (view
->searching
) {
1634 if (view
->search_next_done
== TOG_SEARCH_NO_MORE
)
1635 search_str
= "no more matches";
1636 else if (view
->search_next_done
== TOG_SEARCH_HAVE_NONE
)
1637 search_str
= "no matches found";
1638 else if (!view
->search_next_done
)
1639 search_str
= "searching...";
1642 if (asprintf(&ncommits_str
, " [%d/%d] %s",
1643 entry
? entry
->idx
+ 1 : 0, s
->commits
.ncommits
,
1644 search_str
? search_str
:
1645 (refs_str
? refs_str
: "")) == -1) {
1646 err
= got_error_from_errno("asprintf");
1651 if (s
->in_repo_path
&& strcmp(s
->in_repo_path
, "/") != 0) {
1652 if (asprintf(&header
, "commit %s %s%s",
1653 id_str
? id_str
: "........................................",
1654 s
->in_repo_path
, ncommits_str
) == -1) {
1655 err
= got_error_from_errno("asprintf");
1659 } else if (asprintf(&header
, "commit %s%s",
1660 id_str
? id_str
: "........................................",
1661 ncommits_str
) == -1) {
1662 err
= got_error_from_errno("asprintf");
1666 err
= format_line(&wline
, &width
, header
, view
->ncols
, 0);
1670 werase(view
->window
);
1672 if (view_needs_focus_indication(view
))
1673 wstandout(view
->window
);
1674 tc
= get_color(&s
->colors
, TOG_COLOR_COMMIT
);
1676 wattr_on(view
->window
,
1677 COLOR_PAIR(tc
->colorpair
), NULL
);
1678 waddwstr(view
->window
, wline
);
1680 wattr_off(view
->window
,
1681 COLOR_PAIR(tc
->colorpair
), NULL
);
1682 while (width
< view
->ncols
) {
1683 waddch(view
->window
, ' ');
1686 if (view_needs_focus_indication(view
))
1687 wstandend(view
->window
);
1692 /* Grow author column size if necessary. */
1693 entry
= s
->first_displayed_entry
;
1699 if (ncommits
>= limit
- 1)
1701 author
= strdup(got_object_commit_get_author(entry
->commit
));
1702 if (author
== NULL
) {
1703 err
= got_error_from_errno("strdup");
1706 err
= format_author(&wauthor
, &width
, author
, COLS
,
1708 if (author_cols
< width
)
1709 author_cols
= width
;
1713 entry
= TAILQ_NEXT(entry
, entry
);
1716 entry
= s
->first_displayed_entry
;
1717 s
->last_displayed_entry
= s
->first_displayed_entry
;
1720 if (ncommits
>= limit
- 1)
1722 if (ncommits
== s
->selected
)
1723 wstandout(view
->window
);
1724 err
= draw_commit(view
, entry
->commit
, entry
->id
,
1725 date_display_cols
, author_cols
);
1726 if (ncommits
== s
->selected
)
1727 wstandend(view
->window
);
1731 s
->last_displayed_entry
= entry
;
1732 entry
= TAILQ_NEXT(entry
, entry
);
1747 log_scroll_up(struct tog_log_view_state
*s
, int maxscroll
)
1749 struct commit_queue_entry
*entry
;
1752 entry
= TAILQ_FIRST(&s
->commits
.head
);
1753 if (s
->first_displayed_entry
== entry
)
1756 entry
= s
->first_displayed_entry
;
1757 while (entry
&& nscrolled
< maxscroll
) {
1758 entry
= TAILQ_PREV(entry
, commit_queue_head
, entry
);
1760 s
->first_displayed_entry
= entry
;
1766 static const struct got_error
*
1767 trigger_log_thread(struct tog_view
*view
, int wait
)
1769 struct tog_log_thread_args
*ta
= &view
->state
.log
.thread_args
;
1772 halfdelay(1); /* fast refresh while loading commits */
1774 while (ta
->commits_needed
> 0 || ta
->load_all
) {
1775 if (ta
->log_complete
)
1778 /* Wake the log thread. */
1779 errcode
= pthread_cond_signal(&ta
->need_commits
);
1781 return got_error_set_errno(errcode
,
1782 "pthread_cond_signal");
1785 * The mutex will be released while the view loop waits
1786 * in wgetch(), at which time the log thread will run.
1791 /* Display progress update in log view. */
1792 show_log_view(view
);
1796 /* Wait right here while next commit is being loaded. */
1797 errcode
= pthread_cond_wait(&ta
->commit_loaded
, &tog_mutex
);
1799 return got_error_set_errno(errcode
,
1800 "pthread_cond_wait");
1802 /* Display progress update in log view. */
1803 show_log_view(view
);
1811 static const struct got_error
*
1812 log_scroll_down(struct tog_view
*view
, int maxscroll
)
1814 struct tog_log_view_state
*s
= &view
->state
.log
;
1815 const struct got_error
*err
= NULL
;
1816 struct commit_queue_entry
*pentry
;
1817 int nscrolled
= 0, ncommits_needed
;
1819 if (s
->last_displayed_entry
== NULL
)
1822 ncommits_needed
= s
->last_displayed_entry
->idx
+ 1 + maxscroll
;
1823 if (s
->commits
.ncommits
< ncommits_needed
&&
1824 !s
->thread_args
.log_complete
) {
1826 * Ask the log thread for required amount of commits.
1828 s
->thread_args
.commits_needed
+= maxscroll
;
1829 err
= trigger_log_thread(view
, 1);
1835 pentry
= TAILQ_NEXT(s
->last_displayed_entry
, entry
);
1839 s
->last_displayed_entry
= pentry
;
1841 pentry
= TAILQ_NEXT(s
->first_displayed_entry
, entry
);
1844 s
->first_displayed_entry
= pentry
;
1845 } while (++nscrolled
< maxscroll
);
1850 static const struct got_error
*
1851 open_diff_view_for_commit(struct tog_view
**new_view
, int begin_x
,
1852 struct got_commit_object
*commit
, struct got_object_id
*commit_id
,
1853 struct tog_view
*log_view
, struct got_repository
*repo
)
1855 const struct got_error
*err
;
1856 struct got_object_qid
*parent_id
;
1857 struct tog_view
*diff_view
;
1859 diff_view
= view_open(0, 0, 0, begin_x
, TOG_VIEW_DIFF
);
1860 if (diff_view
== NULL
)
1861 return got_error_from_errno("view_open");
1863 parent_id
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
1864 err
= open_diff_view(diff_view
, parent_id
? parent_id
->id
: NULL
,
1865 commit_id
, NULL
, NULL
, 3, 0, 0, log_view
, repo
);
1867 *new_view
= diff_view
;
1871 static const struct got_error
*
1872 tree_view_visit_subtree(struct tog_tree_view_state
*s
,
1873 struct got_tree_object
*subtree
)
1875 struct tog_parent_tree
*parent
;
1877 parent
= calloc(1, sizeof(*parent
));
1879 return got_error_from_errno("calloc");
1881 parent
->tree
= s
->tree
;
1882 parent
->first_displayed_entry
= s
->first_displayed_entry
;
1883 parent
->selected_entry
= s
->selected_entry
;
1884 parent
->selected
= s
->selected
;
1885 TAILQ_INSERT_HEAD(&s
->parents
, parent
, entry
);
1888 s
->first_displayed_entry
= NULL
;
1892 static const struct got_error
*
1893 tree_view_walk_path(struct tog_tree_view_state
*s
,
1894 struct got_object_id
*commit_id
, const char *path
)
1896 const struct got_error
*err
= NULL
;
1897 struct got_tree_object
*tree
= NULL
;
1899 char *slash
, *subpath
= NULL
;
1901 /* Walk the path and open corresponding tree objects. */
1904 struct got_tree_entry
*te
;
1905 struct got_object_id
*tree_id
;
1911 /* Ensure the correct subtree entry is selected. */
1912 slash
= strchr(p
, '/');
1914 te_name
= strdup(p
);
1916 te_name
= strndup(p
, slash
- p
);
1917 if (te_name
== NULL
) {
1918 err
= got_error_from_errno("strndup");
1921 te
= got_object_tree_find_entry(s
->tree
, te_name
);
1923 err
= got_error_path(te_name
, GOT_ERR_NO_TREE_ENTRY
);
1928 s
->first_displayed_entry
= s
->selected_entry
= te
;
1930 if (!S_ISDIR(got_tree_entry_get_mode(s
->selected_entry
)))
1931 break; /* jump to this file's entry */
1933 slash
= strchr(p
, '/');
1935 subpath
= strndup(path
, slash
- path
);
1937 subpath
= strdup(path
);
1938 if (subpath
== NULL
) {
1939 err
= got_error_from_errno("strdup");
1943 err
= got_object_id_by_path(&tree_id
, s
->repo
, commit_id
,
1948 err
= got_object_open_as_tree(&tree
, s
->repo
, tree_id
);
1953 err
= tree_view_visit_subtree(s
, tree
);
1955 got_object_tree_close(tree
);
1969 static const struct got_error
*
1970 browse_commit_tree(struct tog_view
**new_view
, int begin_x
,
1971 struct commit_queue_entry
*entry
, const char *path
,
1972 const char *head_ref_name
, struct got_repository
*repo
)
1974 const struct got_error
*err
= NULL
;
1975 struct tog_tree_view_state
*s
;
1976 struct tog_view
*tree_view
;
1978 tree_view
= view_open(0, 0, 0, begin_x
, TOG_VIEW_TREE
);
1979 if (tree_view
== NULL
)
1980 return got_error_from_errno("view_open");
1982 err
= open_tree_view(tree_view
, entry
->id
, head_ref_name
, repo
);
1985 s
= &tree_view
->state
.tree
;
1987 *new_view
= tree_view
;
1989 if (got_path_is_root_dir(path
))
1992 return tree_view_walk_path(s
, entry
->id
, path
);
1995 static const struct got_error
*
1996 block_signals_used_by_main_thread(void)
2001 if (sigemptyset(&sigset
) == -1)
2002 return got_error_from_errno("sigemptyset");
2004 /* tog handles SIGWINCH and SIGCONT */
2005 if (sigaddset(&sigset
, SIGWINCH
) == -1)
2006 return got_error_from_errno("sigaddset");
2007 if (sigaddset(&sigset
, SIGCONT
) == -1)
2008 return got_error_from_errno("sigaddset");
2010 /* ncurses handles SIGTSTP */
2011 if (sigaddset(&sigset
, SIGTSTP
) == -1)
2012 return got_error_from_errno("sigaddset");
2014 errcode
= pthread_sigmask(SIG_BLOCK
, &sigset
, NULL
);
2016 return got_error_set_errno(errcode
, "pthread_sigmask");
2022 log_thread(void *arg
)
2024 const struct got_error
*err
= NULL
;
2026 struct tog_log_thread_args
*a
= arg
;
2029 err
= block_signals_used_by_main_thread();
2033 while (!done
&& !err
&& !tog_sigpipe_received
) {
2034 err
= queue_commits(a
);
2036 if (err
->code
!= GOT_ERR_ITER_COMPLETED
)
2040 } else if (a
->commits_needed
> 0 && !a
->load_all
)
2041 a
->commits_needed
--;
2043 errcode
= pthread_mutex_lock(&tog_mutex
);
2045 err
= got_error_set_errno(errcode
,
2046 "pthread_mutex_lock");
2048 } else if (*a
->quit
)
2050 else if (*a
->first_displayed_entry
== NULL
) {
2051 *a
->first_displayed_entry
=
2052 TAILQ_FIRST(&a
->commits
->head
);
2053 *a
->selected_entry
= *a
->first_displayed_entry
;
2056 errcode
= pthread_cond_signal(&a
->commit_loaded
);
2058 err
= got_error_set_errno(errcode
,
2059 "pthread_cond_signal");
2060 pthread_mutex_unlock(&tog_mutex
);
2065 a
->commits_needed
= 0;
2067 if (a
->commits_needed
== 0 && !a
->load_all
) {
2068 errcode
= pthread_cond_wait(&a
->need_commits
,
2071 err
= got_error_set_errno(errcode
,
2072 "pthread_cond_wait");
2078 errcode
= pthread_mutex_unlock(&tog_mutex
);
2079 if (errcode
&& err
== NULL
)
2080 err
= got_error_set_errno(errcode
,
2081 "pthread_mutex_unlock");
2083 a
->log_complete
= 1;
2087 static const struct got_error
*
2088 stop_log_thread(struct tog_log_view_state
*s
)
2090 const struct got_error
*err
= NULL
;
2095 errcode
= pthread_cond_signal(&s
->thread_args
.need_commits
);
2097 return got_error_set_errno(errcode
,
2098 "pthread_cond_signal");
2099 errcode
= pthread_mutex_unlock(&tog_mutex
);
2101 return got_error_set_errno(errcode
,
2102 "pthread_mutex_unlock");
2103 errcode
= pthread_join(s
->thread
, (void **)&err
);
2105 return got_error_set_errno(errcode
, "pthread_join");
2106 errcode
= pthread_mutex_lock(&tog_mutex
);
2108 return got_error_set_errno(errcode
,
2109 "pthread_mutex_lock");
2110 s
->thread
= 0; //NULL;
2113 if (s
->thread_args
.repo
) {
2114 err
= got_repo_close(s
->thread_args
.repo
);
2115 s
->thread_args
.repo
= NULL
;
2118 if (s
->thread_args
.graph
) {
2119 got_commit_graph_close(s
->thread_args
.graph
);
2120 s
->thread_args
.graph
= NULL
;
2126 static const struct got_error
*
2127 close_log_view(struct tog_view
*view
)
2129 const struct got_error
*err
= NULL
;
2130 struct tog_log_view_state
*s
= &view
->state
.log
;
2133 err
= stop_log_thread(s
);
2135 errcode
= pthread_cond_destroy(&s
->thread_args
.need_commits
);
2136 if (errcode
&& err
== NULL
)
2137 err
= got_error_set_errno(errcode
, "pthread_cond_destroy");
2139 errcode
= pthread_cond_destroy(&s
->thread_args
.commit_loaded
);
2140 if (errcode
&& err
== NULL
)
2141 err
= got_error_set_errno(errcode
, "pthread_cond_destroy");
2143 free_commits(&s
->commits
);
2144 free(s
->in_repo_path
);
2145 s
->in_repo_path
= NULL
;
2148 free(s
->head_ref_name
);
2149 s
->head_ref_name
= NULL
;
2153 static const struct got_error
*
2154 search_start_log_view(struct tog_view
*view
)
2156 struct tog_log_view_state
*s
= &view
->state
.log
;
2158 s
->matched_entry
= NULL
;
2159 s
->search_entry
= NULL
;
2163 static const struct got_error
*
2164 search_next_log_view(struct tog_view
*view
)
2166 const struct got_error
*err
= NULL
;
2167 struct tog_log_view_state
*s
= &view
->state
.log
;
2168 struct commit_queue_entry
*entry
;
2170 /* Display progress update in log view. */
2171 show_log_view(view
);
2175 if (s
->search_entry
) {
2177 errcode
= pthread_mutex_unlock(&tog_mutex
);
2179 return got_error_set_errno(errcode
,
2180 "pthread_mutex_unlock");
2181 ch
= wgetch(view
->window
);
2182 errcode
= pthread_mutex_lock(&tog_mutex
);
2184 return got_error_set_errno(errcode
,
2185 "pthread_mutex_lock");
2186 if (ch
== KEY_BACKSPACE
) {
2187 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
2190 if (view
->searching
== TOG_SEARCH_FORWARD
)
2191 entry
= TAILQ_NEXT(s
->search_entry
, entry
);
2193 entry
= TAILQ_PREV(s
->search_entry
,
2194 commit_queue_head
, entry
);
2195 } else if (s
->matched_entry
) {
2196 if (view
->searching
== TOG_SEARCH_FORWARD
)
2197 entry
= TAILQ_NEXT(s
->matched_entry
, entry
);
2199 entry
= TAILQ_PREV(s
->matched_entry
,
2200 commit_queue_head
, entry
);
2202 if (view
->searching
== TOG_SEARCH_FORWARD
)
2203 entry
= TAILQ_FIRST(&s
->commits
.head
);
2205 entry
= TAILQ_LAST(&s
->commits
.head
, commit_queue_head
);
2211 if (entry
== NULL
) {
2212 if (s
->thread_args
.log_complete
||
2213 view
->searching
== TOG_SEARCH_BACKWARD
) {
2214 view
->search_next_done
=
2215 (s
->matched_entry
== NULL
?
2216 TOG_SEARCH_HAVE_NONE
: TOG_SEARCH_NO_MORE
);
2217 s
->search_entry
= NULL
;
2221 * Poke the log thread for more commits and return,
2222 * allowing the main loop to make progress. Search
2223 * will resume at s->search_entry once we come back.
2225 s
->thread_args
.commits_needed
++;
2226 return trigger_log_thread(view
, 0);
2229 err
= match_commit(&have_match
, entry
->id
, entry
->commit
,
2234 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
2235 s
->matched_entry
= entry
;
2239 s
->search_entry
= entry
;
2240 if (view
->searching
== TOG_SEARCH_FORWARD
)
2241 entry
= TAILQ_NEXT(entry
, entry
);
2243 entry
= TAILQ_PREV(entry
, commit_queue_head
, entry
);
2246 if (s
->matched_entry
) {
2247 int cur
= s
->selected_entry
->idx
;
2248 while (cur
< s
->matched_entry
->idx
) {
2249 err
= input_log_view(NULL
, view
, KEY_DOWN
);
2254 while (cur
> s
->matched_entry
->idx
) {
2255 err
= input_log_view(NULL
, view
, KEY_UP
);
2262 s
->search_entry
= NULL
;
2267 static const struct got_error
*
2268 open_log_view(struct tog_view
*view
, struct got_object_id
*start_id
,
2269 struct got_repository
*repo
, const char *head_ref_name
,
2270 const char *in_repo_path
, int log_branches
)
2272 const struct got_error
*err
= NULL
;
2273 struct tog_log_view_state
*s
= &view
->state
.log
;
2274 struct got_repository
*thread_repo
= NULL
;
2275 struct got_commit_graph
*thread_graph
= NULL
;
2278 if (in_repo_path
!= s
->in_repo_path
) {
2279 free(s
->in_repo_path
);
2280 s
->in_repo_path
= strdup(in_repo_path
);
2281 if (s
->in_repo_path
== NULL
)
2282 return got_error_from_errno("strdup");
2285 /* The commit queue only contains commits being displayed. */
2286 TAILQ_INIT(&s
->commits
.head
);
2287 s
->commits
.ncommits
= 0;
2290 if (head_ref_name
) {
2291 s
->head_ref_name
= strdup(head_ref_name
);
2292 if (s
->head_ref_name
== NULL
) {
2293 err
= got_error_from_errno("strdup");
2297 s
->start_id
= got_object_id_dup(start_id
);
2298 if (s
->start_id
== NULL
) {
2299 err
= got_error_from_errno("got_object_id_dup");
2302 s
->log_branches
= log_branches
;
2304 STAILQ_INIT(&s
->colors
);
2305 if (has_colors() && getenv("TOG_COLORS") != NULL
) {
2306 err
= add_color(&s
->colors
, "^$", TOG_COLOR_COMMIT
,
2307 get_color_value("TOG_COLOR_COMMIT"));
2310 err
= add_color(&s
->colors
, "^$", TOG_COLOR_AUTHOR
,
2311 get_color_value("TOG_COLOR_AUTHOR"));
2313 free_colors(&s
->colors
);
2316 err
= add_color(&s
->colors
, "^$", TOG_COLOR_DATE
,
2317 get_color_value("TOG_COLOR_DATE"));
2319 free_colors(&s
->colors
);
2324 view
->show
= show_log_view
;
2325 view
->input
= input_log_view
;
2326 view
->close
= close_log_view
;
2327 view
->search_start
= search_start_log_view
;
2328 view
->search_next
= search_next_log_view
;
2330 err
= got_repo_open(&thread_repo
, got_repo_get_path(repo
), NULL
);
2333 err
= got_commit_graph_open(&thread_graph
, s
->in_repo_path
,
2337 err
= got_commit_graph_iter_start(thread_graph
, s
->start_id
,
2338 s
->repo
, NULL
, NULL
);
2342 errcode
= pthread_cond_init(&s
->thread_args
.need_commits
, NULL
);
2344 err
= got_error_set_errno(errcode
, "pthread_cond_init");
2347 errcode
= pthread_cond_init(&s
->thread_args
.commit_loaded
, NULL
);
2349 err
= got_error_set_errno(errcode
, "pthread_cond_init");
2353 s
->thread_args
.commits_needed
= view
->nlines
;
2354 s
->thread_args
.graph
= thread_graph
;
2355 s
->thread_args
.commits
= &s
->commits
;
2356 s
->thread_args
.in_repo_path
= s
->in_repo_path
;
2357 s
->thread_args
.start_id
= s
->start_id
;
2358 s
->thread_args
.repo
= thread_repo
;
2359 s
->thread_args
.log_complete
= 0;
2360 s
->thread_args
.quit
= &s
->quit
;
2361 s
->thread_args
.first_displayed_entry
= &s
->first_displayed_entry
;
2362 s
->thread_args
.selected_entry
= &s
->selected_entry
;
2363 s
->thread_args
.searching
= &view
->searching
;
2364 s
->thread_args
.search_next_done
= &view
->search_next_done
;
2365 s
->thread_args
.regex
= &view
->regex
;
2368 close_log_view(view
);
2372 static const struct got_error
*
2373 show_log_view(struct tog_view
*view
)
2375 const struct got_error
*err
;
2376 struct tog_log_view_state
*s
= &view
->state
.log
;
2378 if (s
->thread
== 0) { //NULL) {
2379 int errcode
= pthread_create(&s
->thread
, NULL
, log_thread
,
2382 return got_error_set_errno(errcode
, "pthread_create");
2383 if (s
->thread_args
.commits_needed
> 0) {
2384 err
= trigger_log_thread(view
, 1);
2390 return draw_commits(view
);
2393 static const struct got_error
*
2394 input_log_view(struct tog_view
**new_view
, struct tog_view
*view
, int ch
)
2396 const struct got_error
*err
= NULL
;
2397 struct tog_log_view_state
*s
= &view
->state
.log
;
2398 struct tog_view
*diff_view
= NULL
, *tree_view
= NULL
;
2399 struct tog_view
*ref_view
= NULL
;
2400 struct commit_queue_entry
*entry
;
2403 if (s
->thread_args
.load_all
) {
2404 if (ch
== KEY_BACKSPACE
)
2405 s
->thread_args
.load_all
= 0;
2406 else if (s
->thread_args
.log_complete
) {
2407 s
->thread_args
.load_all
= 0;
2408 log_scroll_down(view
, s
->commits
.ncommits
);
2409 s
->selected
= MIN(view
->nlines
- 2,
2410 s
->commits
.ncommits
- 1);
2424 if (s
->first_displayed_entry
== NULL
)
2426 if (s
->selected
> 0)
2429 log_scroll_up(s
, 1);
2435 s
->first_displayed_entry
= TAILQ_FIRST(&s
->commits
.head
);
2440 if (s
->first_displayed_entry
== NULL
)
2442 if (TAILQ_FIRST(&s
->commits
.head
) == s
->first_displayed_entry
)
2445 log_scroll_up(s
, view
->nlines
- 1);
2452 if (s
->first_displayed_entry
== NULL
)
2454 if (s
->selected
< MIN(view
->nlines
- 2,
2455 s
->commits
.ncommits
- 1))
2458 err
= log_scroll_down(view
, 1);
2466 /* We don't know yet how many commits, so we're forced to
2467 * traverse them all. */
2468 if (!s
->thread_args
.log_complete
) {
2469 s
->thread_args
.load_all
= 1;
2470 return trigger_log_thread(view
, 0);
2474 entry
= TAILQ_LAST(&s
->commits
.head
, commit_queue_head
);
2475 for (n
= 0; n
< view
->nlines
- 1; n
++) {
2478 s
->first_displayed_entry
= entry
;
2479 entry
= TAILQ_PREV(entry
, commit_queue_head
, entry
);
2482 s
->selected
= n
- 1;
2488 struct commit_queue_entry
*first
;
2489 first
= s
->first_displayed_entry
;
2492 err
= log_scroll_down(view
, view
->nlines
- 1);
2495 if (first
== s
->first_displayed_entry
&&
2496 s
->selected
< MIN(view
->nlines
- 2,
2497 s
->commits
.ncommits
- 1)) {
2498 /* can't scroll further down */
2499 s
->selected
= MIN(view
->nlines
- 2,
2500 s
->commits
.ncommits
- 1);
2506 if (s
->selected
> view
->nlines
- 2)
2507 s
->selected
= view
->nlines
- 2;
2508 if (s
->selected
> s
->commits
.ncommits
- 1)
2509 s
->selected
= s
->commits
.ncommits
- 1;
2511 if (s
->commits
.ncommits
< view
->nlines
- 1 &&
2512 !s
->thread_args
.log_complete
) {
2513 s
->thread_args
.commits_needed
+= (view
->nlines
- 1) -
2514 s
->commits
.ncommits
;
2515 err
= trigger_log_thread(view
, 1);
2521 if (s
->selected_entry
== NULL
)
2523 if (view_is_parent_view(view
))
2524 begin_x
= view_split_begin_x(view
->begin_x
);
2525 err
= open_diff_view_for_commit(&diff_view
, begin_x
,
2526 s
->selected_entry
->commit
, s
->selected_entry
->id
,
2531 diff_view
->focussed
= 1;
2532 if (view_is_parent_view(view
)) {
2533 err
= view_close_child(view
);
2536 view_set_child(view
, diff_view
);
2537 view
->focus_child
= 1;
2539 *new_view
= diff_view
;
2542 if (s
->selected_entry
== NULL
)
2544 if (view_is_parent_view(view
))
2545 begin_x
= view_split_begin_x(view
->begin_x
);
2546 err
= browse_commit_tree(&tree_view
, begin_x
,
2547 s
->selected_entry
, s
->in_repo_path
, s
->head_ref_name
,
2552 tree_view
->focussed
= 1;
2553 if (view_is_parent_view(view
)) {
2554 err
= view_close_child(view
);
2557 view_set_child(view
, tree_view
);
2558 view
->focus_child
= 1;
2560 *new_view
= tree_view
;
2565 if (ch
== KEY_BACKSPACE
&&
2566 got_path_is_root_dir(s
->in_repo_path
))
2568 err
= stop_log_thread(s
);
2571 if (ch
== KEY_BACKSPACE
) {
2573 err
= got_path_dirname(&parent_path
, s
->in_repo_path
);
2576 free(s
->in_repo_path
);
2577 s
->in_repo_path
= parent_path
;
2578 s
->thread_args
.in_repo_path
= s
->in_repo_path
;
2579 } else if (ch
== CTRL('l')) {
2580 struct got_object_id
*start_id
;
2581 err
= got_repo_match_object_id(&start_id
, NULL
,
2582 s
->head_ref_name
? s
->head_ref_name
: GOT_REF_HEAD
,
2583 GOT_OBJ_TYPE_COMMIT
, &tog_refs
, s
->repo
);
2587 s
->start_id
= start_id
;
2588 s
->thread_args
.start_id
= s
->start_id
;
2590 s
->log_branches
= !s
->log_branches
;
2592 err
= got_repo_open(&s
->thread_args
.repo
,
2593 got_repo_get_path(s
->repo
), NULL
);
2597 err
= tog_load_refs(s
->repo
);
2600 err
= got_commit_graph_open(&s
->thread_args
.graph
,
2601 s
->in_repo_path
, !s
->log_branches
);
2604 err
= got_commit_graph_iter_start(s
->thread_args
.graph
,
2605 s
->start_id
, s
->repo
, NULL
, NULL
);
2608 free_commits(&s
->commits
);
2609 s
->first_displayed_entry
= NULL
;
2610 s
->last_displayed_entry
= NULL
;
2611 s
->selected_entry
= NULL
;
2613 s
->thread_args
.log_complete
= 0;
2615 s
->thread_args
.commits_needed
= view
->nlines
;
2618 if (view_is_parent_view(view
))
2619 begin_x
= view_split_begin_x(view
->begin_x
);
2620 ref_view
= view_open(view
->nlines
, view
->ncols
,
2621 view
->begin_y
, begin_x
, TOG_VIEW_REF
);
2622 if (ref_view
== NULL
)
2623 return got_error_from_errno("view_open");
2624 err
= open_ref_view(ref_view
, s
->repo
);
2626 view_close(ref_view
);
2630 ref_view
->focussed
= 1;
2631 if (view_is_parent_view(view
)) {
2632 err
= view_close_child(view
);
2635 view_set_child(view
, ref_view
);
2636 view
->focus_child
= 1;
2638 *new_view
= ref_view
;
2647 static const struct got_error
*
2648 apply_unveil(const char *repo_path
, const char *worktree_path
)
2650 const struct got_error
*error
;
2653 if (unveil("gmon.out", "rwc") != 0)
2654 return got_error_from_errno2("unveil", "gmon.out");
2656 if (repo_path
&& unveil(repo_path
, "r") != 0)
2657 return got_error_from_errno2("unveil", repo_path
);
2659 if (worktree_path
&& unveil(worktree_path
, "rwc") != 0)
2660 return got_error_from_errno2("unveil", worktree_path
);
2662 if (unveil(GOT_TMPDIR_STR
, "rwc") != 0)
2663 return got_error_from_errno2("unveil", GOT_TMPDIR_STR
);
2665 error
= got_privsep_unveil_exec_helpers();
2669 if (unveil(NULL
, NULL
) != 0)
2670 return got_error_from_errno("unveil");
2680 halfdelay(1); /* Do fast refresh while initial view is loading. */
2683 intrflush(stdscr
, FALSE
);
2684 keypad(stdscr
, TRUE
);
2686 if (getenv("TOG_COLORS") != NULL
) {
2688 use_default_colors();
2690 signal(SIGWINCH
, tog_sigwinch
);
2691 signal(SIGPIPE
, tog_sigpipe
);
2692 signal(SIGCONT
, tog_sigcont
);
2695 static const struct got_error
*
2696 get_in_repo_path_from_argv0(char **in_repo_path
, int argc
, char *argv
[],
2697 struct got_repository
*repo
, struct got_worktree
*worktree
)
2699 const struct got_error
*err
= NULL
;
2702 *in_repo_path
= strdup("/");
2703 if (*in_repo_path
== NULL
)
2704 return got_error_from_errno("strdup");
2709 const char *prefix
= got_worktree_get_path_prefix(worktree
);
2712 err
= got_worktree_resolve_path(&p
, worktree
, argv
[0]);
2715 if (asprintf(in_repo_path
, "%s%s%s", prefix
,
2716 (p
[0] != '\0' && !got_path_is_root_dir(prefix
)) ? "/" : "",
2718 err
= got_error_from_errno("asprintf");
2719 *in_repo_path
= NULL
;
2723 err
= got_repo_map_path(in_repo_path
, repo
, argv
[0]);
2728 static const struct got_error
*
2729 cmd_log(int argc
, char *argv
[])
2731 const struct got_error
*error
;
2732 struct got_repository
*repo
= NULL
;
2733 struct got_worktree
*worktree
= NULL
;
2734 struct got_object_id
*start_id
= NULL
;
2735 char *in_repo_path
= NULL
, *repo_path
= NULL
, *cwd
= NULL
;
2736 char *start_commit
= NULL
, *label
= NULL
;
2737 struct got_reference
*ref
= NULL
;
2738 const char *head_ref_name
= NULL
;
2739 int ch
, log_branches
= 0;
2740 struct tog_view
*view
;
2742 while ((ch
= getopt(argc
, argv
, "bc:r:")) != -1) {
2748 start_commit
= optarg
;
2751 repo_path
= realpath(optarg
, NULL
);
2752 if (repo_path
== NULL
)
2753 return got_error_from_errno2("realpath",
2768 if (repo_path
== NULL
) {
2769 cwd
= getcwd(NULL
, 0);
2771 return got_error_from_errno("getcwd");
2772 error
= got_worktree_open(&worktree
, cwd
);
2773 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
2777 strdup(got_worktree_get_repo_path(worktree
));
2779 repo_path
= strdup(cwd
);
2780 if (repo_path
== NULL
) {
2781 error
= got_error_from_errno("strdup");
2786 error
= got_repo_open(&repo
, repo_path
, NULL
);
2790 error
= get_in_repo_path_from_argv0(&in_repo_path
, argc
, argv
,
2797 error
= apply_unveil(got_repo_get_path(repo
),
2798 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
2802 /* already loaded by tog_log_with_path()? */
2803 if (TAILQ_EMPTY(&tog_refs
)) {
2804 error
= tog_load_refs(repo
);
2809 if (start_commit
== NULL
) {
2810 error
= got_repo_match_object_id(&start_id
, &label
,
2811 worktree
? got_worktree_get_head_ref_name(worktree
) :
2812 GOT_REF_HEAD
, GOT_OBJ_TYPE_COMMIT
, &tog_refs
, repo
);
2815 head_ref_name
= label
;
2817 error
= got_ref_open(&ref
, repo
, start_commit
, 0);
2819 head_ref_name
= got_ref_get_name(ref
);
2820 else if (error
->code
!= GOT_ERR_NOT_REF
)
2822 error
= got_repo_match_object_id(&start_id
, NULL
,
2823 start_commit
, GOT_OBJ_TYPE_COMMIT
, &tog_refs
, repo
);
2828 view
= view_open(0, 0, 0, 0, TOG_VIEW_LOG
);
2830 error
= got_error_from_errno("view_open");
2833 error
= open_log_view(view
, start_id
, repo
, head_ref_name
,
2834 in_repo_path
, log_branches
);
2838 /* Release work tree lock. */
2839 got_worktree_close(worktree
);
2842 error
= view_loop(view
);
2852 const struct got_error
*close_err
= got_repo_close(repo
);
2857 got_worktree_close(worktree
);
2866 fprintf(stderr
, "usage: %s diff [-a] [-C number] [-r repository-path] "
2867 "[-w] object1 object2\n", getprogname());
2872 match_line(const char *line
, regex_t
*regex
, size_t nmatch
,
2873 regmatch_t
*regmatch
)
2875 return regexec(regex
, line
, nmatch
, regmatch
, 0) == 0;
2879 match_color(struct tog_colors
*colors
, const char *line
)
2881 struct tog_color
*tc
= NULL
;
2883 STAILQ_FOREACH(tc
, colors
, entry
) {
2884 if (match_line(line
, &tc
->regex
, 0, NULL
))
2891 static const struct got_error
*
2892 add_matched_line(int *wtotal
, const char *line
, int wlimit
, int col_tab_align
,
2893 WINDOW
*window
, regmatch_t
*regmatch
)
2895 const struct got_error
*err
= NULL
;
2902 s
= strndup(line
, regmatch
->rm_so
);
2904 return got_error_from_errno("strndup");
2906 err
= format_line(&wline
, &width
, s
, wlimit
, col_tab_align
);
2911 waddwstr(window
, wline
);
2918 s
= strndup(line
+ regmatch
->rm_so
,
2919 regmatch
->rm_eo
- regmatch
->rm_so
);
2921 err
= got_error_from_errno("strndup");
2925 err
= format_line(&wline
, &width
, s
, wlimit
, col_tab_align
);
2930 wattr_on(window
, A_STANDOUT
, NULL
);
2931 waddwstr(window
, wline
);
2932 wattr_off(window
, A_STANDOUT
, NULL
);
2939 if (wlimit
> 0 && strlen(line
) > regmatch
->rm_eo
) {
2940 err
= format_line(&wline
, &width
,
2941 line
+ regmatch
->rm_eo
, wlimit
, col_tab_align
);
2944 waddwstr(window
, wline
);
2952 static const struct got_error
*
2953 draw_file(struct tog_view
*view
, const char *header
)
2955 struct tog_diff_view_state
*s
= &view
->state
.diff
;
2956 regmatch_t
*regmatch
= &view
->regmatch
;
2957 const struct got_error
*err
;
2960 size_t linesize
= 0;
2962 struct tog_color
*tc
;
2965 int max_lines
= view
->nlines
;
2966 int nlines
= s
->nlines
;
2969 line_offset
= s
->line_offsets
[s
->first_displayed_line
- 1];
2970 if (fseeko(s
->f
, line_offset
, SEEK_SET
) == -1)
2971 return got_error_from_errno("fseek");
2973 werase(view
->window
);
2976 if (asprintf(&line
, "[%d/%d] %s",
2977 s
->first_displayed_line
- 1 + s
->selected_line
, nlines
,
2979 return got_error_from_errno("asprintf");
2980 err
= format_line(&wline
, &width
, line
, view
->ncols
, 0);
2985 if (view_needs_focus_indication(view
))
2986 wstandout(view
->window
);
2987 waddwstr(view
->window
, wline
);
2990 if (view_needs_focus_indication(view
))
2991 wstandend(view
->window
);
2992 if (width
<= view
->ncols
- 1)
2993 waddch(view
->window
, '\n');
3002 while (max_lines
> 0 && nprinted
< max_lines
) {
3003 linelen
= getline(&line
, &linesize
, s
->f
);
3004 if (linelen
== -1) {
3010 return got_ferror(s
->f
, GOT_ERR_IO
);
3013 tc
= match_color(&s
->colors
, line
);
3015 wattr_on(view
->window
,
3016 COLOR_PAIR(tc
->colorpair
), NULL
);
3017 if (s
->first_displayed_line
+ nprinted
== s
->matched_line
&&
3018 regmatch
->rm_so
>= 0 && regmatch
->rm_so
< regmatch
->rm_eo
) {
3019 err
= add_matched_line(&width
, line
, view
->ncols
, 0,
3020 view
->window
, regmatch
);
3026 err
= format_line(&wline
, &width
, line
, view
->ncols
, 0);
3031 waddwstr(view
->window
, wline
);
3036 wattr_off(view
->window
,
3037 COLOR_PAIR(tc
->colorpair
), NULL
);
3038 if (width
<= view
->ncols
- 1)
3039 waddch(view
->window
, '\n');
3044 s
->last_displayed_line
= s
->first_displayed_line
+
3047 s
->last_displayed_line
= s
->first_displayed_line
;
3052 while (nprinted
< view
->nlines
) {
3053 waddch(view
->window
, '\n');
3057 err
= format_line(&wline
, &width
, TOG_EOF_STRING
, view
->ncols
, 0);
3062 wstandout(view
->window
);
3063 waddwstr(view
->window
, wline
);
3066 wstandend(view
->window
);
3073 get_datestr(time_t *time
, char *datebuf
)
3075 struct tm mytm
, *tm
;
3078 tm
= gmtime_r(time
, &mytm
);
3081 s
= asctime_r(tm
, datebuf
);
3084 p
= strchr(s
, '\n');
3090 static const struct got_error
*
3091 get_changed_paths(struct got_pathlist_head
*paths
,
3092 struct got_commit_object
*commit
, struct got_repository
*repo
)
3094 const struct got_error
*err
= NULL
;
3095 struct got_object_id
*tree_id1
= NULL
, *tree_id2
= NULL
;
3096 struct got_tree_object
*tree1
= NULL
, *tree2
= NULL
;
3097 struct got_object_qid
*qid
;
3099 qid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
3101 struct got_commit_object
*pcommit
;
3102 err
= got_object_open_as_commit(&pcommit
, repo
,
3107 tree_id1
= got_object_id_dup(
3108 got_object_commit_get_tree_id(pcommit
));
3109 if (tree_id1
== NULL
) {
3110 got_object_commit_close(pcommit
);
3111 return got_error_from_errno("got_object_id_dup");
3113 got_object_commit_close(pcommit
);
3118 err
= got_object_open_as_tree(&tree1
, repo
, tree_id1
);
3123 tree_id2
= got_object_commit_get_tree_id(commit
);
3124 err
= got_object_open_as_tree(&tree2
, repo
, tree_id2
);
3128 err
= got_diff_tree(tree1
, tree2
, "", "", repo
,
3129 got_diff_tree_collect_changed_paths
, paths
, 0);
3132 got_object_tree_close(tree1
);
3134 got_object_tree_close(tree2
);
3139 static const struct got_error
*
3140 add_line_offset(off_t
**line_offsets
, size_t *nlines
, off_t off
)
3144 p
= reallocarray(*line_offsets
, *nlines
+ 1, sizeof(off_t
));
3146 return got_error_from_errno("reallocarray");
3148 (*line_offsets
)[*nlines
] = off
;
3153 static const struct got_error
*
3154 write_commit_info(off_t
**line_offsets
, size_t *nlines
,
3155 struct got_object_id
*commit_id
, struct got_reflist_head
*refs
,
3156 struct got_repository
*repo
, FILE *outfile
)
3158 const struct got_error
*err
= NULL
;
3159 char datebuf
[26], *datestr
;
3160 struct got_commit_object
*commit
;
3161 char *id_str
= NULL
, *logmsg
= NULL
, *s
= NULL
, *line
;
3162 time_t committer_time
;
3163 const char *author
, *committer
;
3164 char *refs_str
= NULL
;
3165 struct got_pathlist_head changed_paths
;
3166 struct got_pathlist_entry
*pe
;
3170 TAILQ_INIT(&changed_paths
);
3173 err
= build_refs_str(&refs_str
, refs
, commit_id
, repo
);
3178 err
= got_object_open_as_commit(&commit
, repo
, commit_id
);
3182 err
= got_object_id_str(&id_str
, commit_id
);
3184 err
= got_error_from_errno("got_object_id_str");
3188 err
= add_line_offset(line_offsets
, nlines
, 0);
3192 n
= fprintf(outfile
, "commit %s%s%s%s\n", id_str
, refs_str
? " (" : "",
3193 refs_str
? refs_str
: "", refs_str
? ")" : "");
3195 err
= got_error_from_errno("fprintf");
3199 err
= add_line_offset(line_offsets
, nlines
, outoff
);
3203 n
= fprintf(outfile
, "from: %s\n",
3204 got_object_commit_get_author(commit
));
3206 err
= got_error_from_errno("fprintf");
3210 err
= add_line_offset(line_offsets
, nlines
, outoff
);
3214 committer_time
= got_object_commit_get_committer_time(commit
);
3215 datestr
= get_datestr(&committer_time
, datebuf
);
3217 n
= fprintf(outfile
, "date: %s UTC\n", datestr
);
3219 err
= got_error_from_errno("fprintf");
3223 err
= add_line_offset(line_offsets
, nlines
, outoff
);
3227 author
= got_object_commit_get_author(commit
);
3228 committer
= got_object_commit_get_committer(commit
);
3229 if (strcmp(author
, committer
) != 0) {
3230 n
= fprintf(outfile
, "via: %s\n", committer
);
3232 err
= got_error_from_errno("fprintf");
3236 err
= add_line_offset(line_offsets
, nlines
, outoff
);
3240 if (got_object_commit_get_nparents(commit
) > 1) {
3241 const struct got_object_id_queue
*parent_ids
;
3242 struct got_object_qid
*qid
;
3244 parent_ids
= got_object_commit_get_parent_ids(commit
);
3245 STAILQ_FOREACH(qid
, parent_ids
, entry
) {
3246 err
= got_object_id_str(&id_str
, qid
->id
);
3249 n
= fprintf(outfile
, "parent %d: %s\n", pn
++, id_str
);
3251 err
= got_error_from_errno("fprintf");
3255 err
= add_line_offset(line_offsets
, nlines
, outoff
);
3263 err
= got_object_commit_get_logmsg(&logmsg
, commit
);
3267 while ((line
= strsep(&s
, "\n")) != NULL
) {
3268 n
= fprintf(outfile
, "%s\n", line
);
3270 err
= got_error_from_errno("fprintf");
3274 err
= add_line_offset(line_offsets
, nlines
, outoff
);
3279 err
= get_changed_paths(&changed_paths
, commit
, repo
);
3282 TAILQ_FOREACH(pe
, &changed_paths
, entry
) {
3283 struct got_diff_changed_path
*cp
= pe
->data
;
3284 n
= fprintf(outfile
, "%c %s\n", cp
->status
, pe
->path
);
3286 err
= got_error_from_errno("fprintf");
3290 err
= add_line_offset(line_offsets
, nlines
, outoff
);
3293 free((char *)pe
->path
);
3297 fputc('\n', outfile
);
3299 err
= add_line_offset(line_offsets
, nlines
, outoff
);
3301 got_pathlist_free(&changed_paths
);
3305 got_object_commit_close(commit
);
3307 free(*line_offsets
);
3308 *line_offsets
= NULL
;
3314 static const struct got_error
*
3315 create_diff(struct tog_diff_view_state
*s
)
3317 const struct got_error
*err
= NULL
;
3321 free(s
->line_offsets
);
3322 s
->line_offsets
= malloc(sizeof(off_t
));
3323 if (s
->line_offsets
== NULL
)
3324 return got_error_from_errno("malloc");
3329 err
= got_error_from_errno("got_opentemp");
3332 if (s
->f
&& fclose(s
->f
) == EOF
) {
3333 err
= got_error_from_errno("fclose");
3339 err
= got_object_get_type(&obj_type
, s
->repo
, s
->id1
);
3341 err
= got_object_get_type(&obj_type
, s
->repo
, s
->id2
);
3346 case GOT_OBJ_TYPE_BLOB
:
3347 err
= got_diff_objects_as_blobs(&s
->line_offsets
, &s
->nlines
,
3348 s
->id1
, s
->id2
, s
->label1
, s
->label2
, s
->diff_context
,
3349 s
->ignore_whitespace
, s
->force_text_diff
, s
->repo
, s
->f
);
3351 case GOT_OBJ_TYPE_TREE
:
3352 err
= got_diff_objects_as_trees(&s
->line_offsets
, &s
->nlines
,
3353 s
->id1
, s
->id2
, NULL
, "", "", s
->diff_context
,
3354 s
->ignore_whitespace
, s
->force_text_diff
, s
->repo
, s
->f
);
3356 case GOT_OBJ_TYPE_COMMIT
: {
3357 const struct got_object_id_queue
*parent_ids
;
3358 struct got_object_qid
*pid
;
3359 struct got_commit_object
*commit2
;
3360 struct got_reflist_head
*refs
;
3362 err
= got_object_open_as_commit(&commit2
, s
->repo
, s
->id2
);
3365 refs
= got_reflist_object_id_map_lookup(tog_refs_idmap
, s
->id2
);
3366 /* Show commit info if we're diffing to a parent/root commit. */
3367 if (s
->id1
== NULL
) {
3368 err
= write_commit_info(&s
->line_offsets
, &s
->nlines
,
3369 s
->id2
, refs
, s
->repo
, s
->f
);
3373 parent_ids
= got_object_commit_get_parent_ids(commit2
);
3374 STAILQ_FOREACH(pid
, parent_ids
, entry
) {
3375 if (got_object_id_cmp(s
->id1
, pid
->id
) == 0) {
3376 err
= write_commit_info(
3377 &s
->line_offsets
, &s
->nlines
,
3378 s
->id2
, refs
, s
->repo
, s
->f
);
3385 got_object_commit_close(commit2
);
3387 err
= got_diff_objects_as_commits(&s
->line_offsets
, &s
->nlines
,
3388 s
->id1
, s
->id2
, NULL
, s
->diff_context
, s
->ignore_whitespace
,
3389 s
->force_text_diff
, s
->repo
, s
->f
);
3393 err
= got_error(GOT_ERR_OBJ_TYPE
);
3399 if (s
->f
&& fflush(s
->f
) != 0 && err
== NULL
)
3400 err
= got_error_from_errno("fflush");
3405 diff_view_indicate_progress(struct tog_view
*view
)
3407 mvwaddstr(view
->window
, 0, 0, "diffing...");
3412 static const struct got_error
*
3413 search_start_diff_view(struct tog_view
*view
)
3415 struct tog_diff_view_state
*s
= &view
->state
.diff
;
3417 s
->matched_line
= 0;
3421 static const struct got_error
*
3422 search_next_diff_view(struct tog_view
*view
)
3424 struct tog_diff_view_state
*s
= &view
->state
.diff
;
3427 size_t linesize
= 0;
3430 if (!view
->searching
) {
3431 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
3435 if (s
->matched_line
) {
3436 if (view
->searching
== TOG_SEARCH_FORWARD
)
3437 lineno
= s
->matched_line
+ 1;
3439 lineno
= s
->matched_line
- 1;
3441 if (view
->searching
== TOG_SEARCH_FORWARD
)
3450 if (lineno
<= 0 || lineno
> s
->nlines
) {
3451 if (s
->matched_line
== 0) {
3452 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
3456 if (view
->searching
== TOG_SEARCH_FORWARD
)
3462 offset
= s
->line_offsets
[lineno
- 1];
3463 if (fseeko(s
->f
, offset
, SEEK_SET
) != 0) {
3465 return got_error_from_errno("fseeko");
3467 linelen
= getline(&line
, &linesize
, s
->f
);
3468 if (linelen
!= -1 &&
3469 match_line(line
, &view
->regex
, 1, &view
->regmatch
)) {
3470 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
3471 s
->matched_line
= lineno
;
3474 if (view
->searching
== TOG_SEARCH_FORWARD
)
3481 if (s
->matched_line
) {
3482 s
->first_displayed_line
= s
->matched_line
;
3483 s
->selected_line
= 1;
3489 static const struct got_error
*
3490 open_diff_view(struct tog_view
*view
, struct got_object_id
*id1
,
3491 struct got_object_id
*id2
, const char *label1
, const char *label2
,
3492 int diff_context
, int ignore_whitespace
, int force_text_diff
,
3493 struct tog_view
*log_view
, struct got_repository
*repo
)
3495 const struct got_error
*err
;
3496 struct tog_diff_view_state
*s
= &view
->state
.diff
;
3498 if (id1
!= NULL
&& id2
!= NULL
) {
3500 err
= got_object_get_type(&type1
, repo
, id1
);
3503 err
= got_object_get_type(&type2
, repo
, id2
);
3508 return got_error(GOT_ERR_OBJ_TYPE
);
3510 s
->first_displayed_line
= 1;
3511 s
->last_displayed_line
= view
->nlines
;
3512 s
->selected_line
= 1;
3520 s
->id1
= got_object_id_dup(id1
);
3522 return got_error_from_errno("got_object_id_dup");
3526 s
->id2
= got_object_id_dup(id2
);
3527 if (s
->id2
== NULL
) {
3530 return got_error_from_errno("got_object_id_dup");
3533 s
->first_displayed_line
= 1;
3534 s
->last_displayed_line
= view
->nlines
;
3535 s
->diff_context
= diff_context
;
3536 s
->ignore_whitespace
= ignore_whitespace
;
3537 s
->force_text_diff
= force_text_diff
;
3538 s
->log_view
= log_view
;
3541 STAILQ_INIT(&s
->colors
);
3542 if (has_colors() && getenv("TOG_COLORS") != NULL
) {
3543 err
= add_color(&s
->colors
,
3544 "^-", TOG_COLOR_DIFF_MINUS
,
3545 get_color_value("TOG_COLOR_DIFF_MINUS"));
3548 err
= add_color(&s
->colors
, "^\\+",
3549 TOG_COLOR_DIFF_PLUS
,
3550 get_color_value("TOG_COLOR_DIFF_PLUS"));
3552 free_colors(&s
->colors
);
3555 err
= add_color(&s
->colors
,
3556 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER
,
3557 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3559 free_colors(&s
->colors
);
3563 err
= add_color(&s
->colors
,
3564 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3565 "[MDmA] [^ ])", TOG_COLOR_DIFF_META
,
3566 get_color_value("TOG_COLOR_DIFF_META"));
3568 free_colors(&s
->colors
);
3572 err
= add_color(&s
->colors
,
3573 "^(from|via): ", TOG_COLOR_AUTHOR
,
3574 get_color_value("TOG_COLOR_AUTHOR"));
3576 free_colors(&s
->colors
);
3580 err
= add_color(&s
->colors
,
3581 "^date: ", TOG_COLOR_DATE
,
3582 get_color_value("TOG_COLOR_DATE"));
3584 free_colors(&s
->colors
);
3589 if (log_view
&& view_is_splitscreen(view
))
3590 show_log_view(log_view
); /* draw vborder */
3591 diff_view_indicate_progress(view
);
3593 s
->line_offsets
= NULL
;
3595 err
= create_diff(s
);
3601 free_colors(&s
->colors
);
3605 view
->show
= show_diff_view
;
3606 view
->input
= input_diff_view
;
3607 view
->close
= close_diff_view
;
3608 view
->search_start
= search_start_diff_view
;
3609 view
->search_next
= search_next_diff_view
;
3614 static const struct got_error
*
3615 close_diff_view(struct tog_view
*view
)
3617 const struct got_error
*err
= NULL
;
3618 struct tog_diff_view_state
*s
= &view
->state
.diff
;
3624 if (s
->f
&& fclose(s
->f
) == EOF
)
3625 err
= got_error_from_errno("fclose");
3626 free_colors(&s
->colors
);
3627 free(s
->line_offsets
);
3628 s
->line_offsets
= NULL
;
3633 static const struct got_error
*
3634 show_diff_view(struct tog_view
*view
)
3636 const struct got_error
*err
;
3637 struct tog_diff_view_state
*s
= &view
->state
.diff
;
3638 char *id_str1
= NULL
, *id_str2
, *header
;
3639 const char *label1
, *label2
;
3642 err
= got_object_id_str(&id_str1
, s
->id1
);
3645 label1
= s
->label1
? : id_str1
;
3647 label1
= "/dev/null";
3649 err
= got_object_id_str(&id_str2
, s
->id2
);
3652 label2
= s
->label2
? : id_str2
;
3654 if (asprintf(&header
, "diff %s %s", label1
, label2
) == -1) {
3655 err
= got_error_from_errno("asprintf");
3663 err
= draw_file(view
, header
);
3668 static const struct got_error
*
3669 set_selected_commit(struct tog_diff_view_state
*s
,
3670 struct commit_queue_entry
*entry
)
3672 const struct got_error
*err
;
3673 const struct got_object_id_queue
*parent_ids
;
3674 struct got_commit_object
*selected_commit
;
3675 struct got_object_qid
*pid
;
3678 s
->id2
= got_object_id_dup(entry
->id
);
3680 return got_error_from_errno("got_object_id_dup");
3682 err
= got_object_open_as_commit(&selected_commit
, s
->repo
, entry
->id
);
3685 parent_ids
= got_object_commit_get_parent_ids(selected_commit
);
3687 pid
= STAILQ_FIRST(parent_ids
);
3688 s
->id1
= pid
? got_object_id_dup(pid
->id
) : NULL
;
3689 got_object_commit_close(selected_commit
);
3693 static const struct got_error
*
3694 input_diff_view(struct tog_view
**new_view
, struct tog_view
*view
, int ch
)
3696 const struct got_error
*err
= NULL
;
3697 struct tog_diff_view_state
*s
= &view
->state
.diff
;
3698 struct tog_log_view_state
*ls
;
3699 struct commit_queue_entry
*old_selected_entry
;
3701 size_t linesize
= 0;
3709 s
->force_text_diff
= !s
->force_text_diff
;
3711 s
->ignore_whitespace
= !s
->ignore_whitespace
;
3712 wclear(view
->window
);
3713 s
->first_displayed_line
= 1;
3714 s
->last_displayed_line
= view
->nlines
;
3715 diff_view_indicate_progress(view
);
3716 err
= create_diff(s
);
3720 s
->first_displayed_line
= 1;
3727 s
->first_displayed_line
= (s
->nlines
- view
->nlines
) + 2;
3732 if (s
->first_displayed_line
> 1)
3733 s
->first_displayed_line
--;
3737 if (s
->first_displayed_line
== 1)
3740 while (i
++ < view
->nlines
- 1 &&
3741 s
->first_displayed_line
> 1)
3742 s
->first_displayed_line
--;
3747 s
->first_displayed_line
++;
3755 while (!s
->eof
&& i
++ < view
->nlines
- 1) {
3756 linelen
= getline(&line
, &linesize
, s
->f
);
3757 s
->first_displayed_line
++;
3758 if (linelen
== -1) {
3762 err
= got_ferror(s
->f
, GOT_ERR_IO
);
3769 if (s
->diff_context
> 0) {
3771 diff_view_indicate_progress(view
);
3772 err
= create_diff(s
);
3773 if (s
->first_displayed_line
+ view
->nlines
- 1 >
3775 s
->first_displayed_line
= 1;
3776 s
->last_displayed_line
= view
->nlines
;
3781 if (s
->diff_context
< GOT_DIFF_MAX_CONTEXT
) {
3783 diff_view_indicate_progress(view
);
3784 err
= create_diff(s
);
3789 if (s
->log_view
== NULL
)
3791 ls
= &s
->log_view
->state
.log
;
3792 old_selected_entry
= ls
->selected_entry
;
3794 err
= input_log_view(NULL
, s
->log_view
, KEY_UP
);
3798 if (old_selected_entry
== ls
->selected_entry
)
3801 err
= set_selected_commit(s
, ls
->selected_entry
);
3805 s
->first_displayed_line
= 1;
3806 s
->last_displayed_line
= view
->nlines
;
3808 diff_view_indicate_progress(view
);
3809 err
= create_diff(s
);
3813 if (s
->log_view
== NULL
)
3815 ls
= &s
->log_view
->state
.log
;
3816 old_selected_entry
= ls
->selected_entry
;
3818 err
= input_log_view(NULL
, s
->log_view
, KEY_DOWN
);
3822 if (old_selected_entry
== ls
->selected_entry
)
3825 err
= set_selected_commit(s
, ls
->selected_entry
);
3829 s
->first_displayed_line
= 1;
3830 s
->last_displayed_line
= view
->nlines
;
3832 diff_view_indicate_progress(view
);
3833 err
= create_diff(s
);
3842 static const struct got_error
*
3843 cmd_diff(int argc
, char *argv
[])
3845 const struct got_error
*error
= NULL
;
3846 struct got_repository
*repo
= NULL
;
3847 struct got_worktree
*worktree
= NULL
;
3848 struct got_object_id
*id1
= NULL
, *id2
= NULL
;
3849 char *repo_path
= NULL
, *cwd
= NULL
;
3850 char *id_str1
= NULL
, *id_str2
= NULL
;
3851 char *label1
= NULL
, *label2
= NULL
;
3852 int diff_context
= 3, ignore_whitespace
= 0;
3853 int ch
, force_text_diff
= 0;
3855 struct tog_view
*view
;
3857 while ((ch
= getopt(argc
, argv
, "aC:r:w")) != -1) {
3860 force_text_diff
= 1;
3863 diff_context
= strtonum(optarg
, 0, GOT_DIFF_MAX_CONTEXT
,
3866 err(1, "-C option %s", errstr
);
3869 repo_path
= realpath(optarg
, NULL
);
3870 if (repo_path
== NULL
)
3871 return got_error_from_errno2("realpath",
3873 got_path_strip_trailing_slashes(repo_path
);
3876 ignore_whitespace
= 1;
3888 usage_diff(); /* TODO show local worktree changes */
3889 } else if (argc
== 2) {
3895 if (repo_path
== NULL
) {
3896 cwd
= getcwd(NULL
, 0);
3898 return got_error_from_errno("getcwd");
3899 error
= got_worktree_open(&worktree
, cwd
);
3900 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
3904 strdup(got_worktree_get_repo_path(worktree
));
3906 repo_path
= strdup(cwd
);
3907 if (repo_path
== NULL
) {
3908 error
= got_error_from_errno("strdup");
3913 error
= got_repo_open(&repo
, repo_path
, NULL
);
3919 error
= apply_unveil(got_repo_get_path(repo
), NULL
);
3923 error
= tog_load_refs(repo
);
3927 error
= got_repo_match_object_id(&id1
, &label1
, id_str1
,
3928 GOT_OBJ_TYPE_ANY
, &tog_refs
, repo
);
3932 error
= got_repo_match_object_id(&id2
, &label2
, id_str2
,
3933 GOT_OBJ_TYPE_ANY
, &tog_refs
, repo
);
3937 view
= view_open(0, 0, 0, 0, TOG_VIEW_DIFF
);
3939 error
= got_error_from_errno("view_open");
3942 error
= open_diff_view(view
, id1
, id2
, label1
, label2
, diff_context
,
3943 ignore_whitespace
, force_text_diff
, NULL
, repo
);
3946 error
= view_loop(view
);
3953 const struct got_error
*close_err
= got_repo_close(repo
);
3958 got_worktree_close(worktree
);
3967 fprintf(stderr
, "usage: %s blame [-c commit] [-r repository-path] path\n",
3972 struct tog_blame_line
{
3974 struct got_object_id
*id
;
3977 static const struct got_error
*
3978 draw_blame(struct tog_view
*view
)
3980 struct tog_blame_view_state
*s
= &view
->state
.blame
;
3981 struct tog_blame
*blame
= &s
->blame
;
3982 regmatch_t
*regmatch
= &view
->regmatch
;
3983 const struct got_error
*err
;
3984 int lineno
= 0, nprinted
= 0;
3986 size_t linesize
= 0;
3990 struct tog_blame_line
*blame_line
;
3991 struct got_object_id
*prev_id
= NULL
;
3993 struct tog_color
*tc
;
3995 err
= got_object_id_str(&id_str
, s
->blamed_commit
->id
);
4000 werase(view
->window
);
4002 if (asprintf(&line
, "commit %s", id_str
) == -1) {
4003 err
= got_error_from_errno("asprintf");
4008 err
= format_line(&wline
, &width
, line
, view
->ncols
, 0);
4013 if (view_needs_focus_indication(view
))
4014 wstandout(view
->window
);
4015 tc
= get_color(&s
->colors
, TOG_COLOR_COMMIT
);
4017 wattr_on(view
->window
,
4018 COLOR_PAIR(tc
->colorpair
), NULL
);
4019 waddwstr(view
->window
, wline
);
4021 wattr_off(view
->window
,
4022 COLOR_PAIR(tc
->colorpair
), NULL
);
4023 if (view_needs_focus_indication(view
))
4024 wstandend(view
->window
);
4027 if (width
< view
->ncols
- 1)
4028 waddch(view
->window
, '\n');
4030 if (asprintf(&line
, "[%d/%d] %s%s",
4031 s
->first_displayed_line
- 1 + s
->selected_line
, blame
->nlines
,
4032 s
->blame_complete
? "" : "annotating... ", s
->path
) == -1) {
4034 return got_error_from_errno("asprintf");
4037 err
= format_line(&wline
, &width
, line
, view
->ncols
, 0);
4042 waddwstr(view
->window
, wline
);
4045 if (width
< view
->ncols
- 1)
4046 waddch(view
->window
, '\n');
4049 while (nprinted
< view
->nlines
- 2) {
4050 linelen
= getline(&line
, &linesize
, blame
->f
);
4051 if (linelen
== -1) {
4052 if (feof(blame
->f
)) {
4057 return got_ferror(blame
->f
, GOT_ERR_IO
);
4059 if (++lineno
< s
->first_displayed_line
)
4062 if (view
->focussed
&& nprinted
== s
->selected_line
- 1)
4063 wstandout(view
->window
);
4065 if (blame
->nlines
> 0) {
4066 blame_line
= &blame
->lines
[lineno
- 1];
4067 if (blame_line
->annotated
&& prev_id
&&
4068 got_object_id_cmp(prev_id
, blame_line
->id
) == 0 &&
4070 nprinted
== s
->selected_line
- 1)) {
4071 waddstr(view
->window
, " ");
4072 } else if (blame_line
->annotated
) {
4074 err
= got_object_id_str(&id_str
, blame_line
->id
);
4079 tc
= get_color(&s
->colors
, TOG_COLOR_COMMIT
);
4081 wattr_on(view
->window
,
4082 COLOR_PAIR(tc
->colorpair
), NULL
);
4083 wprintw(view
->window
, "%.8s", id_str
);
4085 wattr_off(view
->window
,
4086 COLOR_PAIR(tc
->colorpair
), NULL
);
4088 prev_id
= blame_line
->id
;
4090 waddstr(view
->window
, "........");
4094 waddstr(view
->window
, "........");
4098 if (view
->focussed
&& nprinted
== s
->selected_line
- 1)
4099 wstandend(view
->window
);
4100 waddstr(view
->window
, " ");
4102 if (view
->ncols
<= 9) {
4104 wline
= wcsdup(L
"");
4105 if (wline
== NULL
) {
4106 err
= got_error_from_errno("wcsdup");
4110 } else if (s
->first_displayed_line
+ nprinted
==
4112 regmatch
->rm_so
>= 0 && regmatch
->rm_so
< regmatch
->rm_eo
) {
4113 err
= add_matched_line(&width
, line
, view
->ncols
- 9, 9,
4114 view
->window
, regmatch
);
4121 err
= format_line(&wline
, &width
, line
,
4122 view
->ncols
- 9, 9);
4123 waddwstr(view
->window
, wline
);
4129 if (width
<= view
->ncols
- 1)
4130 waddch(view
->window
, '\n');
4131 if (++nprinted
== 1)
4132 s
->first_displayed_line
= lineno
;
4135 s
->last_displayed_line
= lineno
;
4142 static const struct got_error
*
4143 blame_cb(void *arg
, int nlines
, int lineno
, struct got_object_id
*id
)
4145 const struct got_error
*err
= NULL
;
4146 struct tog_blame_cb_args
*a
= arg
;
4147 struct tog_blame_line
*line
;
4150 if (nlines
!= a
->nlines
||
4151 (lineno
!= -1 && lineno
< 1) || lineno
> a
->nlines
)
4152 return got_error(GOT_ERR_RANGE
);
4154 errcode
= pthread_mutex_lock(&tog_mutex
);
4156 return got_error_set_errno(errcode
, "pthread_mutex_lock");
4158 if (*a
->quit
) { /* user has quit the blame view */
4159 err
= got_error(GOT_ERR_ITER_COMPLETED
);
4164 goto done
; /* no change in this commit */
4166 line
= &a
->lines
[lineno
- 1];
4167 if (line
->annotated
)
4170 line
->id
= got_object_id_dup(id
);
4171 if (line
->id
== NULL
) {
4172 err
= got_error_from_errno("got_object_id_dup");
4175 line
->annotated
= 1;
4177 errcode
= pthread_mutex_unlock(&tog_mutex
);
4179 err
= got_error_set_errno(errcode
, "pthread_mutex_unlock");
4184 blame_thread(void *arg
)
4186 const struct got_error
*err
, *close_err
;
4187 struct tog_blame_thread_args
*ta
= arg
;
4188 struct tog_blame_cb_args
*a
= ta
->cb_args
;
4191 err
= block_signals_used_by_main_thread();
4195 err
= got_blame(ta
->path
, a
->commit_id
, ta
->repo
,
4196 blame_cb
, ta
->cb_args
, ta
->cancel_cb
, ta
->cancel_arg
);
4197 if (err
&& err
->code
== GOT_ERR_CANCELLED
)
4200 errcode
= pthread_mutex_lock(&tog_mutex
);
4202 return (void *)got_error_set_errno(errcode
,
4203 "pthread_mutex_lock");
4205 close_err
= got_repo_close(ta
->repo
);
4211 errcode
= pthread_mutex_unlock(&tog_mutex
);
4212 if (errcode
&& err
== NULL
)
4213 err
= got_error_set_errno(errcode
, "pthread_mutex_unlock");
4218 static struct got_object_id
*
4219 get_selected_commit_id(struct tog_blame_line
*lines
, int nlines
,
4220 int first_displayed_line
, int selected_line
)
4222 struct tog_blame_line
*line
;
4227 line
= &lines
[first_displayed_line
- 1 + selected_line
- 1];
4228 if (!line
->annotated
)
4234 static const struct got_error
*
4235 stop_blame(struct tog_blame
*blame
)
4237 const struct got_error
*err
= NULL
;
4240 if (blame
->thread
) {
4242 errcode
= pthread_mutex_unlock(&tog_mutex
);
4244 return got_error_set_errno(errcode
,
4245 "pthread_mutex_unlock");
4246 errcode
= pthread_join(blame
->thread
, (void **)&err
);
4248 return got_error_set_errno(errcode
, "pthread_join");
4249 errcode
= pthread_mutex_lock(&tog_mutex
);
4251 return got_error_set_errno(errcode
,
4252 "pthread_mutex_lock");
4253 if (err
&& err
->code
== GOT_ERR_ITER_COMPLETED
)
4255 blame
->thread
= 0; //NULL;
4257 if (blame
->thread_args
.repo
) {
4258 const struct got_error
*close_err
;
4259 close_err
= got_repo_close(blame
->thread_args
.repo
);
4262 blame
->thread_args
.repo
= NULL
;
4265 if (fclose(blame
->f
) == EOF
&& err
== NULL
)
4266 err
= got_error_from_errno("fclose");
4270 for (i
= 0; i
< blame
->nlines
; i
++)
4271 free(blame
->lines
[i
].id
);
4273 blame
->lines
= NULL
;
4275 free(blame
->cb_args
.commit_id
);
4276 blame
->cb_args
.commit_id
= NULL
;
4281 static const struct got_error
*
4282 cancel_blame_view(void *arg
)
4284 const struct got_error
*err
= NULL
;
4288 errcode
= pthread_mutex_lock(&tog_mutex
);
4290 return got_error_set_errno(errcode
,
4291 "pthread_mutex_unlock");
4294 err
= got_error(GOT_ERR_CANCELLED
);
4296 errcode
= pthread_mutex_unlock(&tog_mutex
);
4298 return got_error_set_errno(errcode
,
4299 "pthread_mutex_lock");
4304 static const struct got_error
*
4305 run_blame(struct tog_view
*view
)
4307 struct tog_blame_view_state
*s
= &view
->state
.blame
;
4308 struct tog_blame
*blame
= &s
->blame
;
4309 const struct got_error
*err
= NULL
;
4310 struct got_blob_object
*blob
= NULL
;
4311 struct got_repository
*thread_repo
= NULL
;
4312 struct got_object_id
*obj_id
= NULL
;
4315 err
= got_object_id_by_path(&obj_id
, s
->repo
, s
->blamed_commit
->id
,
4320 err
= got_object_get_type(&obj_type
, s
->repo
, obj_id
);
4324 if (obj_type
!= GOT_OBJ_TYPE_BLOB
) {
4325 err
= got_error(GOT_ERR_OBJ_TYPE
);
4329 err
= got_object_open_as_blob(&blob
, s
->repo
, obj_id
, 8192);
4332 blame
->f
= got_opentemp();
4333 if (blame
->f
== NULL
) {
4334 err
= got_error_from_errno("got_opentemp");
4337 err
= got_object_blob_dump_to_file(&blame
->filesize
, &blame
->nlines
,
4338 &blame
->line_offsets
, blame
->f
, blob
);
4341 if (blame
->nlines
== 0) {
4342 s
->blame_complete
= 1;
4346 /* Don't include \n at EOF in the blame line count. */
4347 if (blame
->line_offsets
[blame
->nlines
- 1] == blame
->filesize
)
4350 blame
->lines
= calloc(blame
->nlines
, sizeof(*blame
->lines
));
4351 if (blame
->lines
== NULL
) {
4352 err
= got_error_from_errno("calloc");
4356 err
= got_repo_open(&thread_repo
, got_repo_get_path(s
->repo
), NULL
);
4360 blame
->cb_args
.view
= view
;
4361 blame
->cb_args
.lines
= blame
->lines
;
4362 blame
->cb_args
.nlines
= blame
->nlines
;
4363 blame
->cb_args
.commit_id
= got_object_id_dup(s
->blamed_commit
->id
);
4364 if (blame
->cb_args
.commit_id
== NULL
) {
4365 err
= got_error_from_errno("got_object_id_dup");
4368 blame
->cb_args
.quit
= &s
->done
;
4370 blame
->thread_args
.path
= s
->path
;
4371 blame
->thread_args
.repo
= thread_repo
;
4372 blame
->thread_args
.cb_args
= &blame
->cb_args
;
4373 blame
->thread_args
.complete
= &s
->blame_complete
;
4374 blame
->thread_args
.cancel_cb
= cancel_blame_view
;
4375 blame
->thread_args
.cancel_arg
= &s
->done
;
4376 s
->blame_complete
= 0;
4378 if (s
->first_displayed_line
+ view
->nlines
- 1 > blame
->nlines
) {
4379 s
->first_displayed_line
= 1;
4380 s
->last_displayed_line
= view
->nlines
;
4381 s
->selected_line
= 1;
4386 got_object_blob_close(blob
);
4393 static const struct got_error
*
4394 open_blame_view(struct tog_view
*view
, char *path
,
4395 struct got_object_id
*commit_id
, struct got_repository
*repo
)
4397 const struct got_error
*err
= NULL
;
4398 struct tog_blame_view_state
*s
= &view
->state
.blame
;
4400 STAILQ_INIT(&s
->blamed_commits
);
4402 s
->path
= strdup(path
);
4403 if (s
->path
== NULL
)
4404 return got_error_from_errno("strdup");
4406 err
= got_object_qid_alloc(&s
->blamed_commit
, commit_id
);
4412 STAILQ_INSERT_HEAD(&s
->blamed_commits
, s
->blamed_commit
, entry
);
4413 s
->first_displayed_line
= 1;
4414 s
->last_displayed_line
= view
->nlines
;
4415 s
->selected_line
= 1;
4416 s
->blame_complete
= 0;
4418 s
->commit_id
= commit_id
;
4419 memset(&s
->blame
, 0, sizeof(s
->blame
));
4421 STAILQ_INIT(&s
->colors
);
4422 if (has_colors() && getenv("TOG_COLORS") != NULL
) {
4423 err
= add_color(&s
->colors
, "^", TOG_COLOR_COMMIT
,
4424 get_color_value("TOG_COLOR_COMMIT"));
4429 view
->show
= show_blame_view
;
4430 view
->input
= input_blame_view
;
4431 view
->close
= close_blame_view
;
4432 view
->search_start
= search_start_blame_view
;
4433 view
->search_next
= search_next_blame_view
;
4435 return run_blame(view
);
4438 static const struct got_error
*
4439 close_blame_view(struct tog_view
*view
)
4441 const struct got_error
*err
= NULL
;
4442 struct tog_blame_view_state
*s
= &view
->state
.blame
;
4444 if (s
->blame
.thread
)
4445 err
= stop_blame(&s
->blame
);
4447 while (!STAILQ_EMPTY(&s
->blamed_commits
)) {
4448 struct got_object_qid
*blamed_commit
;
4449 blamed_commit
= STAILQ_FIRST(&s
->blamed_commits
);
4450 STAILQ_REMOVE_HEAD(&s
->blamed_commits
, entry
);
4451 got_object_qid_free(blamed_commit
);
4455 free_colors(&s
->colors
);
4460 static const struct got_error
*
4461 search_start_blame_view(struct tog_view
*view
)
4463 struct tog_blame_view_state
*s
= &view
->state
.blame
;
4465 s
->matched_line
= 0;
4469 static const struct got_error
*
4470 search_next_blame_view(struct tog_view
*view
)
4472 struct tog_blame_view_state
*s
= &view
->state
.blame
;
4475 size_t linesize
= 0;
4478 if (!view
->searching
) {
4479 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
4483 if (s
->matched_line
) {
4484 if (view
->searching
== TOG_SEARCH_FORWARD
)
4485 lineno
= s
->matched_line
+ 1;
4487 lineno
= s
->matched_line
- 1;
4489 if (view
->searching
== TOG_SEARCH_FORWARD
)
4492 lineno
= s
->blame
.nlines
;
4498 if (lineno
<= 0 || lineno
> s
->blame
.nlines
) {
4499 if (s
->matched_line
== 0) {
4500 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
4504 if (view
->searching
== TOG_SEARCH_FORWARD
)
4507 lineno
= s
->blame
.nlines
;
4510 offset
= s
->blame
.line_offsets
[lineno
- 1];
4511 if (fseeko(s
->blame
.f
, offset
, SEEK_SET
) != 0) {
4513 return got_error_from_errno("fseeko");
4515 linelen
= getline(&line
, &linesize
, s
->blame
.f
);
4516 if (linelen
!= -1 &&
4517 match_line(line
, &view
->regex
, 1, &view
->regmatch
)) {
4518 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
4519 s
->matched_line
= lineno
;
4522 if (view
->searching
== TOG_SEARCH_FORWARD
)
4529 if (s
->matched_line
) {
4530 s
->first_displayed_line
= s
->matched_line
;
4531 s
->selected_line
= 1;
4537 static const struct got_error
*
4538 show_blame_view(struct tog_view
*view
)
4540 const struct got_error
*err
= NULL
;
4541 struct tog_blame_view_state
*s
= &view
->state
.blame
;
4544 if (s
->blame
.thread
== 0 && !s
->blame_complete
) {
4545 errcode
= pthread_create(&s
->blame
.thread
, NULL
, blame_thread
,
4546 &s
->blame
.thread_args
);
4548 return got_error_set_errno(errcode
, "pthread_create");
4550 halfdelay(1); /* fast refresh while annotating */
4553 if (s
->blame_complete
)
4554 halfdelay(10); /* disable fast refresh */
4556 err
= draw_blame(view
);
4562 static const struct got_error
*
4563 input_blame_view(struct tog_view
**new_view
, struct tog_view
*view
, int ch
)
4565 const struct got_error
*err
= NULL
, *thread_err
= NULL
;
4566 struct tog_view
*diff_view
;
4567 struct tog_blame_view_state
*s
= &view
->state
.blame
;
4576 s
->selected_line
= 1;
4577 s
->first_displayed_line
= 1;
4581 if (s
->blame
.nlines
< view
->nlines
- 2) {
4582 s
->selected_line
= s
->blame
.nlines
;
4583 s
->first_displayed_line
= 1;
4585 s
->selected_line
= view
->nlines
- 2;
4586 s
->first_displayed_line
= s
->blame
.nlines
-
4592 if (s
->selected_line
> 1)
4594 else if (s
->selected_line
== 1 &&
4595 s
->first_displayed_line
> 1)
4596 s
->first_displayed_line
--;
4600 if (s
->first_displayed_line
== 1) {
4601 s
->selected_line
= 1;
4604 if (s
->first_displayed_line
> view
->nlines
- 2)
4605 s
->first_displayed_line
-=
4608 s
->first_displayed_line
= 1;
4612 if (s
->selected_line
< view
->nlines
- 2 &&
4613 s
->first_displayed_line
+
4614 s
->selected_line
<= s
->blame
.nlines
)
4616 else if (s
->last_displayed_line
<
4618 s
->first_displayed_line
++;
4622 struct got_object_id
*id
= NULL
;
4623 id
= get_selected_commit_id(s
->blame
.lines
, s
->blame
.nlines
,
4624 s
->first_displayed_line
, s
->selected_line
);
4628 struct got_commit_object
*commit
;
4629 struct got_object_qid
*pid
;
4630 struct got_object_id
*blob_id
= NULL
;
4632 err
= got_object_open_as_commit(&commit
,
4637 got_object_commit_get_parent_ids(commit
));
4639 got_object_commit_close(commit
);
4642 /* Check if path history ends here. */
4643 err
= got_object_id_by_path(&blob_id
, s
->repo
,
4646 if (err
->code
== GOT_ERR_NO_TREE_ENTRY
)
4648 got_object_commit_close(commit
);
4651 err
= got_object_get_type(&obj_type
, s
->repo
,
4654 /* Can't blame non-blob type objects. */
4655 if (obj_type
!= GOT_OBJ_TYPE_BLOB
) {
4656 got_object_commit_close(commit
);
4659 err
= got_object_qid_alloc(&s
->blamed_commit
,
4661 got_object_commit_close(commit
);
4663 if (got_object_id_cmp(id
,
4664 s
->blamed_commit
->id
) == 0)
4666 err
= got_object_qid_alloc(&s
->blamed_commit
,
4672 thread_err
= stop_blame(&s
->blame
);
4676 STAILQ_INSERT_HEAD(&s
->blamed_commits
,
4677 s
->blamed_commit
, entry
);
4678 err
= run_blame(view
);
4684 struct got_object_qid
*first
;
4685 first
= STAILQ_FIRST(&s
->blamed_commits
);
4686 if (!got_object_id_cmp(first
->id
, s
->commit_id
))
4689 thread_err
= stop_blame(&s
->blame
);
4693 STAILQ_REMOVE_HEAD(&s
->blamed_commits
, entry
);
4694 got_object_qid_free(s
->blamed_commit
);
4696 STAILQ_FIRST(&s
->blamed_commits
);
4697 err
= run_blame(view
);
4704 struct got_object_id
*id
= NULL
;
4705 struct got_object_qid
*pid
;
4706 struct got_commit_object
*commit
= NULL
;
4707 id
= get_selected_commit_id(s
->blame
.lines
, s
->blame
.nlines
,
4708 s
->first_displayed_line
, s
->selected_line
);
4711 err
= got_object_open_as_commit(&commit
, s
->repo
, id
);
4715 got_object_commit_get_parent_ids(commit
));
4716 if (view_is_parent_view(view
))
4717 begin_x
= view_split_begin_x(view
->begin_x
);
4718 diff_view
= view_open(0, 0, 0, begin_x
, TOG_VIEW_DIFF
);
4719 if (diff_view
== NULL
) {
4720 got_object_commit_close(commit
);
4721 err
= got_error_from_errno("view_open");
4724 err
= open_diff_view(diff_view
, pid
? pid
->id
: NULL
,
4725 id
, NULL
, NULL
, 3, 0, 0, NULL
, s
->repo
);
4726 got_object_commit_close(commit
);
4728 view_close(diff_view
);
4732 diff_view
->focussed
= 1;
4733 if (view_is_parent_view(view
)) {
4734 err
= view_close_child(view
);
4737 view_set_child(view
, diff_view
);
4738 view
->focus_child
= 1;
4740 *new_view
= diff_view
;
4748 if (s
->last_displayed_line
>= s
->blame
.nlines
&&
4749 s
->selected_line
>= MIN(s
->blame
.nlines
,
4750 view
->nlines
- 2)) {
4753 if (s
->last_displayed_line
>= s
->blame
.nlines
&&
4754 s
->selected_line
< view
->nlines
- 2) {
4755 s
->selected_line
= MIN(s
->blame
.nlines
,
4759 if (s
->last_displayed_line
+ view
->nlines
- 2
4761 s
->first_displayed_line
+=
4764 s
->first_displayed_line
=
4769 if (s
->selected_line
> view
->nlines
- 2) {
4770 s
->selected_line
= MIN(s
->blame
.nlines
,
4777 return thread_err
? thread_err
: err
;
4780 static const struct got_error
*
4781 cmd_blame(int argc
, char *argv
[])
4783 const struct got_error
*error
;
4784 struct got_repository
*repo
= NULL
;
4785 struct got_worktree
*worktree
= NULL
;
4786 char *cwd
= NULL
, *repo_path
= NULL
, *in_repo_path
= NULL
;
4787 char *link_target
= NULL
;
4788 struct got_object_id
*commit_id
= NULL
;
4789 char *commit_id_str
= NULL
;
4791 struct tog_view
*view
;
4793 while ((ch
= getopt(argc
, argv
, "c:r:")) != -1) {
4796 commit_id_str
= optarg
;
4799 repo_path
= realpath(optarg
, NULL
);
4800 if (repo_path
== NULL
)
4801 return got_error_from_errno2("realpath",
4816 if (repo_path
== NULL
) {
4817 cwd
= getcwd(NULL
, 0);
4819 return got_error_from_errno("getcwd");
4820 error
= got_worktree_open(&worktree
, cwd
);
4821 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
4825 strdup(got_worktree_get_repo_path(worktree
));
4827 repo_path
= strdup(cwd
);
4828 if (repo_path
== NULL
) {
4829 error
= got_error_from_errno("strdup");
4834 error
= got_repo_open(&repo
, repo_path
, NULL
);
4838 error
= get_in_repo_path_from_argv0(&in_repo_path
, argc
, argv
, repo
,
4845 error
= apply_unveil(got_repo_get_path(repo
), NULL
);
4849 error
= tog_load_refs(repo
);
4853 if (commit_id_str
== NULL
) {
4854 struct got_reference
*head_ref
;
4855 error
= got_ref_open(&head_ref
, repo
, worktree
?
4856 got_worktree_get_head_ref_name(worktree
) : GOT_REF_HEAD
, 0);
4859 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
4860 got_ref_close(head_ref
);
4862 error
= got_repo_match_object_id(&commit_id
, NULL
,
4863 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &tog_refs
, repo
);
4868 view
= view_open(0, 0, 0, 0, TOG_VIEW_BLAME
);
4870 error
= got_error_from_errno("view_open");
4874 error
= got_object_resolve_symlinks(&link_target
, in_repo_path
,
4879 error
= open_blame_view(view
, link_target
? link_target
: in_repo_path
,
4884 /* Release work tree lock. */
4885 got_worktree_close(worktree
);
4888 error
= view_loop(view
);
4896 got_worktree_close(worktree
);
4898 const struct got_error
*close_err
= got_repo_close(repo
);
4906 static const struct got_error
*
4907 draw_tree_entries(struct tog_view
*view
, const char *parent_path
)
4909 struct tog_tree_view_state
*s
= &view
->state
.tree
;
4910 const struct got_error
*err
= NULL
;
4911 struct got_tree_entry
*te
;
4913 struct tog_color
*tc
;
4914 int width
, n
, i
, nentries
;
4915 int limit
= view
->nlines
;
4919 werase(view
->window
);
4924 err
= format_line(&wline
, &width
, s
->tree_label
, view
->ncols
, 0);
4927 if (view_needs_focus_indication(view
))
4928 wstandout(view
->window
);
4929 tc
= get_color(&s
->colors
, TOG_COLOR_COMMIT
);
4931 wattr_on(view
->window
,
4932 COLOR_PAIR(tc
->colorpair
), NULL
);
4933 waddwstr(view
->window
, wline
);
4935 wattr_off(view
->window
,
4936 COLOR_PAIR(tc
->colorpair
), NULL
);
4937 if (view_needs_focus_indication(view
))
4938 wstandend(view
->window
);
4941 if (width
< view
->ncols
- 1)
4942 waddch(view
->window
, '\n');
4945 err
= format_line(&wline
, &width
, parent_path
, view
->ncols
, 0);
4948 waddwstr(view
->window
, wline
);
4951 if (width
< view
->ncols
- 1)
4952 waddch(view
->window
, '\n');
4955 waddch(view
->window
, '\n');
4959 if (s
->first_displayed_entry
== NULL
) {
4960 te
= got_object_tree_get_first_entry(s
->tree
);
4961 if (s
->selected
== 0) {
4963 wstandout(view
->window
);
4964 s
->selected_entry
= NULL
;
4966 waddstr(view
->window
, " ..\n"); /* parent directory */
4967 if (s
->selected
== 0 && view
->focussed
)
4968 wstandend(view
->window
);
4975 te
= s
->first_displayed_entry
;
4978 nentries
= got_object_tree_get_nentries(s
->tree
);
4979 for (i
= got_tree_entry_get_index(te
); i
< nentries
; i
++) {
4980 char *line
= NULL
, *id_str
= NULL
, *link_target
= NULL
;
4981 const char *modestr
= "";
4984 te
= got_object_tree_get_entry(s
->tree
, i
);
4985 mode
= got_tree_entry_get_mode(te
);
4988 err
= got_object_id_str(&id_str
,
4989 got_tree_entry_get_id(te
));
4991 return got_error_from_errno(
4992 "got_object_id_str");
4994 if (got_object_tree_entry_is_submodule(te
))
4996 else if (S_ISLNK(mode
)) {
4999 err
= got_tree_entry_get_symlink_target(&link_target
,
5005 for (i
= 0; i
< strlen(link_target
); i
++) {
5006 if (!isprint((unsigned char)link_target
[i
]))
5007 link_target
[i
] = '?';
5011 else if (S_ISDIR(mode
))
5013 else if (mode
& S_IXUSR
)
5015 if (asprintf(&line
, "%s %s%s%s%s", id_str
? id_str
: "",
5016 got_tree_entry_get_name(te
), modestr
,
5017 link_target
? " -> ": "",
5018 link_target
? link_target
: "") == -1) {
5021 return got_error_from_errno("asprintf");
5025 err
= format_line(&wline
, &width
, line
, view
->ncols
, 0);
5030 if (n
== s
->selected
) {
5032 wstandout(view
->window
);
5033 s
->selected_entry
= te
;
5035 tc
= match_color(&s
->colors
, line
);
5037 wattr_on(view
->window
,
5038 COLOR_PAIR(tc
->colorpair
), NULL
);
5039 waddwstr(view
->window
, wline
);
5041 wattr_off(view
->window
,
5042 COLOR_PAIR(tc
->colorpair
), NULL
);
5043 if (width
< view
->ncols
- 1)
5044 waddch(view
->window
, '\n');
5045 if (n
== s
->selected
&& view
->focussed
)
5046 wstandend(view
->window
);
5052 s
->last_displayed_entry
= te
;
5061 tree_scroll_up(struct tog_tree_view_state
*s
, int maxscroll
)
5063 struct got_tree_entry
*te
;
5064 int isroot
= s
->tree
== s
->root
;
5067 if (s
->first_displayed_entry
== NULL
)
5070 te
= got_tree_entry_get_prev(s
->tree
, s
->first_displayed_entry
);
5071 while (i
++ < maxscroll
) {
5074 s
->first_displayed_entry
= NULL
;
5077 s
->first_displayed_entry
= te
;
5078 te
= got_tree_entry_get_prev(s
->tree
, te
);
5083 tree_scroll_down(struct tog_tree_view_state
*s
, int maxscroll
)
5085 struct got_tree_entry
*next
, *last
;
5088 if (s
->first_displayed_entry
)
5089 next
= got_tree_entry_get_next(s
->tree
,
5090 s
->first_displayed_entry
);
5092 next
= got_object_tree_get_first_entry(s
->tree
);
5094 last
= s
->last_displayed_entry
;
5095 while (next
&& last
&& n
++ < maxscroll
) {
5096 last
= got_tree_entry_get_next(s
->tree
, last
);
5098 s
->first_displayed_entry
= next
;
5099 next
= got_tree_entry_get_next(s
->tree
, next
);
5104 static const struct got_error
*
5105 tree_entry_path(char **path
, struct tog_parent_trees
*parents
,
5106 struct got_tree_entry
*te
)
5108 const struct got_error
*err
= NULL
;
5109 struct tog_parent_tree
*pt
;
5110 size_t len
= 2; /* for leading slash and NUL */
5112 TAILQ_FOREACH(pt
, parents
, entry
)
5113 len
+= strlen(got_tree_entry_get_name(pt
->selected_entry
))
5116 len
+= strlen(got_tree_entry_get_name(te
));
5118 *path
= calloc(1, len
);
5120 return got_error_from_errno("calloc");
5123 pt
= TAILQ_LAST(parents
, tog_parent_trees
);
5125 const char *name
= got_tree_entry_get_name(pt
->selected_entry
);
5126 if (strlcat(*path
, name
, len
) >= len
) {
5127 err
= got_error(GOT_ERR_NO_SPACE
);
5130 if (strlcat(*path
, "/", len
) >= len
) {
5131 err
= got_error(GOT_ERR_NO_SPACE
);
5134 pt
= TAILQ_PREV(pt
, tog_parent_trees
, entry
);
5137 if (strlcat(*path
, got_tree_entry_get_name(te
), len
) >= len
) {
5138 err
= got_error(GOT_ERR_NO_SPACE
);
5150 static const struct got_error
*
5151 blame_tree_entry(struct tog_view
**new_view
, int begin_x
,
5152 struct got_tree_entry
*te
, struct tog_parent_trees
*parents
,
5153 struct got_object_id
*commit_id
, struct got_repository
*repo
)
5155 const struct got_error
*err
= NULL
;
5157 struct tog_view
*blame_view
;
5161 err
= tree_entry_path(&path
, parents
, te
);
5165 blame_view
= view_open(0, 0, 0, begin_x
, TOG_VIEW_BLAME
);
5166 if (blame_view
== NULL
) {
5167 err
= got_error_from_errno("view_open");
5171 err
= open_blame_view(blame_view
, path
, commit_id
, repo
);
5173 if (err
->code
== GOT_ERR_CANCELLED
)
5175 view_close(blame_view
);
5177 *new_view
= blame_view
;
5183 static const struct got_error
*
5184 log_selected_tree_entry(struct tog_view
**new_view
, int begin_x
,
5185 struct tog_tree_view_state
*s
)
5187 struct tog_view
*log_view
;
5188 const struct got_error
*err
= NULL
;
5193 log_view
= view_open(0, 0, 0, begin_x
, TOG_VIEW_LOG
);
5194 if (log_view
== NULL
)
5195 return got_error_from_errno("view_open");
5197 err
= tree_entry_path(&path
, &s
->parents
, s
->selected_entry
);
5201 err
= open_log_view(log_view
, s
->commit_id
, s
->repo
, s
->head_ref_name
,
5204 view_close(log_view
);
5206 *new_view
= log_view
;
5211 static const struct got_error
*
5212 open_tree_view(struct tog_view
*view
, struct got_object_id
*commit_id
,
5213 const char *head_ref_name
, struct got_repository
*repo
)
5215 const struct got_error
*err
= NULL
;
5216 char *commit_id_str
= NULL
;
5217 struct tog_tree_view_state
*s
= &view
->state
.tree
;
5218 struct got_commit_object
*commit
= NULL
;
5220 TAILQ_INIT(&s
->parents
);
5221 STAILQ_INIT(&s
->colors
);
5223 s
->commit_id
= got_object_id_dup(commit_id
);
5224 if (s
->commit_id
== NULL
)
5225 return got_error_from_errno("got_object_id_dup");
5227 err
= got_object_open_as_commit(&commit
, repo
, commit_id
);
5232 * The root is opened here and will be closed when the view is closed.
5233 * Any visited subtrees and their path-wise parents are opened and
5236 err
= got_object_open_as_tree(&s
->root
, repo
,
5237 got_object_commit_get_tree_id(commit
));
5242 err
= got_object_id_str(&commit_id_str
, commit_id
);
5246 if (asprintf(&s
->tree_label
, "commit %s", commit_id_str
) == -1) {
5247 err
= got_error_from_errno("asprintf");
5251 s
->first_displayed_entry
= got_object_tree_get_entry(s
->tree
, 0);
5252 s
->selected_entry
= got_object_tree_get_entry(s
->tree
, 0);
5253 if (head_ref_name
) {
5254 s
->head_ref_name
= strdup(head_ref_name
);
5255 if (s
->head_ref_name
== NULL
) {
5256 err
= got_error_from_errno("strdup");
5262 if (has_colors() && getenv("TOG_COLORS") != NULL
) {
5263 err
= add_color(&s
->colors
, "\\$$",
5264 TOG_COLOR_TREE_SUBMODULE
,
5265 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5268 err
= add_color(&s
->colors
, "@$", TOG_COLOR_TREE_SYMLINK
,
5269 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5272 err
= add_color(&s
->colors
, "/$",
5273 TOG_COLOR_TREE_DIRECTORY
,
5274 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5278 err
= add_color(&s
->colors
, "\\*$",
5279 TOG_COLOR_TREE_EXECUTABLE
,
5280 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5284 err
= add_color(&s
->colors
, "^$", TOG_COLOR_COMMIT
,
5285 get_color_value("TOG_COLOR_COMMIT"));
5290 view
->show
= show_tree_view
;
5291 view
->input
= input_tree_view
;
5292 view
->close
= close_tree_view
;
5293 view
->search_start
= search_start_tree_view
;
5294 view
->search_next
= search_next_tree_view
;
5296 free(commit_id_str
);
5298 got_object_commit_close(commit
);
5300 close_tree_view(view
);
5304 static const struct got_error
*
5305 close_tree_view(struct tog_view
*view
)
5307 struct tog_tree_view_state
*s
= &view
->state
.tree
;
5309 free_colors(&s
->colors
);
5310 free(s
->tree_label
);
5311 s
->tree_label
= NULL
;
5313 s
->commit_id
= NULL
;
5314 free(s
->head_ref_name
);
5315 s
->head_ref_name
= NULL
;
5316 while (!TAILQ_EMPTY(&s
->parents
)) {
5317 struct tog_parent_tree
*parent
;
5318 parent
= TAILQ_FIRST(&s
->parents
);
5319 TAILQ_REMOVE(&s
->parents
, parent
, entry
);
5320 if (parent
->tree
!= s
->root
)
5321 got_object_tree_close(parent
->tree
);
5325 if (s
->tree
!= NULL
&& s
->tree
!= s
->root
)
5326 got_object_tree_close(s
->tree
);
5328 got_object_tree_close(s
->root
);
5332 static const struct got_error
*
5333 search_start_tree_view(struct tog_view
*view
)
5335 struct tog_tree_view_state
*s
= &view
->state
.tree
;
5337 s
->matched_entry
= NULL
;
5342 match_tree_entry(struct got_tree_entry
*te
, regex_t
*regex
)
5344 regmatch_t regmatch
;
5346 return regexec(regex
, got_tree_entry_get_name(te
), 1, ®match
,
5350 static const struct got_error
*
5351 search_next_tree_view(struct tog_view
*view
)
5353 struct tog_tree_view_state
*s
= &view
->state
.tree
;
5354 struct got_tree_entry
*te
= NULL
;
5356 if (!view
->searching
) {
5357 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
5361 if (s
->matched_entry
) {
5362 if (view
->searching
== TOG_SEARCH_FORWARD
) {
5363 if (s
->selected_entry
)
5364 te
= got_tree_entry_get_next(s
->tree
,
5367 te
= got_object_tree_get_first_entry(s
->tree
);
5369 if (s
->selected_entry
== NULL
)
5370 te
= got_object_tree_get_last_entry(s
->tree
);
5372 te
= got_tree_entry_get_prev(s
->tree
,
5376 if (view
->searching
== TOG_SEARCH_FORWARD
)
5377 te
= got_object_tree_get_first_entry(s
->tree
);
5379 te
= got_object_tree_get_last_entry(s
->tree
);
5384 if (s
->matched_entry
== NULL
) {
5385 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
5388 if (view
->searching
== TOG_SEARCH_FORWARD
)
5389 te
= got_object_tree_get_first_entry(s
->tree
);
5391 te
= got_object_tree_get_last_entry(s
->tree
);
5394 if (match_tree_entry(te
, &view
->regex
)) {
5395 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
5396 s
->matched_entry
= te
;
5400 if (view
->searching
== TOG_SEARCH_FORWARD
)
5401 te
= got_tree_entry_get_next(s
->tree
, te
);
5403 te
= got_tree_entry_get_prev(s
->tree
, te
);
5406 if (s
->matched_entry
) {
5407 s
->first_displayed_entry
= s
->matched_entry
;
5414 static const struct got_error
*
5415 show_tree_view(struct tog_view
*view
)
5417 const struct got_error
*err
= NULL
;
5418 struct tog_tree_view_state
*s
= &view
->state
.tree
;
5421 err
= tree_entry_path(&parent_path
, &s
->parents
, NULL
);
5425 err
= draw_tree_entries(view
, parent_path
);
5432 static const struct got_error
*
5433 input_tree_view(struct tog_view
**new_view
, struct tog_view
*view
, int ch
)
5435 const struct got_error
*err
= NULL
;
5436 struct tog_tree_view_state
*s
= &view
->state
.tree
;
5437 struct tog_view
*log_view
, *ref_view
;
5438 struct got_tree_entry
*te
;
5443 s
->show_ids
= !s
->show_ids
;
5446 if (!s
->selected_entry
)
5448 if (view_is_parent_view(view
))
5449 begin_x
= view_split_begin_x(view
->begin_x
);
5450 err
= log_selected_tree_entry(&log_view
, begin_x
, s
);
5452 log_view
->focussed
= 1;
5453 if (view_is_parent_view(view
)) {
5454 err
= view_close_child(view
);
5457 view_set_child(view
, log_view
);
5458 view
->focus_child
= 1;
5460 *new_view
= log_view
;
5463 if (view_is_parent_view(view
))
5464 begin_x
= view_split_begin_x(view
->begin_x
);
5465 ref_view
= view_open(view
->nlines
, view
->ncols
,
5466 view
->begin_y
, begin_x
, TOG_VIEW_REF
);
5467 if (ref_view
== NULL
)
5468 return got_error_from_errno("view_open");
5469 err
= open_ref_view(ref_view
, s
->repo
);
5471 view_close(ref_view
);
5475 ref_view
->focussed
= 1;
5476 if (view_is_parent_view(view
)) {
5477 err
= view_close_child(view
);
5480 view_set_child(view
, ref_view
);
5481 view
->focus_child
= 1;
5483 *new_view
= ref_view
;
5488 if (s
->tree
== s
->root
)
5489 s
->first_displayed_entry
=
5490 got_object_tree_get_first_entry(s
->tree
);
5492 s
->first_displayed_entry
= NULL
;
5497 te
= got_object_tree_get_last_entry(s
->tree
);
5498 for (n
= 0; n
< view
->nlines
- 3; n
++) {
5500 if(s
->tree
!= s
->root
) {
5501 s
->first_displayed_entry
= NULL
;
5506 s
->first_displayed_entry
= te
;
5507 te
= got_tree_entry_get_prev(s
->tree
, te
);
5510 s
->selected
= n
- 1;
5514 if (s
->selected
> 0) {
5518 tree_scroll_up(s
, 1);
5522 if (s
->tree
== s
->root
) {
5523 if (got_object_tree_get_first_entry(s
->tree
) ==
5524 s
->first_displayed_entry
)
5527 if (s
->first_displayed_entry
== NULL
)
5530 tree_scroll_up(s
, MAX(0, view
->nlines
- 3));
5534 if (s
->selected
< s
->ndisplayed
- 1) {
5538 if (got_tree_entry_get_next(s
->tree
, s
->last_displayed_entry
)
5540 /* can't scroll any further */
5542 tree_scroll_down(s
, 1);
5546 if (got_tree_entry_get_next(s
->tree
, s
->last_displayed_entry
)
5548 /* can't scroll any further; move cursor down */
5549 if (s
->selected
< s
->ndisplayed
- 1)
5550 s
->selected
= s
->ndisplayed
- 1;
5553 tree_scroll_down(s
, view
->nlines
- 3);
5558 if (s
->selected_entry
== NULL
|| ch
== KEY_BACKSPACE
) {
5559 struct tog_parent_tree
*parent
;
5560 /* user selected '..' */
5561 if (s
->tree
== s
->root
)
5563 parent
= TAILQ_FIRST(&s
->parents
);
5564 TAILQ_REMOVE(&s
->parents
, parent
,
5566 got_object_tree_close(s
->tree
);
5567 s
->tree
= parent
->tree
;
5568 s
->first_displayed_entry
=
5569 parent
->first_displayed_entry
;
5571 parent
->selected_entry
;
5572 s
->selected
= parent
->selected
;
5574 } else if (S_ISDIR(got_tree_entry_get_mode(
5575 s
->selected_entry
))) {
5576 struct got_tree_object
*subtree
;
5577 err
= got_object_open_as_tree(&subtree
, s
->repo
,
5578 got_tree_entry_get_id(s
->selected_entry
));
5581 err
= tree_view_visit_subtree(s
, subtree
);
5583 got_object_tree_close(subtree
);
5586 } else if (S_ISREG(got_tree_entry_get_mode(
5587 s
->selected_entry
))) {
5588 struct tog_view
*blame_view
;
5589 int begin_x
= view_is_parent_view(view
) ?
5590 view_split_begin_x(view
->begin_x
) : 0;
5592 err
= blame_tree_entry(&blame_view
, begin_x
,
5593 s
->selected_entry
, &s
->parents
,
5594 s
->commit_id
, s
->repo
);
5598 blame_view
->focussed
= 1;
5599 if (view_is_parent_view(view
)) {
5600 err
= view_close_child(view
);
5603 view_set_child(view
, blame_view
);
5604 view
->focus_child
= 1;
5606 *new_view
= blame_view
;
5610 if (view
->nlines
>= 4 && s
->selected
>= view
->nlines
- 3)
5611 s
->selected
= view
->nlines
- 4;
5624 fprintf(stderr
, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5629 static const struct got_error
*
5630 cmd_tree(int argc
, char *argv
[])
5632 const struct got_error
*error
;
5633 struct got_repository
*repo
= NULL
;
5634 struct got_worktree
*worktree
= NULL
;
5635 char *cwd
= NULL
, *repo_path
= NULL
, *in_repo_path
= NULL
;
5636 struct got_object_id
*commit_id
= NULL
;
5637 const char *commit_id_arg
= NULL
;
5639 struct got_reference
*ref
= NULL
;
5640 const char *head_ref_name
= NULL
;
5642 struct tog_view
*view
;
5644 while ((ch
= getopt(argc
, argv
, "c:r:")) != -1) {
5647 commit_id_arg
= optarg
;
5650 repo_path
= realpath(optarg
, NULL
);
5651 if (repo_path
== NULL
)
5652 return got_error_from_errno2("realpath",
5667 if (repo_path
== NULL
) {
5668 cwd
= getcwd(NULL
, 0);
5670 return got_error_from_errno("getcwd");
5671 error
= got_worktree_open(&worktree
, cwd
);
5672 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
5676 strdup(got_worktree_get_repo_path(worktree
));
5678 repo_path
= strdup(cwd
);
5679 if (repo_path
== NULL
) {
5680 error
= got_error_from_errno("strdup");
5685 error
= got_repo_open(&repo
, repo_path
, NULL
);
5689 error
= get_in_repo_path_from_argv0(&in_repo_path
, argc
, argv
,
5696 error
= apply_unveil(got_repo_get_path(repo
), NULL
);
5700 error
= tog_load_refs(repo
);
5704 if (commit_id_arg
== NULL
) {
5705 error
= got_repo_match_object_id(&commit_id
, &label
,
5706 worktree
? got_worktree_get_head_ref_name(worktree
) :
5707 GOT_REF_HEAD
, GOT_OBJ_TYPE_COMMIT
, &tog_refs
, repo
);
5710 head_ref_name
= label
;
5712 error
= got_ref_open(&ref
, repo
, commit_id_arg
, 0);
5714 head_ref_name
= got_ref_get_name(ref
);
5715 else if (error
->code
!= GOT_ERR_NOT_REF
)
5717 error
= got_repo_match_object_id(&commit_id
, NULL
,
5718 commit_id_arg
, GOT_OBJ_TYPE_COMMIT
, &tog_refs
, repo
);
5723 view
= view_open(0, 0, 0, 0, TOG_VIEW_TREE
);
5725 error
= got_error_from_errno("view_open");
5728 error
= open_tree_view(view
, commit_id
, head_ref_name
, repo
);
5731 if (!got_path_is_root_dir(in_repo_path
)) {
5732 error
= tree_view_walk_path(&view
->state
.tree
, commit_id
,
5739 /* Release work tree lock. */
5740 got_worktree_close(worktree
);
5743 error
= view_loop(view
);
5752 const struct got_error
*close_err
= got_repo_close(repo
);
5760 static const struct got_error
*
5761 ref_view_load_refs(struct tog_ref_view_state
*s
)
5763 struct got_reflist_entry
*sre
;
5764 struct tog_reflist_entry
*re
;
5767 TAILQ_FOREACH(sre
, &tog_refs
, entry
) {
5768 if (strncmp(got_ref_get_name(sre
->ref
), "refs/got/", 9) == 0)
5771 re
= malloc(sizeof(*re
));
5773 return got_error_from_errno("malloc");
5775 re
->ref
= got_ref_dup(sre
->ref
);
5776 if (re
->ref
== NULL
)
5777 return got_error_from_errno("got_ref_dup");
5778 re
->idx
= s
->nrefs
++;
5779 TAILQ_INSERT_TAIL(&s
->refs
, re
, entry
);
5782 s
->first_displayed_entry
= TAILQ_FIRST(&s
->refs
);
5787 ref_view_free_refs(struct tog_ref_view_state
*s
)
5789 struct tog_reflist_entry
*re
;
5791 while (!TAILQ_EMPTY(&s
->refs
)) {
5792 re
= TAILQ_FIRST(&s
->refs
);
5793 TAILQ_REMOVE(&s
->refs
, re
, entry
);
5794 got_ref_close(re
->ref
);
5799 static const struct got_error
*
5800 open_ref_view(struct tog_view
*view
, struct got_repository
*repo
)
5802 const struct got_error
*err
= NULL
;
5803 struct tog_ref_view_state
*s
= &view
->state
.ref
;
5805 s
->selected_entry
= 0;
5808 TAILQ_INIT(&s
->refs
);
5809 STAILQ_INIT(&s
->colors
);
5811 err
= ref_view_load_refs(s
);
5815 if (has_colors() && getenv("TOG_COLORS") != NULL
) {
5816 err
= add_color(&s
->colors
, "^refs/heads/",
5817 TOG_COLOR_REFS_HEADS
,
5818 get_color_value("TOG_COLOR_REFS_HEADS"));
5822 err
= add_color(&s
->colors
, "^refs/tags/",
5823 TOG_COLOR_REFS_TAGS
,
5824 get_color_value("TOG_COLOR_REFS_TAGS"));
5828 err
= add_color(&s
->colors
, "^refs/remotes/",
5829 TOG_COLOR_REFS_REMOTES
,
5830 get_color_value("TOG_COLOR_REFS_REMOTES"));
5835 view
->show
= show_ref_view
;
5836 view
->input
= input_ref_view
;
5837 view
->close
= close_ref_view
;
5838 view
->search_start
= search_start_ref_view
;
5839 view
->search_next
= search_next_ref_view
;
5842 free_colors(&s
->colors
);
5846 static const struct got_error
*
5847 close_ref_view(struct tog_view
*view
)
5849 struct tog_ref_view_state
*s
= &view
->state
.ref
;
5851 ref_view_free_refs(s
);
5852 free_colors(&s
->colors
);
5857 static const struct got_error
*
5858 resolve_reflist_entry(struct got_object_id
**commit_id
,
5859 struct tog_reflist_entry
*re
, struct got_repository
*repo
)
5861 const struct got_error
*err
= NULL
;
5862 struct got_object_id
*obj_id
;
5863 struct got_tag_object
*tag
= NULL
;
5868 err
= got_ref_resolve(&obj_id
, repo
, re
->ref
);
5872 err
= got_object_get_type(&obj_type
, repo
, obj_id
);
5877 case GOT_OBJ_TYPE_COMMIT
:
5878 *commit_id
= obj_id
;
5880 case GOT_OBJ_TYPE_TAG
:
5881 err
= got_object_open_as_tag(&tag
, repo
, obj_id
);
5885 err
= got_object_get_type(&obj_type
, repo
,
5886 got_object_tag_get_object_id(tag
));
5889 if (obj_type
!= GOT_OBJ_TYPE_COMMIT
) {
5890 err
= got_error(GOT_ERR_OBJ_TYPE
);
5893 *commit_id
= got_object_id_dup(
5894 got_object_tag_get_object_id(tag
));
5895 if (*commit_id
== NULL
) {
5896 err
= got_error_from_errno("got_object_id_dup");
5901 err
= got_error(GOT_ERR_OBJ_TYPE
);
5907 got_object_tag_close(tag
);
5915 static const struct got_error
*
5916 log_ref_entry(struct tog_view
**new_view
, int begin_x
,
5917 struct tog_reflist_entry
*re
, struct got_repository
*repo
)
5919 struct tog_view
*log_view
;
5920 const struct got_error
*err
= NULL
;
5921 struct got_object_id
*commit_id
= NULL
;
5925 err
= resolve_reflist_entry(&commit_id
, re
, repo
);
5927 if (err
->code
!= GOT_ERR_OBJ_TYPE
)
5933 log_view
= view_open(0, 0, 0, begin_x
, TOG_VIEW_LOG
);
5934 if (log_view
== NULL
) {
5935 err
= got_error_from_errno("view_open");
5939 err
= open_log_view(log_view
, commit_id
, repo
,
5940 got_ref_get_name(re
->ref
), "", 0);
5943 view_close(log_view
);
5945 *new_view
= log_view
;
5951 ref_scroll_up(struct tog_ref_view_state
*s
, int maxscroll
)
5953 struct tog_reflist_entry
*re
;
5956 if (s
->first_displayed_entry
== TAILQ_FIRST(&s
->refs
))
5959 re
= TAILQ_PREV(s
->first_displayed_entry
, tog_reflist_head
, entry
);
5960 while (i
++ < maxscroll
) {
5963 s
->first_displayed_entry
= re
;
5964 re
= TAILQ_PREV(re
, tog_reflist_head
, entry
);
5969 ref_scroll_down(struct tog_ref_view_state
*s
, int maxscroll
)
5971 struct tog_reflist_entry
*next
, *last
;
5974 if (s
->first_displayed_entry
)
5975 next
= TAILQ_NEXT(s
->first_displayed_entry
, entry
);
5977 next
= TAILQ_FIRST(&s
->refs
);
5979 last
= s
->last_displayed_entry
;
5980 while (next
&& last
&& n
++ < maxscroll
) {
5981 last
= TAILQ_NEXT(last
, entry
);
5983 s
->first_displayed_entry
= next
;
5984 next
= TAILQ_NEXT(next
, entry
);
5989 static const struct got_error
*
5990 search_start_ref_view(struct tog_view
*view
)
5992 struct tog_ref_view_state
*s
= &view
->state
.ref
;
5994 s
->matched_entry
= NULL
;
5999 match_reflist_entry(struct tog_reflist_entry
*re
, regex_t
*regex
)
6001 regmatch_t regmatch
;
6003 return regexec(regex
, got_ref_get_name(re
->ref
), 1, ®match
,
6007 static const struct got_error
*
6008 search_next_ref_view(struct tog_view
*view
)
6010 struct tog_ref_view_state
*s
= &view
->state
.ref
;
6011 struct tog_reflist_entry
*re
= NULL
;
6013 if (!view
->searching
) {
6014 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
6018 if (s
->matched_entry
) {
6019 if (view
->searching
== TOG_SEARCH_FORWARD
) {
6020 if (s
->selected_entry
)
6021 re
= TAILQ_NEXT(s
->selected_entry
, entry
);
6023 re
= TAILQ_PREV(s
->selected_entry
,
6024 tog_reflist_head
, entry
);
6026 if (s
->selected_entry
== NULL
)
6027 re
= TAILQ_LAST(&s
->refs
, tog_reflist_head
);
6029 re
= TAILQ_PREV(s
->selected_entry
,
6030 tog_reflist_head
, entry
);
6033 if (view
->searching
== TOG_SEARCH_FORWARD
)
6034 re
= TAILQ_FIRST(&s
->refs
);
6036 re
= TAILQ_LAST(&s
->refs
, tog_reflist_head
);
6041 if (s
->matched_entry
== NULL
) {
6042 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
6045 if (view
->searching
== TOG_SEARCH_FORWARD
)
6046 re
= TAILQ_FIRST(&s
->refs
);
6048 re
= TAILQ_LAST(&s
->refs
, tog_reflist_head
);
6051 if (match_reflist_entry(re
, &view
->regex
)) {
6052 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
6053 s
->matched_entry
= re
;
6057 if (view
->searching
== TOG_SEARCH_FORWARD
)
6058 re
= TAILQ_NEXT(re
, entry
);
6060 re
= TAILQ_PREV(re
, tog_reflist_head
, entry
);
6063 if (s
->matched_entry
) {
6064 s
->first_displayed_entry
= s
->matched_entry
;
6071 static const struct got_error
*
6072 show_ref_view(struct tog_view
*view
)
6074 const struct got_error
*err
= NULL
;
6075 struct tog_ref_view_state
*s
= &view
->state
.ref
;
6076 struct tog_reflist_entry
*re
;
6079 struct tog_color
*tc
;
6081 int limit
= view
->nlines
;
6083 werase(view
->window
);
6090 re
= s
->first_displayed_entry
;
6092 if (asprintf(&line
, "references [%d/%d]", re
->idx
+ s
->selected
+ 1,
6094 return got_error_from_errno("asprintf");
6096 err
= format_line(&wline
, &width
, line
, view
->ncols
, 0);
6101 if (view_needs_focus_indication(view
))
6102 wstandout(view
->window
);
6103 waddwstr(view
->window
, wline
);
6104 if (view_needs_focus_indication(view
))
6105 wstandend(view
->window
);
6110 if (width
< view
->ncols
- 1)
6111 waddch(view
->window
, '\n');
6116 while (re
&& limit
> 0) {
6119 if (got_ref_is_symbolic(re
->ref
)) {
6120 if (asprintf(&line
, "%s -> %s",
6121 got_ref_get_name(re
->ref
),
6122 got_ref_get_symref_target(re
->ref
)) == -1)
6123 return got_error_from_errno("asprintf");
6124 } else if (s
->show_ids
) {
6125 struct got_object_id
*id
;
6127 err
= got_ref_resolve(&id
, s
->repo
, re
->ref
);
6130 err
= got_object_id_str(&id_str
, id
);
6135 if (asprintf(&line
, "%s: %s",
6136 got_ref_get_name(re
->ref
), id_str
) == -1) {
6137 err
= got_error_from_errno("asprintf");
6145 line
= strdup(got_ref_get_name(re
->ref
));
6147 return got_error_from_errno("strdup");
6150 err
= format_line(&wline
, &width
, line
, view
->ncols
, 0);
6155 if (n
== s
->selected
) {
6157 wstandout(view
->window
);
6158 s
->selected_entry
= re
;
6160 tc
= match_color(&s
->colors
, got_ref_get_name(re
->ref
));
6162 wattr_on(view
->window
,
6163 COLOR_PAIR(tc
->colorpair
), NULL
);
6164 waddwstr(view
->window
, wline
);
6166 wattr_off(view
->window
,
6167 COLOR_PAIR(tc
->colorpair
), NULL
);
6168 if (width
< view
->ncols
- 1)
6169 waddch(view
->window
, '\n');
6170 if (n
== s
->selected
&& view
->focussed
)
6171 wstandend(view
->window
);
6177 s
->last_displayed_entry
= re
;
6180 re
= TAILQ_NEXT(re
, entry
);
6187 static const struct got_error
*
6188 browse_ref_tree(struct tog_view
**new_view
, int begin_x
,
6189 struct tog_reflist_entry
*re
, struct got_repository
*repo
)
6191 const struct got_error
*err
= NULL
;
6192 struct got_object_id
*commit_id
= NULL
;
6193 struct tog_view
*tree_view
;
6197 err
= resolve_reflist_entry(&commit_id
, re
, repo
);
6199 if (err
->code
!= GOT_ERR_OBJ_TYPE
)
6206 tree_view
= view_open(0, 0, 0, begin_x
, TOG_VIEW_TREE
);
6207 if (tree_view
== NULL
) {
6208 err
= got_error_from_errno("view_open");
6212 err
= open_tree_view(tree_view
, commit_id
,
6213 got_ref_get_name(re
->ref
), repo
);
6217 *new_view
= tree_view
;
6222 static const struct got_error
*
6223 input_ref_view(struct tog_view
**new_view
, struct tog_view
*view
, int ch
)
6225 const struct got_error
*err
= NULL
;
6226 struct tog_ref_view_state
*s
= &view
->state
.ref
;
6227 struct tog_view
*log_view
, *tree_view
;
6228 struct tog_reflist_entry
*re
;
6233 s
->show_ids
= !s
->show_ids
;
6237 if (!s
->selected_entry
)
6239 if (view_is_parent_view(view
))
6240 begin_x
= view_split_begin_x(view
->begin_x
);
6241 err
= log_ref_entry(&log_view
, begin_x
, s
->selected_entry
,
6244 log_view
->focussed
= 1;
6245 if (view_is_parent_view(view
)) {
6246 err
= view_close_child(view
);
6249 view_set_child(view
, log_view
);
6250 view
->focus_child
= 1;
6252 *new_view
= log_view
;
6255 if (!s
->selected_entry
)
6257 if (view_is_parent_view(view
))
6258 begin_x
= view_split_begin_x(view
->begin_x
);
6259 err
= browse_ref_tree(&tree_view
, begin_x
, s
->selected_entry
,
6261 if (err
|| tree_view
== NULL
)
6264 tree_view
->focussed
= 1;
6265 if (view_is_parent_view(view
)) {
6266 err
= view_close_child(view
);
6269 view_set_child(view
, tree_view
);
6270 view
->focus_child
= 1;
6272 *new_view
= tree_view
;
6277 s
->first_displayed_entry
= TAILQ_FIRST(&s
->refs
);
6282 re
= TAILQ_LAST(&s
->refs
, tog_reflist_head
);
6283 for (n
= 0; n
< view
->nlines
- 1; n
++) {
6286 s
->first_displayed_entry
= re
;
6287 re
= TAILQ_PREV(re
, tog_reflist_head
, entry
);
6290 s
->selected
= n
- 1;
6294 if (s
->selected
> 0) {
6298 ref_scroll_up(s
, 1);
6302 if (s
->first_displayed_entry
== TAILQ_FIRST(&s
->refs
))
6304 ref_scroll_up(s
, MAX(0, view
->nlines
- 1));
6308 if (s
->selected
< s
->ndisplayed
- 1) {
6312 if (TAILQ_NEXT(s
->last_displayed_entry
, entry
) == NULL
)
6313 /* can't scroll any further */
6315 ref_scroll_down(s
, 1);
6319 if (TAILQ_NEXT(s
->last_displayed_entry
, entry
) == NULL
) {
6320 /* can't scroll any further; move cursor down */
6321 if (s
->selected
< s
->ndisplayed
- 1)
6322 s
->selected
= s
->ndisplayed
- 1;
6325 ref_scroll_down(s
, view
->nlines
- 1);
6329 err
= tog_load_refs(s
->repo
);
6332 ref_view_free_refs(s
);
6333 err
= ref_view_load_refs(s
);
6336 if (view
->nlines
>= 2 && s
->selected
>= view
->nlines
- 1)
6337 s
->selected
= view
->nlines
- 2;
6350 fprintf(stderr
, "usage: %s ref [-r repository-path]\n",
6355 static const struct got_error
*
6356 cmd_ref(int argc
, char *argv
[])
6358 const struct got_error
*error
;
6359 struct got_repository
*repo
= NULL
;
6360 struct got_worktree
*worktree
= NULL
;
6361 char *cwd
= NULL
, *repo_path
= NULL
;
6363 struct tog_view
*view
;
6365 while ((ch
= getopt(argc
, argv
, "r:")) != -1) {
6368 repo_path
= realpath(optarg
, NULL
);
6369 if (repo_path
== NULL
)
6370 return got_error_from_errno2("realpath",
6385 if (repo_path
== NULL
) {
6386 cwd
= getcwd(NULL
, 0);
6388 return got_error_from_errno("getcwd");
6389 error
= got_worktree_open(&worktree
, cwd
);
6390 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
6394 strdup(got_worktree_get_repo_path(worktree
));
6396 repo_path
= strdup(cwd
);
6397 if (repo_path
== NULL
) {
6398 error
= got_error_from_errno("strdup");
6403 error
= got_repo_open(&repo
, repo_path
, NULL
);
6409 error
= apply_unveil(got_repo_get_path(repo
), NULL
);
6413 error
= tog_load_refs(repo
);
6417 view
= view_open(0, 0, 0, 0, TOG_VIEW_REF
);
6419 error
= got_error_from_errno("view_open");
6423 error
= open_ref_view(view
, repo
);
6428 /* Release work tree lock. */
6429 got_worktree_close(worktree
);
6432 error
= view_loop(view
);
6437 const struct got_error
*close_err
= got_repo_close(repo
);
6446 list_commands(FILE *fp
)
6450 fprintf(fp
, "commands:");
6451 for (i
= 0; i
< nitems(tog_commands
); i
++) {
6452 struct tog_cmd
*cmd
= &tog_commands
[i
];
6453 fprintf(fp
, " %s", cmd
->name
);
6459 usage(int hflag
, int status
)
6461 FILE *fp
= (status
== 0) ? stdout
: stderr
;
6463 fprintf(fp
, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6466 fprintf(fp
, "lazy usage: %s path\n", getprogname());
6473 make_argv(int argc
, ...)
6481 argv
= calloc(argc
, sizeof(char *));
6484 for (i
= 0; i
< argc
; i
++) {
6485 argv
[i
] = strdup(va_arg(ap
, char *));
6486 if (argv
[i
] == NULL
)
6495 * Try to convert 'tog path' into a 'tog log path' command.
6496 * The user could simply have mistyped the command rather than knowingly
6497 * provided a path. So check whether argv[0] can in fact be resolved
6498 * to a path in the HEAD commit and print a special error if not.
6499 * This hack is for mpi@ <3
6501 static const struct got_error
*
6502 tog_log_with_path(int argc
, char *argv
[])
6504 const struct got_error
*error
= NULL
, *close_err
;
6505 struct tog_cmd
*cmd
= NULL
;
6506 struct got_repository
*repo
= NULL
;
6507 struct got_worktree
*worktree
= NULL
;
6508 struct got_object_id
*commit_id
= NULL
, *id
= NULL
;
6509 char *cwd
= NULL
, *repo_path
= NULL
, *in_repo_path
= NULL
;
6510 char *commit_id_str
= NULL
, **cmd_argv
= NULL
;
6512 cwd
= getcwd(NULL
, 0);
6514 return got_error_from_errno("getcwd");
6516 error
= got_worktree_open(&worktree
, cwd
);
6517 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
6521 repo_path
= strdup(got_worktree_get_repo_path(worktree
));
6523 repo_path
= strdup(cwd
);
6524 if (repo_path
== NULL
) {
6525 error
= got_error_from_errno("strdup");
6529 error
= got_repo_open(&repo
, repo_path
, NULL
);
6533 error
= get_in_repo_path_from_argv0(&in_repo_path
, argc
, argv
,
6538 error
= tog_load_refs(repo
);
6541 error
= got_repo_match_object_id(&commit_id
, NULL
, worktree
?
6542 got_worktree_get_head_ref_name(worktree
) : GOT_REF_HEAD
,
6543 GOT_OBJ_TYPE_COMMIT
, &tog_refs
, repo
);
6548 got_worktree_close(worktree
);
6552 error
= got_object_id_by_path(&id
, repo
, commit_id
, in_repo_path
);
6554 if (error
->code
!= GOT_ERR_NO_TREE_ENTRY
)
6556 fprintf(stderr
, "%s: '%s' is no known command or path\n",
6557 getprogname(), argv
[0]);
6562 close_err
= got_repo_close(repo
);
6567 error
= got_object_id_str(&commit_id_str
, commit_id
);
6571 cmd
= &tog_commands
[0]; /* log */
6573 cmd_argv
= make_argv(argc
, cmd
->name
, "-c", commit_id_str
, argv
[0]);
6574 error
= cmd
->cmd_main(argc
, cmd_argv
);
6577 close_err
= got_repo_close(repo
);
6582 got_worktree_close(worktree
);
6584 free(commit_id_str
);
6591 for (i
= 0; i
< argc
; i
++)
6600 main(int argc
, char *argv
[])
6602 const struct got_error
*error
= NULL
;
6603 struct tog_cmd
*cmd
= NULL
;
6604 int ch
, hflag
= 0, Vflag
= 0;
6605 char **cmd_argv
= NULL
;
6606 static struct option longopts
[] = {
6607 { "version", no_argument
, NULL
, 'V' },
6611 setlocale(LC_CTYPE
, "");
6613 while ((ch
= getopt_long(argc
, argv
, "+hV", longopts
, NULL
)) != -1) {
6633 got_version_print_str();
6638 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6646 /* Build an argument vector which runs a default command. */
6647 cmd
= &tog_commands
[0];
6649 cmd_argv
= make_argv(argc
, cmd
->name
);
6653 /* Did the user specify a command? */
6654 for (i
= 0; i
< nitems(tog_commands
); i
++) {
6655 if (strncmp(tog_commands
[i
].name
, argv
[0],
6656 strlen(argv
[0])) == 0) {
6657 cmd
= &tog_commands
[i
];
6666 /* No command specified; try log with a path */
6667 error
= tog_log_with_path(argc
, argv
);
6672 error
= cmd
->cmd_main(argc
, cmd_argv
? cmd_argv
: argv
);
6679 for (i
= 0; i
< argc
; i
++)
6684 if (error
&& error
->code
!= GOT_ERR_CANCELLED
)
6685 fprintf(stderr
, "%s: %s\n", getprogname(), error
->msg
);