Skip to content

Commit

Permalink
Do not raise if branch exists and no write permission (#2426)
Browse files Browse the repository at this point in the history
* Do not raise if branch exists and no write permission

* Update src/huggingface_hub/hf_api.py

Co-authored-by: Julien Chaumond <julien@huggingface.co>

---------

Co-authored-by: Julien Chaumond <julien@huggingface.co>
  • Loading branch information
Wauplin and julien-c authored Jul 30, 2024
1 parent 0d925c2 commit d432d2a
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5760,8 +5760,18 @@ def create_branch(
try:
hf_raise_for_status(response)
except HfHubHTTPError as e:
if not (e.response.status_code == 409 and exist_ok):
raise
if exist_ok and e.response.status_code == 409:
return
elif exist_ok and e.response.status_code == 403:
# No write permission on the namespace but branch might already exist
try:
refs = self.list_repo_refs(repo_id=repo_id, repo_type=repo_type, token=token)
for branch_ref in refs.branches:
if branch_ref.name == branch:
return # Branch already exists => do not raise
except HfHubHTTPError:
pass # We raise the original error if the branch does not exist
raise

@validate_hf_hub_args
def delete_branch(
Expand Down

0 comments on commit d432d2a

Please sign in to comment.