Skip to content

Commit b1e7fd1

Browse files
committed
new checkout methods and helpers
1 parent 434ed6e commit b1e7fd1

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

git_explode/exploder.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def run(self):
3434
orig_head = GitExplodeUtils.get_head()
3535
commits, deps_from, deps_on = self.get_dependencies()
3636
self.explode(commits, deps_from, deps_on)
37-
print("checkout %s" % orig_head)
37+
self.checkout(orig_head)
3838

3939
def get_dependencies(self):
4040
"""
@@ -101,14 +101,12 @@ def explode(self, commits, deps_from, deps_on):
101101
deps = deps_from[sha]
102102

103103
if not deps:
104-
base_desc = GitUtils.commit_summary(self.base_commit)
105104
branch = self.topic_mgr.register(sha)
106-
print("checkout %s on base %s" % (branch, base_desc))
107-
self.current_branch = branch
105+
self.checkout_new(branch, self.base)
108106
else:
109107
self.prepare_cherrypick_base(sha, deps.keys(), commits)
110108

111-
print("cherrypick %s" % GitUtils.commit_summary(commit))
109+
GitExplodeUtils.git('cherry-pick', sha)
112110

113111
self.queue_new_leaves(todo, commit, commits, deps_on,
114112
unexploded_deps_from)
@@ -121,21 +119,18 @@ def prepare_cherrypick_base(self, sha, deps, commits):
121119
branch = existing_branch
122120
self.topic_mgr.assign(branch, sha)
123121
if self.current_branch != branch:
124-
print("checkout %s" % branch)
125-
self.current_branch = branch
122+
self.checkout(branch)
126123
else:
127124
if existing_branch is None:
128125
branch = self.topic_mgr.register(*deps)
129-
base_desc = GitUtils.commit_summary(commits[deps[0]])
130-
print("checkout %s on %s" % (branch, base_desc))
131-
self.current_branch = branch
126+
self.checkout_new(branch, deps[0])
132127
to_merge = deps[1:]
133-
print("merge %s" % ' '.join(to_merge))
128+
GitExplodeUtils.git('merge', *to_merge)
134129
else:
135130
# Can reuse existing merge commit, but
136131
# create a new branch at the same point
137132
branch = self.topic_mgr.register(*deps)
138-
print("checkout -b %s %s" % (branch, existing_branch))
133+
self.checkout_new(branch, existing_branch)
139134

140135
def queue_new_leaves(self, todo, exploded_commit, commits, deps_on,
141136
unexploded_deps_from):
@@ -165,3 +160,13 @@ def get_leaves(self, commits, deps_from):
165160
if len(dependencies) == 0:
166161
leaves.append(commits[sha])
167162
return leaves
163+
164+
def checkout(self, branch):
165+
self.logger.debug("checkout %s" % branch)
166+
GitExplodeUtils.checkout(branch)
167+
self.current_branch = branch
168+
169+
def checkout_new(self, branch, at):
170+
self.logger.debug("checkout -b %s %s" % (branch, at))
171+
GitExplodeUtils.checkout_new(branch, at)
172+
self.current_branch = branch

git_explode/gitutils.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,31 @@
2323

2424

2525
class GitUtils(object):
26+
@classmethod
27+
def git(cls, *args):
28+
cmd_words = ['git'] + list(args)
29+
print(' '.join(cmd_words))
30+
return cls.quiet_git(*args)
31+
32+
@classmethod
33+
def quiet_git(cls, *args):
34+
cmd_words = ['git'] + list(args)
35+
output = subprocess.check_output(cmd_words)
36+
return output.rstrip()
37+
2638
@classmethod
2739
def get_head(cls):
28-
"""
40+
"""Retrieve the branch or reference to the current HEAD.
2941
"""
3042
try:
31-
return subprocess.check_output(['git', 'symbolic-ref',
32-
'--short', '-q', 'HEAD']).rstrip()
43+
return cls.quiet_git('symbolic-ref', '--short', '-q', 'HEAD')
3344
except subprocess.CalledProcessError:
34-
return subprocess.check_output(['git', 'rev-parse',
35-
'HEAD']).rstrip()
45+
return cls.quiet_git('rev-parse', 'HEAD')
46+
47+
@classmethod
48+
def checkout(cls, branch):
49+
cls.git('checkout', '-q', branch)
50+
51+
@classmethod
52+
def checkout_new(cls, branch, at):
53+
cls.git('checkout', '-q', '-b', branch, at)

0 commit comments

Comments
 (0)