#!perl

our $DATE = '2015-01-22'; # DATE
our $VERSION = '0.44'; # VERSION

use 5.010001;
use strict;
use warnings;

use Perinci::CmdLine::Any -prefer_lite=>1;

Perinci::CmdLine::Any->new(
    url => '/Git/Bunch/',
    subcommands => {
        check  => { url => '/Git/Bunch/check_bunch'  },
        sync   => { url => '/Git/Bunch/sync_bunch'   },
        exec   => { url => '/Git/Bunch/exec_bunch'   },
    },
    log => 1,
)->run;

1;
# ABSTRACT: Manage gitbunch directory (directory which contain git repos)
# PODNAME: gitbunch

__END__

=pod

=encoding UTF-8

=head1 NAME

gitbunch - Manage gitbunch directory (directory which contain git repos)

=head1 VERSION

This document describes version 0.44 of gitbunch (from Perl distribution Git-Bunch), released on 2015-01-22.

=head1 DESCRIPTION

See L<Git::Bunch> for more documentation.

=head1 SYNOPSIS

Usage:

 % gitbunch check [options] <source>
 % gitbunch exec [options] <source> <command> ...
 % gitbunch sync [options] <source> <target>

=head1 SUBCOMMANDS

=head2 B<check>

Check status of git repositories inside gitbunch directory.

Will perform a 'git status' for each git repositories inside the bunch and
report which repositories are clean/unclean.

Will die if can't chdir into bunch or git repository.


=head2 B<exec>

Execute a command for each repo in the bunch.

For each git repository in the bunch, will chdir to it and execute specified
command.


=head2 B<sync>

Synchronize bunch to another bunch.

For each git repository in the bunch, will perform a 'git pull/push' for each
branch. If repository in destination doesn't exist, it will be rsync-ed first
from source. When 'git pull' fails, will exit to let you fix the problem
manually.

For all other non-git repos, will simply synchronize by one-way rsync.

=head1 OPTIONS

C<*> marks required options.

=head2 Common options

=over

=item B<--config-path>=I<s>

{en_US Set path to configuration file}.

Can be specified multiple times.

=item B<--config-profile>=I<s>

{en_US Set configuration profile to use}.

=item B<--debug>

{en_US Set log level to debug}.

=item B<--format>=I<s>

{en_US Choose output format, e.g. json, text}.

=item B<--help>, B<-h>, B<-?>

{en_US Display this help message}.

=item B<--json>

{en_US Set output format to json}.

=item B<--log-level>=I<s>

{en_US Set log level}.

=item B<--naked-res>

{en_US When outputing as JSON, strip result envelope}.

{en_US 
By default, when outputing as JSON, the full enveloped result is returned, e.g.:

    [200,"OK",[1,2,3],{"func.extra"=>4}]

The reason is so you can get the status (1st element), status message (2nd
element) as well as result metadata/extra result (4th element) instead of just
the result (3rd element). However, sometimes you want just the result, e.g. when
you want to pipe the result for more post-processing. In this case you can use
`--naked-res` so you just get:

    [1,2,3]
}


=item B<--no-config>

{en_US Do not use any configuration file}.

=item B<--quiet>

{en_US Set log level to quiet}.

=item B<--subcommands>

{en_US List available subcommands}.

=item B<--trace>

{en_US Set log level to trace}.

=item B<--verbose>

{en_US Set log level to info}.

=item B<--version>, B<-v>

=back

=head2 Options for subcommand check

=over

=item B<--exclude-files>

{en_US Exclude files from processing}.

{en_US 
This only applies to `sync_bunch` operations. Operations like `check_bunch` and
`exec_bunch` already ignore these and only operate on git repos.
}


=item B<--exclude-non-git-dirs>

{en_US Exclude non-git dirs from processing}.

{en_US 
This only applies to and `sync_bunch` operations. Operations like `check_bunch`
and `exec_bunch` already ignore these and only operate on git repos.
}


=item B<--exclude-repos-json>=I<s>

{en_US Exclude some repos from processing} (JSON-encoded).

See C<--exclude-repos>.

=item B<--exclude-repos-pat>=I<s>

{en_US Specify regex pattern of repos to exclude}.

=item B<--exclude-repos>=I<s@>

{en_US Exclude some repos from processing}.

Can be specified multiple times.

=item B<--include-files>

{en_US Alias for --no-exclude-files}.

See C<--exclude-files>.

=item B<--include-non-git-dirs>

{en_US Alias for --no-exclude-non-git-dirs}.

See C<--exclude-non-git-dirs>.

=item B<--include-repos-json>=I<s>

{en_US Specific git repos to sync, if not specified all repos in the bunch will be processed} (JSON-encoded).

See C<--include-repos>.

=item B<--include-repos-pat>=I<s>

{en_US Specify regex pattern of repos to include}.

=item B<--include-repos>=I<s@>

{en_US Specific git repos to sync, if not specified all repos in the bunch will be processed}.

Can be specified multiple times.

=item B<--repo>=I<s>

{en_US Only process a single repo}.

=item B<--sort>=I<s>

{en_US Order entries in bunch}.

Default value:

 "-commit-timestamp"

Valid values:

 ["name", "-name", "mtime", "-mtime", "rand", "commit-timestamp", "-commit-timestamp"]

{en_US 
`commit-timestamp` (and `-commit-timestamp`) compares the timestamp of
`.git/commit-timestamp` file in each repo. Repos or dirs not having this file
will be processed later. You can touch these `.git/commit-timestamp` files in
your post-commit script, for example. This allows sorting recently committed
repos more cheaply (compared to doing `git log -1`).
}


=item B<--source>=I<s>*

{en_US Directory to check}.

=back

=head2 Options for subcommand exec

=over

=item B<--command>=I<s>*

{en_US Command to execute}.

=item B<--exclude-files>

{en_US Exclude files from processing}.

{en_US 
This only applies to `sync_bunch` operations. Operations like `check_bunch` and
`exec_bunch` already ignore these and only operate on git repos.
}


=item B<--exclude-non-git-dirs>

{en_US Exclude non-git dirs from processing}.

{en_US 
This only applies to and `sync_bunch` operations. Operations like `check_bunch`
and `exec_bunch` already ignore these and only operate on git repos.
}


=item B<--exclude-repos-json>=I<s>

{en_US Exclude some repos from processing} (JSON-encoded).

See C<--exclude-repos>.

=item B<--exclude-repos-pat>=I<s>

{en_US Specify regex pattern of repos to exclude}.

=item B<--exclude-repos>=I<s@>

{en_US Exclude some repos from processing}.

Can be specified multiple times.

=item B<--include-files>

{en_US Alias for --no-exclude-files}.

See C<--exclude-files>.

=item B<--include-non-git-dirs>

{en_US Alias for --no-exclude-non-git-dirs}.

See C<--exclude-non-git-dirs>.

=item B<--include-repos-json>=I<s>

{en_US Specific git repos to sync, if not specified all repos in the bunch will be processed} (JSON-encoded).

See C<--include-repos>.

=item B<--include-repos-pat>=I<s>

{en_US Specify regex pattern of repos to include}.

=item B<--include-repos>=I<s@>

{en_US Specific git repos to sync, if not specified all repos in the bunch will be processed}.

Can be specified multiple times.

=item B<--repo>=I<s>

{en_US Only process a single repo}.

=item B<--sort>=I<s>

{en_US Order entries in bunch}.

Default value:

 "-commit-timestamp"

Valid values:

 ["name", "-name", "mtime", "-mtime", "rand", "commit-timestamp", "-commit-timestamp"]

{en_US 
`commit-timestamp` (and `-commit-timestamp`) compares the timestamp of
`.git/commit-timestamp` file in each repo. Repos or dirs not having this file
will be processed later. You can touch these `.git/commit-timestamp` files in
your post-commit script, for example. This allows sorting recently committed
repos more cheaply (compared to doing `git log -1`).
}


=item B<--source>=I<s>*

{en_US Directory to check}.

=back

=head2 Options for subcommand sync

=over

=item B<--backup>

{en_US Whether doing backup to target}.

{en_US 
This setting lets you express that you want to perform synchronizing to a backup
target, and that you do not do work on the target. Thus, you do not care about
uncommitted or untracked files/dirs in the target repos (might happen if you
also do periodic copying of repos to backup using cp/rsync). When this setting
is turned on, the function will first do a `git clean -f -d` (to delete
untracked files/dirs) and then `git checkout .` (to discard all uncommitted
changes). This setting will also implicitly turn on `create_bare` setting
(unless that setting has been explicitly enabled/disabled).
}


=item B<--create-bare-target>, B<--use-bare>

{en_US Whether to create bare git repo when target does not exist}.

{en_US 
When target repo does not exist, gitbunch can either copy the source repo using
`rsync` (the default, if this setting is undefined), or it can create target
repo with `git init --bare` (if this setting is set to 1), or it can create
target repo with `git init` (if this setting is set to 0).

Bare git repositories contain only contents of the .git folder inside the
directory and no working copies of your source files.

Creating bare repos are apt for backup purposes since they are more
space-efficient.

Non-repos will still be copied/rsync-ed.
}


=item B<--delete-branch>

{en_US Whether to delete branches in dest repos not existing in source repos}.

=item B<--exclude-files>

{en_US Exclude files from processing}.

{en_US 
This only applies to `sync_bunch` operations. Operations like `check_bunch` and
`exec_bunch` already ignore these and only operate on git repos.
}


=item B<--exclude-non-git-dirs>

{en_US Exclude non-git dirs from processing}.

{en_US 
This only applies to and `sync_bunch` operations. Operations like `check_bunch`
and `exec_bunch` already ignore these and only operate on git repos.
}


=item B<--exclude-repos-json>=I<s>

{en_US Exclude some repos from processing} (JSON-encoded).

See C<--exclude-repos>.

=item B<--exclude-repos-pat>=I<s>

{en_US Specify regex pattern of repos to exclude}.

=item B<--exclude-repos>=I<s@>

{en_US Exclude some repos from processing}.

Can be specified multiple times.

=item B<--include-files>

{en_US Alias for --no-exclude-files}.

See C<--exclude-files>.

=item B<--include-non-git-dirs>

{en_US Alias for --no-exclude-non-git-dirs}.

See C<--exclude-non-git-dirs>.

=item B<--include-repos-json>=I<s>

{en_US Specific git repos to sync, if not specified all repos in the bunch will be processed} (JSON-encoded).

See C<--include-repos>.

=item B<--include-repos-pat>=I<s>

{en_US Specify regex pattern of repos to include}.

=item B<--include-repos>=I<s@>

{en_US Specific git repos to sync, if not specified all repos in the bunch will be processed}.

Can be specified multiple times.

=item B<--repo>=I<s>

{en_US Only process a single repo}.

=item B<--rsync-opt-maintain-ownership>

{en_US Whether or not, when rsync-ing from source, we use -a (= -rlptgoD) or -rlptD (-a minus -go)}.

{en_US 
Sometimes using -a results in failure to preserve permission modes on
sshfs-mounted filesystem, while -rlptD succeeds, so by default we don't maintain
ownership. If you need to maintain ownership (e.g. you run as root and the repos
are not owned by root), turn this option on.
}


=item B<--sort>=I<s>

{en_US Order entries in bunch}.

Default value:

 "-commit-timestamp"

Valid values:

 ["name", "-name", "mtime", "-mtime", "rand", "commit-timestamp", "-commit-timestamp"]

{en_US 
`commit-timestamp` (and `-commit-timestamp`) compares the timestamp of
`.git/commit-timestamp` file in each repo. Repos or dirs not having this file
will be processed later. You can touch these `.git/commit-timestamp` files in
your post-commit script, for example. This allows sorting recently committed
repos more cheaply (compared to doing `git log -1`).
}


=item B<--source>=I<s>*

{en_US Directory to check}.

=item B<--target>=I<s>*

{en_US Destination bunch}.

=back

=head1 ENVIRONMENT

GITBUNCH_OPT

=head1 FILES

~/gitbunch.conf

/etc/gitbunch.conf

=head1 FAQ

=head2 How to see more verbose/debugging output?

B<gitbunch> uses L<Log::Any::App>. You can customize logging levels using a
variety of ways, e.g. environment variable:

 % TRACE=1 gitbunch ...

By default it also writes log to ~/gitbunch.log. You can disable this using:

 % FILE_LOG_LEVEL=off gitbunch ...

Or you can customize the level using:

 % FILE_QUIET=1 gitbunch ...

See the documentation for Log::Any::App for more details.

=head1 COMPLETION

This script has shell tab completion capability with support for several shells.

=head2 bash

To activate bash completion for this script, put:

 complete -C gitbunch gitbunch

in your bash startup (e.g. C<~/.bashrc>). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is recommended, however, that you install L<shcompgen> which allows you to activate completion scripts for several kinds of scripts on multiple shells. Some CPAN distributions (those that are built with L<Dist::Zilla::Plugin::GenShellCompletion>) will even automatically enable shell completion for their included scripts (using C<shcompgen>) at installation time, so you can immadiately have tab completion.

=head2 tcsh

To activate tcsh completion for this script, put:

 complete gitbunch 'p/*/`gitbunch`/'

in your tcsh startup (e.g. C<~/.tcshrc>). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is also recommended to install C<shcompgen> (see above).

=head2 other shells

For fish and zsh, install C<shcompgen> as described above.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/Git-Bunch>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-Git-Bunch>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Git-Bunch>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
