Skip to content

Commit 35418de

Browse files
authored
Helper: when pushing, return commit url (#20)
cc @philschmid
1 parent 6e3568d commit 35418de

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/huggingface_hub/repository.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,52 @@ def git_config_username_and_email(
222222
except subprocess.CalledProcessError as exc:
223223
raise EnvironmentError(exc.stderr)
224224

225+
def git_head_hash(self) -> str:
226+
"""
227+
Get commit sha on top of HEAD.
228+
"""
229+
try:
230+
p = subprocess.run(
231+
"git rev-parse HEAD".split(),
232+
stderr=subprocess.PIPE,
233+
stdout=subprocess.PIPE,
234+
encoding="utf-8",
235+
check=True,
236+
cwd=self.local_dir,
237+
)
238+
return p.stdout.strip()
239+
except subprocess.CalledProcessError as exc:
240+
raise EnvironmentError(exc.stderr)
241+
242+
def git_remote_url(self) -> str:
243+
"""
244+
Get URL to origin remote.
245+
"""
246+
try:
247+
p = subprocess.run(
248+
"git config --get remote.origin.url".split(),
249+
stderr=subprocess.PIPE,
250+
stdout=subprocess.PIPE,
251+
encoding="utf-8",
252+
check=True,
253+
cwd=self.local_dir,
254+
)
255+
return p.stdout.strip()
256+
except subprocess.CalledProcessError as exc:
257+
raise EnvironmentError(exc.stderr)
258+
259+
def git_head_commit_url(self) -> str:
260+
"""
261+
Get URL to last commit on HEAD
262+
We assume it's been pushed, and the url scheme is
263+
the same one as for GitHub or HuggingFace.
264+
"""
265+
sha = self.git_head_hash()
266+
url = self.git_remote_url()
267+
if url.endswith("/"):
268+
url = url[:-1]
269+
return f"{url}/commit/{sha}"
270+
225271
def lfs_track(self, patterns: Union[str, List[str]]):
226272
"""
227273
Tell git-lfs to track those files.
@@ -319,9 +365,11 @@ def git_commit(self, commit_message="commit files to HF hub"):
319365
else:
320366
raise EnvironmentError(exc.stdout)
321367

322-
def git_push(self):
368+
def git_push(self) -> str:
323369
"""
324370
git push
371+
372+
Returns url to commit on remote repo.
325373
"""
326374
try:
327375
result = subprocess.run(
@@ -336,12 +384,14 @@ def git_push(self):
336384
except subprocess.CalledProcessError as exc:
337385
raise EnvironmentError(exc.stderr)
338386

339-
def push_to_hub(self, commit_message="commit files to HF hub"):
387+
return self.git_head_commit_url()
388+
389+
def push_to_hub(self, commit_message="commit files to HF hub") -> str:
340390
"""
341391
Helper to add, commit, and pushe file to remote repository on the HuggingFace Hub.
342392
Args:
343393
commit_message: commit message.
344394
"""
345395
self.git_add()
346396
self.git_commit(commit_message)
347-
self.git_push()
397+
return self.git_push()

tests/test_repository.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import time
2020
import unittest
2121

22+
import requests
2223
from huggingface_hub.hf_api import HfApi
2324
from huggingface_hub.repository import Repository
2425

@@ -110,7 +111,11 @@ def test_add_commit_push(self):
110111
repo.git_add()
111112
repo.git_commit()
112113
try:
113-
repo.git_push()
114+
url = repo.git_push()
114115
except subprocess.CalledProcessError as exc:
115116
print(exc.stderr)
116117
raise exc
118+
# Check that the returned commit url
119+
# actually exists.
120+
r = requests.head(url)
121+
r.raise_for_status()

0 commit comments

Comments
 (0)