summaryrefslogtreecommitdiffstats
path: root/lib/Devscripts/Salsa/push.pm
blob: fa3b6cc23d46d65662d5df4cc4a4454be71e7b05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Push local work. Like gbp push but able to push incomplete work
package Devscripts::Salsa::push;
use strict;
use Devscripts::Output;
use Devscripts::Utils;
use Dpkg::Source::Format;
use Moo::Role;
use Dpkg::IPC;

sub readGbpConf {
    my ($self) = @_;
    my $res = '';
    foreach my $gbpconf (qw(.gbp.conf debian/gbp.conf .git/gbp.conf)) {
        if (-e $gbpconf) {
            open(my $f, $gbpconf);
            while (<$f>) {
                $res .= $_;
                if (/^\s*(debian|upstream)\-(branch|tag)\s*=\s*(.*\S)/) {
                    $self->{"$1_$2"} = $3;
                }
            }
            close $f;
            last;
        }
    }
    if ($self->{debian_tag}) {
        $self->{debian_tag} =~ s/%\(version\)s/.*/g;
        $self->{debian_tag} =~ s/^/^/;
        $self->{debian_tag} =~ s/$/\$/;
    } else {
        my @tmp
          = Dpkg::Source::Format->new(filename => 'debian/source/format')->get;
        $self->{debian_tag} = $tmp[2] eq 'native' ? '.*' : '^debian/.*$';
    }
    if ($self->{upstream_tag}) {
        $self->{upstream_tag} =~ s/%\(version\)s/.*/g;
        $self->{upstream_tag} =~ s/^/^/;
        $self->{upstream_tag} =~ s/$/\$/;
    } else {
        $self->{upstream_tag} = '^upstream/.*$';
    }
    $self->{debian_branch}   ||= 'master';
    $self->{upstream_branch} ||= 'upstream';
    return $res;
}

sub push {
    my ($self) = @_;
    $self->readGbpConf;
    my @refs;
    foreach (
        $self->{debian_branch}, $self->{upstream_branch},
        'pristine-tar',         'refs/notes/commits'
    ) {
        if (ds_exec_no_fail(qw(git rev-parse --verify --quiet), $_) == 0) {
            push @refs, $_;
        }
    }
    my $out;
    spawn(exec => ['git', 'tag'], wait_child => 1, to_string => \$out);
    my @tags = grep /(?:$self->{debian_tag}|$self->{upstream_tag})/,
      split(/\r?\n/, $out);
    unless (
        $ds_yes < 0
        and ds_prompt(
                "You're going to push :\n - "
              . join(', ', @refs)
              . "\nand check tags that match:\n - "
              . join(', ', $self->{debian_tag}, $self->{upstream_tag})
              . "\nContinue (Y/n) "
        ) =~ refuse
    ) {
        my $origin;
        eval {
            spawn(
                exec       => ['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
                wait_child => 1,
                to_string  => \$out,
            );
            chomp $out;
            spawn(
                exec =>
                  ['git', 'config', '--local', '--get', "branch.$out.remote"],
                wait_child => 1,
                to_string  => \$origin,
            );
            chomp $origin;
        };
        if ($origin) {
            ds_verbose 'Origin is ' . $origin;
        } else {
            ds_warn 'Unable to detect remote name, trying "origin"';
            ds_verbose "Error: $@" if ($@);
            $origin = 'origin';
        }
        ds_verbose "Execute 'git push $origin " . join(' ', @refs, '<tags>');
        ds_debug "Tags are: " . join(' ', @tags);
        spawn(
            exec       => ['git', 'push', $origin, @refs, @tags],
            wait_child => 1
        );
    }
    return 0;
}

1;