Skip to content

Commit d255704

Browse files
committed
Use askpass over embeddign the token in the URL
1 parent 844b964 commit d255704

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

llvm/utils/llvm_push_pr.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def run_command(
7575
text: bool = False,
7676
stdin_input: Optional[str] = None,
7777
read_only: bool = False,
78+
env: Optional[dict] = None,
7879
) -> subprocess.CompletedProcess:
7980
if self.dry_run and not read_only:
8081
self.print(f"[Dry Run] Would run: {' '.join(command)}")
@@ -89,6 +90,7 @@ def run_command(
8990
capture_output=capture_output,
9091
text=text,
9192
input=stdin_input,
93+
env=env,
9294
)
9395
except FileNotFoundError as e:
9496
raise LlvmPrError(
@@ -329,6 +331,9 @@ def __init__(
329331
self.original_branch: str = ""
330332
self.created_branches: List[str] = []
331333
self.repo_settings: dict = {}
334+
self._git_askpass_cmd = (
335+
f"python3 -c \"import os; print(os.environ['{LLVM_GITHUB_TOKEN_VAR}'])\""
336+
)
332337

333338
def _run_cmd(
334339
self, command: List[str], read_only: bool = False, **kwargs
@@ -364,17 +369,18 @@ def _rebase_current_branch(self) -> None:
364369
f"Fetching from '{self.config.upstream_remote}' and rebasing '{self.original_branch}' on top of '{target}'..."
365370
)
366371

367-
authenticated_url = self._get_authenticated_remote_url(
368-
self.config.upstream_remote
372+
git_env = os.environ.copy()
373+
git_env["GIT_ASKPASS"] = self._git_askpass_cmd
374+
git_env[LLVM_GITHUB_TOKEN_VAR] = self.config.token
375+
git_env["GIT_TERMINAL_PROMPT"] = "0"
376+
377+
self._run_cmd(
378+
["git", "fetch", self.config.upstream_remote, self.config.base_branch],
379+
env=git_env,
369380
)
370-
# Use a refspec to explicitly update the local remote-tracking branch (e.g., origin/main)
371-
# when fetching from an authenticated URL. This ensures that 'git rebase origin/main'
372-
# operates on the most up-to-date remote state.
373-
refspec = f"refs/heads/{self.config.base_branch}:refs/remotes/{self.config.upstream_remote}/{self.config.base_branch}"
374-
self._run_cmd(["git", "fetch", authenticated_url, refspec])
375381

376382
try:
377-
self._run_cmd(["git", "rebase", target])
383+
self._run_cmd(["git", "rebase", target], env=git_env)
378384
except subprocess.CalledProcessError as e:
379385
self.runner.print(
380386
"Error: The rebase operation failed, likely due to a merge conflict.",
@@ -392,12 +398,13 @@ def _rebase_current_branch(self) -> None:
392398
capture_output=True,
393399
text=True,
394400
read_only=True,
401+
env=git_env,
395402
)
396403

397404
# REBASE_HEAD exists, so rebase is in progress
398405
if rebase_status_result.returncode == 0:
399406
self.runner.print("Aborting rebase...", file=sys.stderr)
400-
self._run_cmd(["git", "rebase", "--abort"], check=False)
407+
self._run_cmd(["git", "rebase", "--abort"], check=False, env=git_env)
401408
raise LlvmPrError("rebase operation failed.") from e
402409

403410
def _get_authenticated_remote_url(self, remote_name: str) -> str:
@@ -481,14 +488,18 @@ def _create_and_push_branch_for_commit(
481488
self.runner.print(f"Processing commit {commit_hash[:7]}: {commit_title}")
482489
self.runner.print(f"Pushing commit to temporary branch '{branch_name}'")
483490

484-
push_url = self._get_authenticated_remote_url(self.remote)
491+
git_env = os.environ.copy()
492+
git_env["GIT_ASKPASS"] = self._git_askpass_cmd
493+
git_env[LLVM_GITHUB_TOKEN_VAR] = self.config.token
494+
git_env["GIT_TERMINAL_PROMPT"] = "0"
495+
485496
push_command = [
486497
"git",
487498
"push",
488-
push_url,
499+
self.remote,
489500
f"{commit_hash}:refs/heads/{branch_name}",
490501
]
491-
self._run_cmd(push_command)
502+
self._run_cmd(push_command, env=git_env)
492503
self.created_branches.append(branch_name)
493504
return branch_name
494505

0 commit comments

Comments
 (0)