1616import re
1717import subprocess
1818import warnings
19+ from dataclasses import dataclass , field
1920from typing import BinaryIO , Dict , Iterable , Iterator , List , Optional , Tuple , Union
2021from urllib .parse import quote
2122
@@ -171,6 +172,62 @@ class BlobLfsInfo(TypedDict, total=False):
171172 sha256 : str
172173
173174
175+ @dataclass
176+ class CommitInfo :
177+ """Data structure containing information about a newly created commit.
178+
179+ Returned by [`create_commit`].
180+
181+ Args:
182+ commit_url (`str`):
183+ Url where to find the commit.
184+
185+ commit_message (`str`):
186+ The summary (first line) of the commit that has been created.
187+
188+ commit_description (`str`):
189+ Description of the commit that has been created. Can be empty.
190+
191+ oid (`str`):
192+ Commit hash id. Example: `"91c54ad1727ee830252e457677f467be0bfd8a57"`.
193+
194+ pr_url (`str`, *optional*):
195+ Url to the PR that has been created, if any. Populated when `create_pr=True`
196+ is passed.
197+
198+ pr_revision (`str`, *optional*):
199+ Revision of the PR that has been created, if any. Populated when
200+ `create_pr=True` is passed. Example: `"refs/pr/1"`.
201+
202+ pr_num (`int`, *optional*):
203+ Number of the PR discussion that has been created, if any. Populated when
204+ `create_pr=True` is passed. Can be passed as `discussion_num` in
205+ [`get_discussion_details`]. Example: `1`.
206+ """
207+
208+ commit_url : str
209+ commit_message : str
210+ commit_description : str
211+ oid : str
212+ pr_url : Optional [str ] = None
213+
214+ # Computed from `pr_url` in `__post_init__`
215+ pr_revision : Optional [str ] = field (init = False )
216+ pr_num : Optional [str ] = field (init = False )
217+
218+ def __post_init__ (self ):
219+ """Populate pr-related fields after initialization.
220+
221+ See https://docs.python.org/3.10/library/dataclasses.html#post-init-processing.
222+ """
223+ if self .pr_url is not None :
224+ self .pr_revision = _parse_revision_from_pr_url (self .pr_url )
225+ self .pr_num = int (self .pr_revision .split ("/" )[- 1 ])
226+ else :
227+ self .pr_revision = None
228+ self .pr_num = None
229+
230+
174231class RepoFile :
175232 """
176233 Data structure that represents a public file inside a repo, accessible from
@@ -1850,7 +1907,7 @@ def create_commit(
18501907 create_pr : Optional [bool ] = None ,
18511908 num_threads : int = 5 ,
18521909 parent_commit : Optional [str ] = None ,
1853- ) -> Optional [ str ] :
1910+ ) -> CommitInfo :
18541911 """
18551912 Creates a commit in the given repo, deleting & uploading files as needed.
18561913
@@ -1902,9 +1959,9 @@ def create_commit(
19021959 if the repo is updated / committed to concurrently.
19031960
19041961 Returns:
1905- `str` or `None` :
1906- If `create_pr` is `True`, returns the URL to the newly created Pull Request
1907- on the Hub. Otherwise returns `None` .
1962+ [`CommitInfo`] :
1963+ Instance of [`CommitInfo`] containing information about the newly
1964+ created commit (commit hash, commit url, pr url, commit message,...) .
19081965
19091966 Raises:
19101967 [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
@@ -2015,7 +2072,14 @@ def create_commit(
20152072 params = {"create_pr" : "1" } if create_pr else None ,
20162073 )
20172074 hf_raise_for_status (commit_resp , endpoint_name = "commit" )
2018- return commit_resp .json ().get ("pullRequestUrl" , None )
2075+ commit_data = commit_resp .json ()
2076+ return CommitInfo (
2077+ commit_url = commit_data ["commitUrl" ],
2078+ commit_message = commit_message ,
2079+ commit_description = commit_description ,
2080+ oid = commit_data ["commitOid" ],
2081+ pr_url = commit_data ["pullRequestUrl" ] if create_pr else None ,
2082+ )
20192083
20202084 @validate_hf_hub_args
20212085 def upload_file (
@@ -2157,7 +2221,7 @@ def upload_file(
21572221 path_in_repo = path_in_repo ,
21582222 )
21592223
2160- pr_url = self .create_commit (
2224+ commit_info = self .create_commit (
21612225 repo_id = repo_id ,
21622226 repo_type = repo_type ,
21632227 operations = [operation ],
@@ -2169,8 +2233,8 @@ def upload_file(
21692233 parent_commit = parent_commit ,
21702234 )
21712235
2172- if pr_url is not None :
2173- revision = quote (_parse_revision_from_pr_url (pr_url ), safe = "" )
2236+ if commit_info . pr_url is not None :
2237+ revision = quote (_parse_revision_from_pr_url (commit_info . pr_url ), safe = "" )
21742238 if repo_type in REPO_TYPES_URL_PREFIXES :
21752239 repo_id = REPO_TYPES_URL_PREFIXES [repo_type ] + repo_id
21762240 revision = revision if revision is not None else DEFAULT_REVISION
@@ -2317,7 +2381,7 @@ def upload_folder(
23172381 ignore_patterns = ignore_patterns ,
23182382 )
23192383
2320- pr_url = self .create_commit (
2384+ commit_info = self .create_commit (
23212385 repo_type = repo_type ,
23222386 repo_id = repo_id ,
23232387 operations = files_to_add ,
@@ -2329,8 +2393,8 @@ def upload_folder(
23292393 parent_commit = parent_commit ,
23302394 )
23312395
2332- if pr_url is not None :
2333- revision = quote (_parse_revision_from_pr_url (pr_url ), safe = "" )
2396+ if commit_info . pr_url is not None :
2397+ revision = quote (_parse_revision_from_pr_url (commit_info . pr_url ), safe = "" )
23342398 if repo_type in REPO_TYPES_URL_PREFIXES :
23352399 repo_id = REPO_TYPES_URL_PREFIXES [repo_type ] + repo_id
23362400 revision = revision if revision is not None else DEFAULT_REVISION
@@ -2350,7 +2414,7 @@ def delete_file(
23502414 commit_description : Optional [str ] = None ,
23512415 create_pr : Optional [bool ] = None ,
23522416 parent_commit : Optional [str ] = None ,
2353- ):
2417+ ) -> CommitInfo :
23542418 """
23552419 Deletes a file in the given repo.
23562420
0 commit comments