Skip to content

Commit

Permalink
Helper: when pushing, return commit url (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-c committed Mar 2, 2021
1 parent 6e3568d commit 35418de
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
56 changes: 53 additions & 3 deletions src/huggingface_hub/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,52 @@ def git_config_username_and_email(
except subprocess.CalledProcessError as exc:
raise EnvironmentError(exc.stderr)

def git_head_hash(self) -> str:
"""
Get commit sha on top of HEAD.
"""
try:
p = subprocess.run(
"git rev-parse HEAD".split(),
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
encoding="utf-8",
check=True,
cwd=self.local_dir,
)
return p.stdout.strip()
except subprocess.CalledProcessError as exc:
raise EnvironmentError(exc.stderr)

def git_remote_url(self) -> str:
"""
Get URL to origin remote.
"""
try:
p = subprocess.run(
"git config --get remote.origin.url".split(),
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
encoding="utf-8",
check=True,
cwd=self.local_dir,
)
return p.stdout.strip()
except subprocess.CalledProcessError as exc:
raise EnvironmentError(exc.stderr)

def git_head_commit_url(self) -> str:
"""
Get URL to last commit on HEAD
We assume it's been pushed, and the url scheme is
the same one as for GitHub or HuggingFace.
"""
sha = self.git_head_hash()
url = self.git_remote_url()
if url.endswith("/"):
url = url[:-1]
return f"{url}/commit/{sha}"

def lfs_track(self, patterns: Union[str, List[str]]):
"""
Tell git-lfs to track those files.
Expand Down Expand Up @@ -319,9 +365,11 @@ def git_commit(self, commit_message="commit files to HF hub"):
else:
raise EnvironmentError(exc.stdout)

def git_push(self):
def git_push(self) -> str:
"""
git push
Returns url to commit on remote repo.
"""
try:
result = subprocess.run(
Expand All @@ -336,12 +384,14 @@ def git_push(self):
except subprocess.CalledProcessError as exc:
raise EnvironmentError(exc.stderr)

def push_to_hub(self, commit_message="commit files to HF hub"):
return self.git_head_commit_url()

def push_to_hub(self, commit_message="commit files to HF hub") -> str:
"""
Helper to add, commit, and pushe file to remote repository on the HuggingFace Hub.
Args:
commit_message: commit message.
"""
self.git_add()
self.git_commit(commit_message)
self.git_push()
return self.git_push()
7 changes: 6 additions & 1 deletion tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import time
import unittest

import requests
from huggingface_hub.hf_api import HfApi
from huggingface_hub.repository import Repository

Expand Down Expand Up @@ -110,7 +111,11 @@ def test_add_commit_push(self):
repo.git_add()
repo.git_commit()
try:
repo.git_push()
url = repo.git_push()
except subprocess.CalledProcessError as exc:
print(exc.stderr)
raise exc
# Check that the returned commit url
# actually exists.
r = requests.head(url)
r.raise_for_status()

0 comments on commit 35418de

Please sign in to comment.