Skip to content

Commit 3da58ce

Browse files
fix(#3577): implement python-based remote default parsing and strict typing
Replaces git symbolic-ref with git remote show origin parsed in Python to ensure accuracy while preventing command injection. Updates type hints. Signed-off-by: guptapratykshh <pratykshgupta9999@gmail.com>
1 parent c678903 commit 3da58ce

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

augur/tasks/git/util/facade_worker/facade_worker/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,15 @@ def insert_or_update_data(self, query, **bind_args)-> None:
257257
def inc_repos_processed(self):
258258
self.repos_processed += 1
259259

260-
def run_git_command(self, cmd: list, timeout: int, capture_output: bool = False, operation_description: str = None) -> tuple:
260+
def run_git_command(self, cmd: list[str], timeout: int, capture_output: bool = False, operation_description: str = None) -> tuple:
261261
"""
262262
Execute a git command with timeout handling.
263263
264264
This method provides a unified interface for running git commands with
265265
consistent timeout handling and error logging across all facade operations.
266266
267267
Args:
268-
cmd: The git command to execute as a list of arguments (e.g., ["git", "clone", url])
268+
cmd: The git command to execute
269269
timeout: Timeout in seconds
270270
capture_output: If True, capture stdout/stderr; if False, discard them
271271
operation_description: Human-readable description for error logging

augur/tasks/git/util/facade_worker/facade_worker/repofetch.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -340,24 +340,34 @@ def git_repo_updates(facade_helper, repo_git):
340340

341341
# session.log_activity('Verbose', f'remote default is {logremotedefault}.')
342342

343-
getremotedefault = ["git", "-C", absolute_path, "symbolic-ref", "refs/remotes/origin/HEAD"]
343+
getremotedefault = ["git", "-C", absolute_path, "remote", "show", "origin"]
344344

345-
return_code_remote, remotedefault = facade_helper.run_git_command(
345+
return_code_remote, output = facade_helper.run_git_command(
346346
getremotedefault,
347-
timeout=60,
347+
timeout=60, # 1 minute for remote query
348348
capture_output=True,
349349
operation_description='get remote default branch'
350350
)
351-
if return_code_remote == 0 and remotedefault:
352-
remotedefault = remotedefault.split('/')[-1]
351+
352+
remotedefault = ""
353+
if return_code_remote == 0 and output:
354+
for line in output.split('\n'):
355+
if "HEAD branch" in line:
356+
parts = line.split(":", 1)
357+
if len(parts) > 1:
358+
remotedefault = parts[1].strip()
359+
break
353360

354361
facade_helper.log_activity(
355362
'Verbose', f'remote default getting checked out is: {remotedefault}.')
356363

357-
checkout_cmd = ["git", "-C", absolute_path, "checkout", remotedefault]
364+
getremotedefault = ["git", "-C", absolute_path, "checkout", remotedefault]
365+
366+
facade_helper.log_activity(
367+
'Verbose', f"get remote default command is: \n \n git -C {absolute_path} checkout {remotedefault} \n \n ")
358368

359369
return_code_remote_default_again, _ = facade_helper.run_git_command(
360-
checkout_cmd,
370+
getremotedefault,
361371
timeout=600, # 10 minutes for git checkout
362372
capture_output=False,
363373
operation_description=f'git checkout {remotedefault}'
@@ -408,23 +418,30 @@ def git_repo_updates(facade_helper, repo_git):
408418

409419
# session.log_activity('Verbose', f'remote default is {logremotedefault}.')
410420

411-
getremotedefault = ["git", "-C", absolute_path, "symbolic-ref", "refs/remotes/origin/HEAD"]
421+
getremotedefault = ["git", "-C", absolute_path, "remote", "show", "origin"]
412422

413-
return_code_remote, remotedefault = facade_helper.run_git_command(
423+
return_code_remote, output = facade_helper.run_git_command(
414424
getremotedefault,
415-
timeout=60,
425+
timeout=60, # 1 minute for remote query
416426
capture_output=True,
417427
operation_description='get remote default branch'
418428
)
419-
if return_code_remote == 0 and remotedefault:
420-
remotedefault = remotedefault.split('/')[-1]
429+
430+
remotedefault = ""
431+
if return_code_remote == 0 and output:
432+
for line in output.split('\n'):
433+
if "HEAD branch" in line:
434+
parts = line.split(":", 1)
435+
if len(parts) > 1:
436+
remotedefault = parts[1].strip()
437+
break
421438

422439
try:
423440

424-
checkout_cmd = ["git", "-C", absolute_path, "checkout", remotedefault]
441+
getremotedefault = ["git", "-C", absolute_path, "checkout", remotedefault]
425442

426443
return_code_remote_default, _ = facade_helper.run_git_command(
427-
checkout_cmd,
444+
getremotedefault,
428445
timeout=600, # 10 minutes for git checkout
429446
capture_output=False,
430447
operation_description=f'git checkout {remotedefault}'
@@ -480,6 +497,8 @@ def git_repo_updates(facade_helper, repo_git):
480497
pass
481498

482499
cmdpull2 = ["git", "-C", absolute_path, "pull"]
500+
501+
print(cmdpull2)
483502
return_code, _ = facade_helper.run_git_command(
484503
cmdpull2,
485504
timeout=600, # 10 minutes for git pull

0 commit comments

Comments
 (0)