1 # Copyright (C) 2007, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
5 from zeroinstall
import SafeException
8 def __init__(self
, local_iface
):
9 self
.local_iface
= local_iface
12 def _run(self
, args
, **kwargs
):
13 return subprocess
.Popen(["git"] + args
, cwd
= os
.path
.dirname(self
.local_iface
.uri
), **kwargs
)
15 def _run_check(self
, args
, **kwargs
):
16 child
= self
._run
(args
, **kwargs
)
19 raise SafeException("Git %s failed with exit code %d" % (repr(args
), code
))
21 def reset_hard(self
, revision
):
22 self
._run
_check
(['reset', '--hard', revision
])
24 def ensure_committed(self
):
25 child
= self
._run
(["status", "-a"], stdout
= subprocess
.PIPE
)
26 stdout
, unused
= child
.communicate()
27 if not child
.returncode
:
28 raise SafeException('Uncommitted changes! Use "git-commit -a" to commit them. Changes are:\n' + stdout
)
30 def make_tag(self
, version
):
33 def tag(self
, version
, revision
):
34 tag
= self
.make_tag(version
)
35 self
._run
_check
(['tag', tag
, revision
])
36 print "Tagged as %s" % tag
38 def ensure_no_tag(self
, version
):
39 tag
= self
.make_tag(version
)
40 child
= self
._run
(['tag', '-l', '-q', tag
])
43 raise SafeException(("Release %s is already tagged! If you want to replace it, do\n" +
44 "git-tag -d %s") % (version
, tag
))
46 def export(self
, prefix
, archive_file
):
47 child
= self
._run
(['archive', '--format=tar', '--prefix=' + prefix
+ '/', 'HEAD'], stdout
= subprocess
.PIPE
)
48 subprocess
.check_call(['bzip2', '-'], stdin
= child
.stdout
, stdout
= file(archive_file
, 'w'))
51 if os
.path
.exists(archive_file
):
52 os
.unlink(archive_file
)
53 raise SafeException("git-archive failed with exit code %d" % status
)
55 def commit(self
, message
):
56 self
._run
_check
(['commit', '-q', '-a', '-m', message
])
58 def get_head_revision(self
):
59 proc
= self
._run
(['rev-parse', 'HEAD'], stdout
= subprocess
.PIPE
)
60 stdout
, unused
= proc
.communicate()
62 raise Exception("git rev-parse failed with exit code %d" % proc
.returncode
)