Don't put refs/tags before tag name.
[0release.git] / scm.py
blob4e52da9a964662cf03d9f067d493fcc9285ca0fe
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
6 from logging import info
8 class SCM:
9 def __init__(self, local_iface, options):
10 self.local_iface = local_iface
11 self.options = options
13 class GIT(SCM):
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)
20 code = child.wait()
21 if code:
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 'v' + version
36 def tag(self, version, revision):
37 tag = self.make_tag(version)
38 if self.options.key:
39 key_opts = ['-u', self.options.key]
40 else:
41 key_opts = []
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()
48 if child.returncode:
49 print stdout
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])
58 code = child.wait()
59 if code == 0:
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'))
66 status = child.wait()
67 if status:
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()
78 if proc.returncode:
79 raise Exception("git rev-parse failed with exit code %d" % proc.returncode)
80 head = stdout.strip()
81 assert head
82 return head