1package inc::GitUpToDate;
2use Moose;
3
4with 'Dist::Zilla::Role::BeforeBuild';
5
6sub git {
7    if (wantarray) {
8        chomp(my @ret = qx{git $_[0]});
9        return @ret;
10    }
11    else {
12        chomp(my $ret = qx{git $_[0]});
13        return $ret;
14    }
15}
16
17sub before_build {
18    my $self = shift;
19
20    return unless $ENV{DZIL_RELEASING};
21
22    my $branch = git "symbolic-ref HEAD";
23    die "Could not get the current branch"
24        unless $branch;
25
26    $branch =~ s{refs/heads/}{};
27
28    $self->log("Ensuring branch $branch is up to date");
29
30    git "fetch origin";
31    my $origin = git "rev-parse origin/$branch";
32    my $head = git "rev-parse HEAD";
33
34    die "Branch $branch is not up to date (origin: $origin, HEAD: $head)"
35        if $origin ne $head;
36
37
38    # now also check that HEAD is current with the release branch
39    # that is, that the release branch is an ancestor commit of HEAD.
40    my $release_branch = ($self->zilla->plugin_named('Git::CheckFor::CorrectBranch')->release_branch)[0];
41    foreach my $remote ('origin/', '')
42    {
43        my $release_commit = git "rev-parse ${remote}$release_branch";
44        my $common_ancestor = git "merge-base $head $release_commit";
45
46        die "Branch $branch does not contain all commits from the current release branch ",
47                "(common ancestor for ${remote}$release_branch: $common_ancestor)"
48            if $common_ancestor ne $release_commit;
49    }
50}
51
521;
53