Skip to content

Commit d28211c

Browse files
authored
Merge pull request #252 from victormlg/use-upstream
ENT-14725: Checkout upstream instead of local branch when generating dependency tables
2 parents a55a133 + 2174c37 commit d28211c

1 file changed

Lines changed: 43 additions & 4 deletions

File tree

‎src/cfengine_cli/deptool.py‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ def __init__(
109109
a ref from previous work might be left checked out.
110110
"""
111111
self.repo_path = repo_path
112+
self.repo_owner = repo_owner
113+
self.repo_name = repo_name
112114

113115
repo_url = "https://github.com/{}/{}.git".format(repo_owner, repo_name)
114116

@@ -211,6 +213,36 @@ def is_git_branch(self, ref):
211213
return False
212214
return True
213215

216+
def upstream_remote(self):
217+
result = self.run_command("remote", "-v", capture_output=True)
218+
upstream = "github\\.com[:/]{}/{}(\\.git)?/?$".format(
219+
re.escape(self.repo_owner), re.escape(self.repo_name)
220+
)
221+
for line in result.stdout.splitlines():
222+
parts = line.split()
223+
# GitHub owner and repository names are case-insensitive
224+
if len(parts) >= 2 and re.search(upstream, parts[1], re.IGNORECASE):
225+
return parts[0]
226+
return None
227+
228+
def upstream_branch(self, ref):
229+
remote = self.upstream_remote()
230+
if remote is None:
231+
log.warning(
232+
"No remote points to github.com/{}/{}, using local refs".format(
233+
self.repo_owner, self.repo_name
234+
)
235+
)
236+
return None
237+
remote_ref = "{}/{}".format(remote, ref)
238+
try:
239+
self.run_command(
240+
"show-ref", "--verify", "refs/remotes/{}".format(remote_ref)
241+
)
242+
except subprocess.CalledProcessError:
243+
return None
244+
return remote_ref
245+
214246

215247
def pretty(data):
216248
return json.dumps(data, indent=2)
@@ -311,10 +343,17 @@ def deps_versions(self, ref):
311343
```
312344
The ref is checked out during the execution of this function.
313345
"""
314-
self.buildscripts_repo.checkout(ref)
315-
# also support checking out refs that are not necessarily branches, such as tags
316-
if self.buildscripts_repo.is_git_branch(ref):
317-
self.buildscripts_repo.run_command("pull")
346+
upstream_ref = self.buildscripts_repo.upstream_branch(ref)
347+
if upstream_ref is not None:
348+
self.buildscripts_repo.checkout(upstream_ref)
349+
else:
350+
if self.buildscripts_repo.is_git_branch(ref):
351+
log.warning(
352+
"Branch '{}' not found in upstream remote, using local branch".format(
353+
ref
354+
)
355+
)
356+
self.buildscripts_repo.checkout(ref)
318357

319358
deps_versions = {}
320359
deps_list = self.deps_list(ref)

0 commit comments

Comments
 (0)