summaryrefslogtreecommitdiffstats
path: root/bin/git-gpush
blob: 24c5ba33fce7791f5ce1e2469655ac175483d366 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env perl
# Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
# Contact: http://www.qt-project.org/
#
# You may use this file under the terms of the 3-clause BSD license.
# See the file LICENSE from this package for details.
#

use strict;
use warnings;

package Git::Gerrit::Push;

use File::Basename;

# Cannot use Pod::Usage for this file, since git on Windows will invoke its own perl version, which
# may not (msysgit for example) support this module, even if it's considered a Core module.
sub usage
{
    print << "EOM";
Usage:
    git gpush [opts] [remote] [[sha1/ref-from]:[ref-to]] [+<reviewer>] [=<CC user>] [-- <push opts>]

    Pushes changes to Gerrit and adds reviewers and CC to the patch
    sets

Description:
    This script is used to push patch sets to Gerrit, and at the same
    time add reviewers and CCs to the patch sets pushed.

    You can use email addresses, Gerrit usernames or aliases for the
    name of the reviewers/CCs. Aliases are read from the
        .git-gpush-aliases
    located next to the script, then from the git config which may
    have aliases set either locally in the current repository,
    globally (in your ~/.gitconfig), or system-wide.

    You can and alias in your global git config like this:
        git config --global gpush.alias.<alias key> <alias value>
    and if you only want it local in the current repository, just drop
    the --global option.

    An alias may contain multiple comma-separated email addresses;
    for example, to set a single alias for an entire team.

    If no sha1 or ref-from is specified or configured, 'HEAD' is used.
    You may configure a ref-from like this
        git config gpush.ref-from <ref-from value>

    If no ref-to is specified or configured, the remote tracking
    branch for 'ref-from' is used as
        'refs/for/<remote tracking branch>'.
    You may configure a ref-to like this
        git config gpush.ref-to <ref-from value>

    If no remote is specified or configured, 'gerrit' is used. You may
    configure a remote like this:
        git config gpush.remote <remote name>

    If all the options above have been populated, the remainder
    options are passed on directly to the normal 'git push' command.
    If you want to avoid specifying all options first, any options
    specified after a '--' are also passed on directly to the
    underlying 'git push' command. This is can be particularly useful
    with the -n/--dry-run options, which make git do everything except
    actually sending the updates.

Options:
    -v, --verbose
        Shows the alias resolving, and final 'git push' command as a
        comma-separated list of arguments.

    --aliases
        Reports all registered aliases.

Copyright:
    Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
    Contact: http://www.qt-project.org/

License:
    You may use this file under the terms of the 3-clause BSD license.
EOM
}

sub parse_arguments
{
    my ($self, @arguments) = @_;

    while (scalar @arguments) {
        my $arg = shift @arguments;

        if ($arg eq "-v" || $arg eq "--verbose") {
            $self->{'verbose'} = 1;
            push @{$self->{'arguments'}}, $arg;
        } elsif ($arg eq "--aliases") {
            foreach my $key (sort(keys %{$self->{'aliases'}})) {
                print "$key = $self->{'aliases'}->{$key}\n";
            }
            exit 0;
        } elsif ($arg eq "-?" || $arg eq "--?" || $arg eq "-h" || $arg eq "--help") {
            $self->usage();
            exit 0;
        } elsif ($arg eq "--") {
            push @{$self->{'arguments'}}, @arguments;
            return;
        } elsif ($arg =~ /^\+(.+)/) {
            push @{$self->{'reviewers'}}, split(/,/, $self->lookup_alias($1));
        } elsif ($arg =~ /^\=(.+)/) {
            push @{$self->{'CCs'}}, split(/,/, $self->lookup_alias($1));
        } elsif ($arg =~ /^\-(.+)/) {
            push @{$self->{'arguments'}}, $arg;
        } elsif (!$self->{'remote-override'} || !$self->{'ref-override'}) {
            if ($arg =~ /(.*):(.*)/) {
                $self->{'ref-from'} = $1 if (defined $1 && $1 ne "");
                $self->{'ref-to'} = $2 if (defined $2 && $2 ne "");
                $self->{'ref-override'} = 1;
            } else {
                $self->{'remote'} = $arg;
                $self->{'remote-override'} = 1;
            }
        } else {
            push @{$self->{'arguments'}}, $arg;
        }
    }
}

sub fileContents {
    my ($self, $filename) = @_;

    my @contents = "";
    my $fh;
    if (-e $filename && open($fh, "< $filename")) {
        @contents = <$fh>;
        close $fh;
    }
    return @contents;
}

sub load_aliases
{
    my ($self) = @_;
    my $script_path = dirname($0);

    # Read aliases from .git-gpush-aliases file
    foreach my $line ($self->fileContents("$script_path/.git-gpush-aliases")) {
        chomp $line;
        $line =~ s,(#|//).*$,,;             # Remove any comments
        if ($line =~ /([^ ]+)\s*=\s*(\S+)/) {  # Capture the alias
            $self->{'aliases'}->{$1} = $2;
        }
    }

    # Read aliases and configurations from git config
    my @gitconfigs = `git config --get-regexp gpush.*`;
    return if ($?); # just return if no git configs for gpush

    foreach (@gitconfigs) {
        if (/^gpush\.(remote|ref-from|ref-to) (\w+)/) {
            $self->{$1} = $2;
        } elsif (/^gpush\.alias.([^ ]*) (.+)/) {
            $self->{'aliases'}->{$1} = $2;
        } # else ignore
    }
}

sub lookup_alias
{
    my ($self, $user) = @_;

    my $alias = $self->{'aliases'}->{$user};
    if (defined $alias && $alias ne "") {
        print " $user = $alias\n" if ($self->{'verbose'});
        return $alias;
    }

    return $user;
}

sub push_patches
{
    my ($self) = @_;

    # Detect tracking branch if ref-to is not set
    if ($self->{'ref-to'} eq "") {
        my $ref = $self->{'ref-from'};
        $ref =~ s/[~^].*$//;
        my $sref = `git symbolic-ref -q $ref`;
        if ($? == 0) {
            chomp $sref;
            $ref = $sref;
        }
        $ref =~ s,^refs/heads/,,;
        `git rev-parse --verify -q refs/heads/$ref`;
        die "Cannot detect tracking branch, $ref is not a valid ref.\n" if ($? != 0);
        my $trackref = `git config branch.$ref.merge`;
        die "Cannot detect tracking branch, 'git config branch.$ref.merge' failed.\n" if ($? != 0);
        chomp $trackref;
        $trackref =~ s,^refs/heads/,,;
        $self->{'ref-to'} = $trackref;
    }
    if ($self->{'ref-to'} =~ m,^refs/for/,) {
        print STDERR "Notice: it is unnecessary to specify refs/for/ in the target ref.\n";
    } else {
        $self->{'ref-to'} = "refs/for/".$self->{'ref-to'};
    }

    my @reviewers = ();
    if (@{$self->{'reviewers'}} || @{$self->{'CCs'}}) {
        push @reviewers, "--receive-pack=git receive-pack";
        push @reviewers, map { " --reviewer=$_" } @{$self->{'reviewers'}};
        push @reviewers, map { " --cc=$_" } @{$self->{'CCs'}};
    }

    my @gitcmd = ("git", "push");
    push @gitcmd, @{$self->{'arguments'}};
    push @gitcmd, join '', @reviewers if(scalar @reviewers); # Single argument to git push
    push @gitcmd, $self->{'remote'}, "$self->{'ref-from'}:$self->{'ref-to'}";

    print '+'.join(',', @gitcmd)."\n" if ($self->{'verbose'});
    exit system(@gitcmd);
}

sub new
{
    my ($class, @arguments) = @_;

    my $self = {};
    bless $self, $class;

    $self->{'verbose'} = 0;

    $self->{'remote'} = "gerrit";
    $self->{'remote-override'} = 0;
    $self->{'ref-from'} = "HEAD";
    $self->{'ref-to'} = "";
    $self->{'ref-override'} = 0;

    $self->{'aliases'} = ();

    $self->{'reviewers'} = [];
    $self->{'CCs'} = [];

    $self->{'arguments'} = [];

    $self->load_aliases;
    $self->parse_arguments(@arguments);
    return $self;
}

sub run
{
    my ($self) = @_;
    $self->push_patches;
}

#==============================================================================

Git::Gerrit::Push->new(@ARGV)->run if (!caller);
1;