@@ -35,7 +35,6 @@ class Backend(BaseVCS):
35
35
repo_depth = 50
36
36
37
37
def __init__ (self , * args , ** kwargs ):
38
- self .version_identifier = kwargs .pop ("version_identifier" , None )
39
38
super ().__init__ (* args , ** kwargs )
40
39
self .token = kwargs .get ('token' )
41
40
self .repo_url = self ._get_clone_url ()
@@ -56,9 +55,15 @@ def _get_clone_url(self):
56
55
def set_remote_url (self , url ):
57
56
return self .run ('git' , 'remote' , 'set-url' , 'origin' , url )
58
57
59
- def update (self ):
60
- """Clone or update the repository."""
61
- super ().update ()
58
+ def update (self , identifier = None ):
59
+ """
60
+ Clone or update the repository.
61
+
62
+ :param identifier: This is the optional identifier for git fetch - a branch or tag name.
63
+ PR references are generated automatically for certain Git providers.
64
+ :return:
65
+ """
66
+ super ().update (identifier = identifier )
62
67
63
68
if self .use_clone_fetch_checkout_pattern ():
64
69
@@ -67,7 +72,7 @@ def update(self):
67
72
self .clone_ng ()
68
73
# New behavior: No confusing return value. We are not using return values
69
74
# in the callers.
70
- self .fetch_ng ()
75
+ self .fetch_ng (identifier = identifier )
71
76
72
77
else :
73
78
if self .repo_exists ():
@@ -80,52 +85,65 @@ def update(self):
80
85
return self .fetch ()
81
86
return self .clone ()
82
87
83
- def get_remote_reference (self ):
88
+ def get_remote_fetch_reference (self , identifier ):
89
+ """
90
+ Gets a valid remote reference for the identifier.
91
+
92
+ :param identifier: Should be a branch or tag name when building branches or tags.
93
+ :return: A reference valid for fetch operation
94
+ """
84
95
# Tags and branches have the tag/branch identifier set by the caller who instantiated the
85
96
# Git backend -- this means that the build process needs to know this from build data,
86
97
# essentially from an incoming webhook call.
87
98
if self .version_type in (BRANCH , TAG ):
88
- return self . version_identifier
99
+ return identifier
89
100
if self .version_type == EXTERNAL :
101
+
102
+ # TODO: We should be able to resolve this without looking up in oauth registry
90
103
git_provider_name = self .project .git_provider_name
104
+
105
+ # TODO: Why are these our only patterns?
91
106
if git_provider_name == GITHUB_BRAND :
92
107
return GITHUB_PR_PULL_PATTERN .format (id = self .verbose_name )
93
108
if self .project .git_provider_name == GITLAB_BRAND :
94
- return GITLAB_MR_PULL_PATTERN
109
+ return GITLAB_MR_PULL_PATTERN . format ( id = self . verbose_name )
95
110
96
111
# This seems to be the default behavior when we don't know the remote
97
112
# reference for a PR/MR: Just fetch everything
113
+ # TODO: Provide more information about fetch operations without references
98
114
return None
99
115
100
116
def clone_ng (self ):
101
117
# If the repository is already cloned, we don't do anything.
102
- # TODO: Why is it already cloned?
118
+ # This is likely legacy, but we may want to be able to call .update()
119
+ # several times in the same build
103
120
if self .repo_exists ():
104
121
return
105
122
106
123
# --no-checkout: Makes it explicit what we are doing here. Nothing is checked out
107
- # until it's explcitly done.
124
+ # until it's explicitly done.
108
125
# --depth 1: Shallow clone, fetch as little data as possible.
109
- cmd = ["git" , "clone" , "--no-checkout" , "--depth" , "1" ]
126
+ cmd = ["git" , "clone" , "--no-checkout" , "--depth" , "1" , self . repo_url , "." ]
110
127
111
128
code , stdout , stderr = self .run (* cmd )
112
129
return code , stdout , stderr
113
130
114
- def fetch_ng (self ):
131
+ def fetch_ng (self , identifier ):
115
132
"""Implementation for new clone+fetch+checkout pattern."""
116
133
117
- # --force: ?
134
+ # --force: Likely legacy, it seems to be irrelevant to this usage
118
135
# --tags: We need to fetch tags in order to resolve these references in the checkout
119
- # --prune: ?
120
- # --prune-tags: ?
136
+ # --prune: Likely legacy, we don't expect a previous fetch command to have run
137
+ # --prune-tags: Likely legacy, we don't expect a previous fetch command to have run
138
+ # --tags: This flag was used in the previous approach such that all tags were fetched
139
+ # in order to checkout a tag afterwards.
121
140
cmd = ["git" , "fetch" , "origin" , "--force" , "--tags" , "--prune" , "--prune-tags" ]
122
- remote_reference = self .get_remote_reference ( )
141
+ remote_reference = self .get_remote_fetch_reference ( identifier )
123
142
124
143
if remote_reference :
125
- # TODO: If we are fetching just one reference, what should the depth be?
144
+ # TODO: We are still fetching the latest 50 commits.
126
145
# A PR might have another commit added after the build has started...
127
- cmd .append ("--depth 1" )
128
- cmd .append (remote_reference )
146
+ cmd .extend (["--depth" , self .repo_depth , remote_reference ])
129
147
130
148
code , stdout , stderr = self .run (* cmd )
131
149
return code , stdout , stderr
0 commit comments