Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not raise if branch exists and no write permission #2426

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading