From d432d2ab0e9742f07064d8cbc2defe5c11832414 Mon Sep 17 00:00:00 2001 From: Lucain Date: Tue, 30 Jul 2024 09:26:39 +0200 Subject: [PATCH] Do not raise if branch exists and no write permission (#2426) * Do not raise if branch exists and no write permission * Update src/huggingface_hub/hf_api.py Co-authored-by: Julien Chaumond --------- Co-authored-by: Julien Chaumond --- src/huggingface_hub/hf_api.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/huggingface_hub/hf_api.py b/src/huggingface_hub/hf_api.py index bc323345be..932b8ddf69 100644 --- a/src/huggingface_hub/hf_api.py +++ b/src/huggingface_hub/hf_api.py @@ -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(