commit 6cf1ad06f4416396e8ee1d1ddd96adca7f48c95f
Author: Nishanth Aravamudan <nish.aravamudan@canonical.com>
Date: Tue Aug 29 14:40:52 2017 -0700
git_repository::pristine_tar_branches: do not clobber any local branches
Save the current pristine-tar branch commit, if any and re-create it on
exit.
Forcibly create a new pristine-tar branch pointing at the remote
pristine-tar branch we want.
LP: #1713780
diff --git a/gitubuntu/git_repository.py b/gitubuntu/git_repository.py
index 7ca6dfd..e1885aa 100644
--- a/gitubuntu/git_repository.py
+++ b/gitubuntu/git_repository.py
@@ -486,18 +486,51 @@ class GitUbuntuRepository:
In this context, the repository pristine-tar branch will point to
the pristine-tar branch for @dist distribution in @namespace.
- If a local branch named pristine-tar exists already, it will be
- moved to a (hopefully) safe name.
+
+ Because of our model, the distribution-pristine-tar branch may
+ be a local branch (import-time) or a remote-tracking branch
+ (build-time) and we need different behavior in both cases.
+ Specifically, we want to affect the local branch's contents, but
+ we cannot do that to a remote-tracking branch.
+
+ Upon entry to the context, detect the former case (by doing a
+ local only lookup first) and doing a branch rename there.
+ Otherwise, create a new local branch.
+
+ Upon exit, if a local branch had been found, rename pristine-tar
+ back to the original name. Otherwise, simply delete the created
+ pristine-tar branch.
+
+ If a local branch named pristine-tar existed outside this
+ context, it will be restored upon leaving the context.
"""
pt_branch = '%s/importer/%s/pristine-tar' % (namespace, dist)
old_pt_branch = self.raw_repo.lookup_branch('pristine-tar')
+ old_pt_branch_commit = None
if old_pt_branch:
- self.git_run(['branch', '-M', 'pristine-tar', 'pristine-tar.bak'])
- self.git_run(['branch', 'pristine-tar', pt_branch])
+ old_pt_branch_commit = old_pt_branch.peel(pygit2.Commit)
+ old_pt_branch.delete()
+ local_pt_branch = self.raw_repo.lookup_branch(pt_branch)
+ if local_pt_branch:
+ local_pt_branch.rename('pristine-tar')
+ else:
+ self.raw_repo.create_branch(
+ 'pristine-tar',
+ self.raw_repo.lookup_branch(
+ pt_branch,
+ pygit2.GIT_BRANCH_REMOTE,
+ ).peel(pygit2.Commit),
+ )
yield
- self.git_run(['branch', '-D', 'pristine-tar'])
- if old_pt_branch:
- self.git_run(['branch', '-M', 'pristine-tar.bak', 'pristine-tar'])
+ if local_pt_branch:
+ self.raw_repo.lookup_branch('pristine-tar').rename(pt_branch)
+ else:
+ self.raw_repo.lookup_branch('pristine-tar').delete()
+ if old_pt_branch_commit:
+ self.raw_repo.create_branch(
+ 'pristine-tar',
+ old_pt_branch_commit,
+ )
def pristine_tar_list(self, dist, namespace='pkg'):
with self.pristine_tar_branches(dist, namespace):