Force unit-tests to test with the same version of 0release
[0release.git] / scm.py
blob1a8e96d0e195dde036eda5ccc25ba6603b78c9bf
1 # Copyright (C) 2007, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
4 import os, subprocess, tempfile
5 from zeroinstall import SafeException
6 from logging import info, warn
7 from support import unpack_tarball
9 class SCM:
10 def __init__(self, root_dir, options):
11 self.options = options
12 self.root_dir = root_dir
13 assert type(root_dir) == str, root_dir
15 class GIT(SCM):
16 def _run(self, args, **kwargs):
17 info("Running git %s (in %s)", ' '.join(args), self.root_dir)
18 return subprocess.Popen(["git"] + args, cwd = self.root_dir, **kwargs)
20 def _run_check(self, args, **kwargs):
21 child = self._run(args, **kwargs)
22 code = child.wait()
23 if code:
24 raise SafeException("Git %s failed with exit code %d" % (repr(args), code))
26 def _run_stdout(self, args, **kwargs):
27 child = self._run(args, stdout = subprocess.PIPE, **kwargs)
28 stdout, unused = child.communicate()
29 if child.returncode:
30 raise SafeException('Failed to get current branch! Exit code %d: %s' % (child.returncode, stdout))
31 return stdout
33 def ensure_versioned(self, path):
34 """Ensure path is a file tracked by the version control system.
35 @raise SafeException: if file is not tracked"""
36 out = self._run_stdout(['ls-tree', 'HEAD', path]).strip()
37 if not out:
38 raise SafeException("File '%s' is not under version control, according to git-ls-tree" % path)
40 def reset_hard(self, revision):
41 self._run_check(['reset', '--hard', revision])
43 def ensure_committed(self):
44 child = self._run(["status", "--porcelain", "-uno"], stdout = subprocess.PIPE)
45 stdout, unused = child.communicate()
46 if child.returncode == 0:
47 # Git >= 1.7
48 if stdout.strip():
49 raise SafeException('Uncommitted changes! Use "git-commit -a" to commit them. Changes are:\n' + stdout)
50 return
51 else:
52 # Old Git
53 child = self._run(["status", "-a"], stdout = subprocess.PIPE)
54 stdout, unused = child.communicate()
55 if not child.returncode:
56 raise SafeException('Uncommitted changes! Use "git-commit -a" to commit them. Changes are:\n' + stdout)
57 for scm in self._submodules():
58 scm.ensure_committed()
60 def _submodules(self):
61 for line in self._run_stdout(['submodule', 'status']).split('\n'):
62 if not line: continue
63 r, subdir = line.strip().split(' ')[:2]
64 scm = GIT(os.path.join(self.root_dir, subdir), self.options)
65 scm.rev = r
66 scm.rel_path = subdir
67 yield scm
69 def make_tag(self, version):
70 return 'v' + version
72 def tag(self, version, revision):
73 tag = self.make_tag(version)
74 if self.options.key:
75 key_opts = ['-u', self.options.key]
76 else:
77 key_opts = []
78 self._run_check(['tag', '-s'] + key_opts + ['-m', 'Release %s' % version, tag, revision])
79 print "Tagged as %s" % tag
81 def get_current_branch(self):
82 current_branch = self._run_stdout(['symbolic-ref', 'HEAD']).strip()
83 info("Current branch is %s", current_branch)
84 return current_branch
86 def delete_branch(self, branch):
87 self._run_check(['branch', '-D', branch])
89 def push_head_and_release(self, version):
90 self._run_check(['push', self.options.public_scm_repository, self.make_tag(version), self.get_current_branch()])
92 def ensure_no_tag(self, version):
93 tag = self.make_tag(version)
94 child = self._run(['tag', '-l', tag], stdout = subprocess.PIPE)
95 stdout, unused = child.communicate()
96 if tag in stdout.split('\n'):
97 raise SafeException(("Release %s is already tagged! If you want to replace it, do\n" +
98 "git tag -d %s") % (version, tag))
100 def export(self, prefix, archive_file, revision):
101 child = self._run(['archive', '--format=tar', '--prefix=' + prefix + '/', revision], stdout = subprocess.PIPE)
102 subprocess.check_call(['bzip2', '-'], stdin = child.stdout, stdout = file(archive_file, 'w'))
103 status = child.wait()
104 if status:
105 if os.path.exists(archive_file):
106 os.unlink(archive_file)
107 raise SafeException("git-archive failed with exit code %d" % status)
109 def export_submodules(self, target):
110 # Export all sub-modules under target
111 cwd = os.getcwd()
112 target = os.path.abspath(target)
113 for scm in self._submodules():
114 tmp = tempfile.NamedTemporaryFile(prefix = '0release-')
115 try:
116 scm.export(prefix = '.', archive_file = tmp.name, revision = scm.rev)
117 os.chdir(os.path.join(target, scm.rel_path))
118 unpack_tarball(tmp.name)
119 finally:
120 tmp.close()
121 os.chdir(cwd)
123 def commit(self, message, branch, parent):
124 self._run_check(['add', '-u']) # Commit all changed tracked files to index
125 tree = self._run_stdout(['write-tree']).strip()
126 child = self._run(['commit-tree', tree, '-p', parent], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
127 stdout, unused = child.communicate(message)
128 commit = stdout.strip()
129 info("Committed as %s", commit)
130 self._run_check(['branch', '-f', branch, commit])
131 return commit
133 def get_head_revision(self):
134 proc = self._run(['rev-parse', 'HEAD'], stdout = subprocess.PIPE)
135 stdout, unused = proc.communicate()
136 if proc.returncode:
137 raise Exception("git rev-parse failed with exit code %d" % proc.returncode)
138 head = stdout.strip()
139 assert head
140 return head
142 def export_changelog(self, last_release_version, head, stream):
143 if last_release_version:
144 self._run_check(['log', 'refs/tags/v' + last_release_version + '..' + head], stdout = stream)
145 else:
146 self._run_check(['log', head], stdout = stream)
148 def grep(self, pattern):
149 child = self._run(['grep', pattern])
150 child.wait()
151 if child.returncode in [0, 1]:
152 return
153 warn("git grep returned exit code %d", proc.returncode)
155 def has_submodules(self):
156 return os.path.isfile(os.path.join(self.root_dir, '.gitmodules'))
158 def get_scm(local_feed, options):
159 start_dir = os.path.dirname(os.path.abspath(local_feed.local_path))
160 current = start_dir
161 while True:
162 if os.path.exists(os.path.join(current, '.git')):
163 return GIT(current, options)
164 parent = os.path.dirname(current)
165 if parent == current:
166 raise SafeException("Unable to determine which version control system is being used. Couldn't find .git in %s or any parent directory." % start_dir)
167 current = parent