Skip to content

Commit

Permalink
Fix creating empty commit on PR (#2413)
Browse files Browse the repository at this point in the history
* Fix creating empty commit on PR

* unquoted revision for fetching files to copy as well
  • Loading branch information
Wauplin committed Jul 24, 2024
1 parent 1dd68a9 commit 75af085
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3801,7 +3801,7 @@ def create_commit(

# Get latest commit info
try:
info = self.repo_info(repo_id=repo_id, repo_type=repo_type, revision=revision, token=token)
info = self.repo_info(repo_id=repo_id, repo_type=repo_type, revision=unquoted_revision, token=token)
except RepositoryNotFoundError as e:
e.append_to_message(_CREATE_COMMIT_NO_REPO_ERROR_MESSAGE)
raise
Expand All @@ -3822,7 +3822,7 @@ def create_commit(
repo_type=repo_type,
repo_id=repo_id,
headers=headers,
revision=revision,
revision=unquoted_revision,
endpoint=self.endpoint,
)
commit_payload = _prepare_commit_payload(
Expand Down
21 changes: 21 additions & 0 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,27 @@ def test_prevent_empty_commit_if_no_new_addition(self, repo_url: RepoUrl) -> Non
)
assert logs.records[1].levelname == "WARNING"

@use_tmp_repo()
def test_empty_commit_on_pr(self, repo_url: RepoUrl) -> None:
"""
Regression test for #2411. Revision was quoted twice, leading to a HTTP 404.
See https://github.com/huggingface/huggingface_hub/issues/2411.
"""
pr = self._api.create_pull_request(repo_id=repo_url.repo_id, title="Test PR")

with self.assertLogs("huggingface_hub", level="WARNING"):
url = self._api.create_commit(
repo_id=repo_url.repo_id,
operations=[],
commit_message="Empty commit",
revision=pr.git_reference,
)

commits = self._api.list_repo_commits(repo_id=repo_url.repo_id, revision=pr.git_reference)
assert len(commits) == 1 # no 2nd commit
assert url.oid == commits[0].commit_id

@use_tmp_repo()
def test_continue_commit_without_existing_files(self, repo_url: RepoUrl) -> None:
self._api.create_commit(
Expand Down

0 comments on commit 75af085

Please sign in to comment.