App-IsGitSynced

 view release on metacpan or  search on metacpan

bin/is_git_synced  view on Meta::CPAN

    `cd $path; git diff --exit-code 2>&1`;

    if (not ${^CHILD_ERROR_NATIVE}) {
        return $STATUSES{success};
    } else {
        return (
            $STATUSES{fail},
            "path '$path' has unstaged changes"
        );
    }
}

sub has_no_staged_changes {
    my ($path) = @_;

    `cd $path; git diff --cached --exit-code 2>&1`;

    if (not ${^CHILD_ERROR_NATIVE}) {
        return $STATUSES{success};
    } else {
        return (
            $STATUSES{fail},
            "path '$path' has staged changes",
        );
    }
}

sub has_origin {
    my ($path) = @_;

    my $output = `cd $path; git remote`;
    my @remotes = split(/\n/, $output);

    my $has_origin;
    foreach my $remote (@remotes) {
        $has_origin = $TRUE if $remote eq 'origin';
    }

    if ($has_origin) {
        return $STATUSES{success};
    } else {
        return (
            $STATUSES{fail},
            "path '$path' has no remote 'origin'",
        );
    }
}

# http://stackoverflow.com/questions/8830833/check-that-the-local-git-repo-has-everything-commited-and-pushed-to-master
sub has_no_divergences_with_origin {
    my ($path) = @_;

    my $output = `cd $path; git branch`;
    my @branches = map { s/..(.*)/$1/; $_; } split(/\n/, $output);

    my $has_divergences_with_origin;
    foreach my $branch (@branches) {
        next if $branch eq '(no branch)';
        next if $branch =~ /(detached .*)/;

        my $local = `cd $path; git rev-parse --verify $branch 2>&1`;
        my $origin = `cd $path; git rev-parse --verify origin/$branch 2>&1`;

        $has_divergences_with_origin = $TRUE if $local ne $origin;
    }

    if (not $has_divergences_with_origin) {
        return $STATUSES{success};
    } else {
        return (
            $STATUSES{fail},
            "path '$path' has some divergences with remote 'origin'",
        );
    }
}

# main
my @paths = get_paths_and_set_options();

if ($OPTIONS{'--help'}) {
    pod2usage({
        -exitval => $SUCCESS_EXIT_STATUS,
    });
} elsif ($OPTIONS{'--version'}) {
    print "is_git_synced $App::IsGitSynced::VERSION\n";
    exit $SUCCESS_EXIT_STATUS;
}

my $was_error;

if (!@paths) {
    error("no required path specified");
    $was_error++;
}

foreach my $path (@paths) {

    my @checks = (
        \&is_dir,
        \&is_git_repo,
        \&has_no_untracked,
        \&has_no_unstaged_changes,
        \&has_no_staged_changes,
        \&has_origin,
        \&has_no_divergences_with_origin,
    );

    my $local_error;
    my $skipped;

    CHECKS:
    foreach my $check (@checks) {
        my ($check_result, $fail_text) = $check->($path);

        if ($check_result == $STATUSES{success}) {
            next CHECKS;
        } elsif ($check_result == $STATUSES{skip}) {
            $skipped = $TRUE;
            last CHECKS;
        } elsif ($check_result == $STATUSES{fail}) {
            error($fail_text);
            $local_error = 1;



( run in 2.090 seconds using v1.01-cache-2.11-cpan-e1769b4cff6 )