@@ -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 ()
0 commit comments