aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/gbp-posttag-push
blob: c4feda9c09ee939df87c118b79b3c2d8ac3c18fb (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
#!/usr/bin/python
# vim: set fileencoding=utf-8 :
#
# (C) 2009 Guido Guenther <agx@sigxcpu.org>
#
# gbp-posttag-push: post tag hook to be called by git-buildpackage to push out
# the newly created tag and to forward the remote branch to that position
# 
# it checks for explicit push destinations, if none are found it pushes back to
# where the branch got merged from. Before pushing it checks if the tag is
# signed.
#
# use:
# [git-buildpackage]
# posttag = gbp-posttag-push
#
# Options:
# -d: dry-run

import os
import subprocess
import sys
import gbp.command_wrappers as gbpc
from optparse import OptionParser


class Env(object):
    pass


def get_pushs(env):
    """get a list of push targets"""
    dests = {}
    cmd = "git config --get-regexp 'remote\..*\.push' '%s(:.*)?$'" % env.branch
    for remote in subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()[0].split("\n"):
        if not len(remote):
            continue
        print '"%s"' % remote
        repo, refspec = remote.split()
        repo = ".".join(repo.split('.')[1:-1]) # remote.<repo>.push
        try:
            remote = refspec.split(':')[1] # src:dest
        except IndexError:
            remote = refspec
        dests[repo] = remote
    return dests


def get_pull(env):
    """where did we pull from?"""
    cmd = 'git config --get branch."%s".remote' % env.branch
    remote =  subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()[0].strip()
    if not remote:
        remote = 'origin'
    return { remote: env.branch }


def git_push_sim(args):
    print "git push %s" % " ".join(args)


def main(args):
    env = Env()

    git_verify_tag = gbpc.GitCommand("tag", ["-v"])

    parser = OptionParser()
    parser.add_option("-d", "--dry-run", dest="dryrun", default=False,
                      action="store_true", help="dry run, don't push.")

    (options, args) = parser.parse_args()

    if options.dryrun:
        print "Dry run mode. Not pushing."
        git_push = git_push_sim
    else:
        git_push = gbpc.GitCommand("push")

    for envvar in [ "GBP_TAG", "GBP_BRANCH", "GBP_SHA1" ]:
        var = os.getenv(envvar)
        if var:
            env.__dict__.setdefault( "%s" % envvar.split("_")[1].lower(), var)
        else:
            print >>sys.stderr, "%s not set." % envvar 
            return 1

    dests = get_pushs(env)
    if not dests:
        dests = get_pull(env)

    try:
        git_verify_tag([env.tag])
    except gbpc.CommandExecFailed:
        print >>sys.stderr, "Not pushing unsigned tag $GBP_TAG."
        return 0

    for dest in dests:
        print "Pushing %s to %s" % (env.sha1, dest)
        git_push([dest, "tag", env.tag])
        git_push([dest, "%s:%s" % (env.sha1, dests[dest])])
        print "done."

if __name__ == '__main__':
    sys.exit(main(sys.argv))

# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: