1 # Copyright (C) 2007, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
5 from zeroinstall
import SafeException
6 from logging
import info
9 def __init__(self
, local_iface
, options
):
10 self
.local_iface
= local_iface
11 self
.options
= options
14 def _run(self
, args
, **kwargs
):
15 info("Running git %s", ' '.join(args
))
16 return subprocess
.Popen(["git"] + args
, cwd
= os
.path
.dirname(self
.local_iface
.uri
), **kwargs
)
18 def _run_check(self
, args
, **kwargs
):
19 child
= self
._run
(args
, **kwargs
)
22 raise SafeException("Git %s failed with exit code %d" % (repr(args
), code
))
24 def reset_hard(self
, revision
):
25 self
._run
_check
(['reset', '--hard', revision
])
27 def ensure_committed(self
):
28 child
= self
._run
(["status", "-a"], stdout
= subprocess
.PIPE
)
29 stdout
, unused
= child
.communicate()
30 if not child
.returncode
:
31 raise SafeException('Uncommitted changes! Use "git-commit -a" to commit them. Changes are:\n' + stdout
)
33 def make_tag(self
, version
):
34 return 'refs/tags/v' + version
36 def tag(self
, version
, revision
):
37 tag
= self
.make_tag(version
)
39 key_opts
= ['-u', self
.options
.key
]
42 self
._run
_check
(['tag', '-s'] + key_opts
+ ['-m', 'Release %s' % version
, tag
, revision
])
43 print "Tagged as %s" % tag
45 def push_head_and_release(self
, version
):
46 child
= self
._run
(['symbolic-ref', 'HEAD'], stdout
= subprocess
.PIPE
)
47 stdout
, unused
= child
.communicate()
50 raise SafeException('Failed to get current branch! Exit code %d' % child
.returncode
)
51 current_branch
= stdout
.strip()
52 info("Current branch is %s", current_branch
)
53 self
._run
_check
(['push', self
.options
.public_scm_repository
, self
.make_tag(version
), current_branch
])
55 def ensure_no_tag(self
, version
):
56 tag
= self
.make_tag(version
)
57 child
= self
._run
(['tag', '-l', '-q', tag
])
60 raise SafeException(("Release %s is already tagged! If you want to replace it, do\n" +
61 "git-tag -d %s") % (version
, tag
))
63 def export(self
, prefix
, archive_file
):
64 child
= self
._run
(['archive', '--format=tar', '--prefix=' + prefix
+ '/', 'HEAD'], stdout
= subprocess
.PIPE
)
65 subprocess
.check_call(['bzip2', '-'], stdin
= child
.stdout
, stdout
= file(archive_file
, 'w'))
68 if os
.path
.exists(archive_file
):
69 os
.unlink(archive_file
)
70 raise SafeException("git-archive failed with exit code %d" % status
)
72 def commit(self
, message
):
73 self
._run
_check
(['commit', '-q', '-a', '-m', message
])
75 def get_head_revision(self
):
76 proc
= self
._run
(['rev-parse', 'HEAD'], stdout
= subprocess
.PIPE
)
77 stdout
, unused
= proc
.communicate()
79 raise Exception("git rev-parse failed with exit code %d" % proc
.returncode
)