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
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
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
)
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()
30 raise SafeException('Failed to get current branch! Exit code %d: %s' % (child
.returncode
, 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()
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:
49 raise SafeException('Uncommitted changes! Use "git-commit -a" to commit them. Changes are:\n' + stdout
)
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'):
63 r
, subdir
= line
.strip().split(' ')[:2]
64 scm
= GIT(os
.path
.join(self
.root_dir
, subdir
), self
.options
)
69 def make_tag(self
, version
):
72 def tag(self
, version
, revision
):
73 tag
= self
.make_tag(version
)
75 key_opts
= ['-u', self
.options
.key
]
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
)
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()
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
112 target
= os
.path
.abspath(target
)
113 for scm
in self
._submodules
():
114 tmp
= tempfile
.NamedTemporaryFile(prefix
= '0release-')
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
)
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
])
133 def get_head_revision(self
):
134 proc
= self
._run
(['rev-parse', 'HEAD'], stdout
= subprocess
.PIPE
)
135 stdout
, unused
= proc
.communicate()
137 raise Exception("git rev-parse failed with exit code %d" % proc
.returncode
)
138 head
= stdout
.strip()
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
)
146 self
._run
_check
(['log', head
], stdout
= stream
)
148 def grep(self
, pattern
):
149 child
= self
._run
(['grep', pattern
])
151 if child
.returncode
in [0, 1]:
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
))
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
)