Added 'commit-release' hook.
[0release.git] / scm.py
blobb3b9bd035b6d301dc11ee6d03aaf1e7a7b3b7b45
1 # Copyright (C) 2007, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
4 import os, subprocess
5 from zeroinstall import SafeException
7 class SCM:
8 def __init__(self, local_iface):
9 self.local_iface = local_iface
11 class GIT(SCM):
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)
17 code = child.wait()
18 if code:
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):
31 return 'v' + 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])
41 code = child.wait()
42 if code == 0:
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'))
49 status = child.wait()
50 if status:
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()
61 if proc.returncode:
62 raise Exception("git rev-parse failed with exit code %d" % proc.returncode)
63 head = stdout.strip()
64 assert head
65 return head