@@ -2425,6 +2425,103 @@ def delete_folder(
24252425 parent_commit = parent_commit ,
24262426 )
24272427
2428+ @validate_hf_hub_args
2429+ def create_branch (
2430+ self ,
2431+ repo_id : str ,
2432+ * ,
2433+ branch : str ,
2434+ token : Optional [str ] = None ,
2435+ repo_type : Optional [str ] = None ,
2436+ ) -> None :
2437+ """
2438+ Create a new branch from `main` on a repo on the Hub.
2439+
2440+ Args:
2441+ repo_id (`str`):
2442+ The repository in which the branch will be created.
2443+ Example: `"user/my-cool-model"`.
2444+
2445+ branch (`str`):
2446+ The name of the branch to create.
2447+
2448+ token (`str`, *optional*):
2449+ Authentication token. Will default to the stored token.
2450+
2451+ repo_type (`str`, *optional*):
2452+ Set to `"dataset"` or `"space"` if creating a branch on a dataset or
2453+ space, `None` or `"model"` if tagging a model. Default is `None`.
2454+
2455+ Raises:
2456+ [`~utils.RepositoryNotFoundError`]:
2457+ If repository is not found (error 404): wrong repo_id/repo_type, private
2458+ but not authenticated or repo does not exist.
2459+ [`~utils.BadRequestError`]:
2460+ If invalid reference for a branch. Ex: `refs/pr/5` or 'refs/foo/bar'.
2461+ [`~utils.HfHubHTTPError`]:
2462+ If the branch already exists on the repo (error 409).
2463+ """
2464+ if repo_type is None :
2465+ repo_type = REPO_TYPE_MODEL
2466+ branch = quote (branch , safe = "" )
2467+
2468+ # Prepare request
2469+ branch_url = f"{ self .endpoint } /api/{ repo_type } s/{ repo_id } /branch/{ branch } "
2470+ headers = self ._build_hf_headers (token = token , is_write_action = True )
2471+
2472+ # Create branch
2473+ response = requests .post (url = branch_url , headers = headers )
2474+ hf_raise_for_status (response )
2475+
2476+ @validate_hf_hub_args
2477+ def delete_branch (
2478+ self ,
2479+ repo_id : str ,
2480+ * ,
2481+ branch : str ,
2482+ token : Optional [str ] = None ,
2483+ repo_type : Optional [str ] = None ,
2484+ ) -> None :
2485+ """
2486+ Delete a branch from a repo on the Hub.
2487+
2488+ Args:
2489+ repo_id (`str`):
2490+ The repository in which a branch will be deleted.
2491+ Example: `"user/my-cool-model"`.
2492+
2493+ branch (`str`):
2494+ The name of the branch to delete.
2495+
2496+ token (`str`, *optional*):
2497+ Authentication token. Will default to the stored token.
2498+
2499+ repo_type (`str`, *optional*):
2500+ Set to `"dataset"` or `"space"` if creating a branch on a dataset or
2501+ space, `None` or `"model"` if tagging a model. Default is `None`.
2502+
2503+ Raises:
2504+ [`~utils.RepositoryNotFoundError`]:
2505+ If repository is not found (error 404): wrong repo_id/repo_type, private
2506+ but not authenticated or repo does not exist.
2507+ [`~utils.HfHubHTTPError`]:
2508+ If trying to delete a protected branch. Ex: `main` cannot be deleted.
2509+ [`~utils.HfHubHTTPError`]:
2510+ If trying to delete a branch that does not exist.
2511+
2512+ """
2513+ if repo_type is None :
2514+ repo_type = REPO_TYPE_MODEL
2515+ branch = quote (branch , safe = "" )
2516+
2517+ # Prepare request
2518+ branch_url = f"{ self .endpoint } /api/{ repo_type } s/{ repo_id } /branch/{ branch } "
2519+ headers = self ._build_hf_headers (token = token , is_write_action = True )
2520+
2521+ # Delete branch
2522+ response = requests .delete (url = branch_url , headers = headers )
2523+ hf_raise_for_status (response )
2524+
24282525 @validate_hf_hub_args
24292526 def create_tag (
24302527 self ,
@@ -2501,7 +2598,7 @@ def delete_tag(
25012598
25022599 Args:
25032600 repo_id (`str`):
2504- The repository in which a commit will be deleted.
2601+ The repository in which a tag will be deleted.
25052602 Example: `"user/my-cool-model"`.
25062603
25072604 tag (`str`):
@@ -2511,20 +2608,19 @@ def delete_tag(
25112608 Authentication token. Will default to the stored token.
25122609
25132610 repo_type (`str`, *optional*):
2514- Set to `"dataset"` or `"space"` if tagging a dataset or
2515- space, `None` or `"model"` if tagging a model. Default is
2516- `None`.
2611+ Set to `"dataset"` or `"space"` if tagging a dataset or space, `None` or
2612+ `"model"` if tagging a model. Default is `None`.
25172613
25182614 Raises:
25192615 [`~utils.RepositoryNotFoundError`]:
25202616 If repository is not found (error 404): wrong repo_id/repo_type, private
25212617 but not authenticated or repo does not exist.
25222618 [`~utils.RevisionNotFoundError`]:
25232619 If tag is not found.
2524-
25252620 """
25262621 if repo_type is None :
25272622 repo_type = REPO_TYPE_MODEL
2623+ tag = quote (tag , safe = "" )
25282624
25292625 # Prepare request
25302626 tag_url = f"{ self .endpoint } /api/{ repo_type } s/{ repo_id } /tag/{ tag } "
@@ -3443,6 +3539,8 @@ def _warn_if_truncated(
34433539upload_folder = api .upload_folder
34443540delete_file = api .delete_file
34453541delete_folder = api .delete_folder
3542+ create_branch = api .create_branch
3543+ delete_branch = api .delete_branch
34463544create_tag = api .create_tag
34473545delete_tag = api .delete_tag
34483546get_full_repo_name = api .get_full_repo_name
0 commit comments