Skip to content

Commit

Permalink
git: Don't reset last active branch to wrong commit
Browse files Browse the repository at this point in the history
When we change the checked out code using `git reset --hard <rev>` we
also change the revision that the currently active branch points to.
This leads to that branch pointing to a wrong commit as we're actually
checking out a different branch (we switch to it just a little bit
later). Instead of modifying the current branch we should just modify
the working copy, which can be done by `git checkout -f <rev>`.
  • Loading branch information
p12tic committed Mar 18, 2021
1 parent 6c20102 commit 44e14d6
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed the ``Git`` source step causing last active branch to point to wrong commits.
This only affected the branch state in the repository, the checked out code was correct.
7 changes: 3 additions & 4 deletions master/buildbot/steps/source/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def _fetch(self, _):
rev = self.revision
else:
rev = 'FETCH_HEAD'
command = ['reset', '--hard', rev, '--']
command = ['checkout', '-f', rev]
abandonOnFailure = not self.retryFetch and not self.clobberOnFailure
res = yield self._dovccmd(command, abandonOnFailure)

Expand Down Expand Up @@ -424,9 +424,8 @@ def _fullClone(self, shallowClone=False):

# If revision specified checkout that revision
if self.revision:
res = yield self._dovccmd(['reset', '--hard',
self.revision, '--'],
shallowClone)
res = yield self._dovccmd(['checkout', '-f', self.revision], shallowClone)

# init and update submodules, recursively. If there's not recursion
# it will not do it.
if self.submodules:
Expand Down
8 changes: 4 additions & 4 deletions master/buildbot/test/unit/steps/test_source_gerrit.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_mode_full_clean(self):
'gerrit_branch', '--progress'])
+ 0,
ExpectShell(workdir='wkdir',
command=['git', 'reset', '--hard', 'FETCH_HEAD', '--'])
command=['git', 'checkout', '-f', 'FETCH_HEAD'])
+ 0,
ExpectShell(workdir='wkdir',
command=['git', 'checkout', '-B', 'gerrit_branch'])
Expand Down Expand Up @@ -110,7 +110,7 @@ def test_mode_full_clean_force_build(self):
'refs/changes/34/1234/567', '--progress'])
+ 0,
ExpectShell(workdir='wkdir',
command=['git', 'reset', '--hard', 'FETCH_HEAD', '--'])
command=['git', 'checkout', '-f', 'FETCH_HEAD'])
+ 0,
ExpectShell(workdir='wkdir',
command=['git', 'checkout', '-B', 'refs/changes/34/1234/567'])
Expand Down Expand Up @@ -156,7 +156,7 @@ def test_mode_full_clean_force_same_project(self):
'refs/changes/34/1234/567', '--progress'])
+ 0,
ExpectShell(workdir='wkdir',
command=['git', 'reset', '--hard', 'FETCH_HEAD', '--'])
command=['git', 'checkout', '-f', 'FETCH_HEAD'])
+ 0,
ExpectShell(workdir='wkdir',
command=['git', 'checkout', '-B', 'refs/changes/34/1234/567'])
Expand Down Expand Up @@ -202,7 +202,7 @@ def test_mode_full_clean_different_project(self):
'HEAD', '--progress'])
+ 0,
ExpectShell(workdir='wkdir',
command=['git', 'reset', '--hard', 'FETCH_HEAD', '--'])
command=['git', 'checkout', '-f', 'FETCH_HEAD'])
+ 0,
ExpectShell(workdir='wkdir',
command=['git', 'rev-parse', 'HEAD'])
Expand Down

0 comments on commit 44e14d6

Please sign in to comment.