Ubuntu Pastebin

Paste from nacc at Tue, 29 Aug 2017 22:23:59 +0000

Download as text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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):
Download as text