-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for private repositories (#10)
using token which is not stored in `.git`
- Loading branch information
1 parent
2324e31
commit 57a8b07
Showing
6 changed files
with
146 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env python | ||
|
||
import os | ||
import sys | ||
|
||
what = sys.argv[1].lower() | ||
|
||
if "username" in what: | ||
print(os.environ["GIT_PULLER_ACCOUNT"]) | ||
if "password" in what: | ||
print(os.environ["GIT_PULLER_TOKEN"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
from subprocess import run | ||
from typing import Optional | ||
import re | ||
import os | ||
|
||
|
||
def extract_repository_owner(git_url: str) -> str: | ||
fragments = git_url.strip("/").split("/") | ||
return fragments[-2] if len(fragments) >= 2 else "" | ||
|
||
|
||
def extract_repository_name(git_url: str) -> str: | ||
fragment = git_url.split("/")[-1] | ||
if fragment.endswith(".git"): | ||
return fragment[:-4] | ||
return fragment | ||
|
||
|
||
def has_updates(repo_path: Path) -> bool: | ||
try: | ||
run( | ||
"git fetch origin $(git branch --show-current) --quiet", | ||
cwd=repo_path, | ||
shell=True, | ||
) | ||
result = run( | ||
"git status -b --porcelain -u n --ignored n", | ||
cwd=repo_path, | ||
capture_output=True, | ||
shell=True, | ||
) | ||
except FileNotFoundError: | ||
return False | ||
data = re.match( | ||
r"^## (.*?)( \[(ahead (?P<ahead>\d+))?(, )?(behind (?P<behind>\d+))?\])?$", | ||
result.stdout.decode("utf-8"), | ||
) | ||
if not data: | ||
return False | ||
return data["behind"] is not None | ||
|
||
|
||
@contextmanager | ||
def git_credentials(token: Optional[str], account: Optional[str]): | ||
if token and account: | ||
try: | ||
path = Path(__file__).parent | ||
os.environ["GIT_ASKPASS"] = str(path / "git_askpass.py") | ||
os.environ["GIT_PULLER_ACCOUNT"] = account | ||
os.environ["GIT_PULLER_TOKEN"] = token | ||
# do not prompt user if askpass fails as this would | ||
# dead lock execution! | ||
os.environ["GIT_TERMINAL_PROMPT"] = "0" | ||
yield | ||
finally: | ||
del os.environ["GIT_PULLER_ACCOUNT"] | ||
del os.environ["GIT_PULLER_TOKEN"] | ||
del os.environ["GIT_TERMINAL_PROMPT"] | ||
del os.environ["GIT_ASKPASS"] | ||
else: | ||
yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters