@@ -711,18 +711,35 @@ def branch(
711
711
delete : bool = False ,
712
712
copy : bool = False ,
713
713
move : bool = False ,
714
+ remote : bool = False ,
715
+ all : bool = False ,
714
716
** kwargs ,
715
717
):
716
718
"""
717
- Checkout, create, delete, move, or copy, a branch. Only
718
- :param branch_name:
719
- :param start_point:
720
- :param new_branch:
721
- :param force:
722
- :param delete:
723
- :param copy:
724
- :param move:
725
- :return:
719
+ List all branches and optionally checkout, create, delete, move, or copy, a branch.
720
+
721
+ If 'branch_name' is None, existing branches are listed, including remotely tracked branches
722
+ if 'remote' or 'all' are set. If 'branch_name' is provided, a new branch is created, checked
723
+ our, deleted, moved or copied.
724
+
725
+ :param branch_name: Name of branch to Checkout, create, delete, move, or copy.
726
+ :param start_point: A commit that a new branch should point at.
727
+ :param new_branch: Name of branch to copy to or rename to if 'copy' or 'move' is set.
728
+ :param force: Reset 'branch_name' to 'start_point', even if 'branch_name' exists already.
729
+ Without 'force', dolt branch refuses to change an existing branch. In combination with
730
+ 'delete', allow deleting the branch irrespective of its merged status. In
731
+ combination with 'move', allow renaming the branch even if the new branch name
732
+ already exists, the same applies for 'copy'.
733
+ :param delete: Delete a branch. The branch must be fully merged in its upstream branch.
734
+ :param copy: Create a copy of a branch.
735
+ :param move: Move/rename a branch. If 'new_branch' does not exist, 'branch_name' will be
736
+ renamed to 'new_branch'. If 'new_branch' exists, 'force' must be used to force the
737
+ rename to happen.
738
+ :param remote: When in list mode, show only remote tracked branches, unless 'all' is true.
739
+ When with -d, delete a remote tracking branch.
740
+ :param all: When in list mode, shows both local and remote tracked branches
741
+
742
+ :return: active_branch, branches
726
743
"""
727
744
switch_count = [el for el in [delete , copy , move ] if el ]
728
745
if len (switch_count ) > 1 :
@@ -733,7 +750,7 @@ def branch(
733
750
raise ValueError (
734
751
"force is not valid without providing a new branch name, or copy, move, or delete being true"
735
752
)
736
- return self ._get_branches ()
753
+ return self ._get_branches (remote = remote , all = all )
737
754
738
755
args = ["branch" ]
739
756
if force :
@@ -762,6 +779,8 @@ def execute_wrapper(command_args: List[str]):
762
779
if not branch_name :
763
780
raise ValueError ("must provide branch_name when deleting" )
764
781
args .extend (["--delete" , branch_name ])
782
+ if remote :
783
+ args .append ("--remote" )
765
784
return execute_wrapper (args )
766
785
767
786
if move :
@@ -779,25 +798,48 @@ def execute_wrapper(command_args: List[str]):
779
798
args .append (start_point )
780
799
return execute_wrapper (args )
781
800
782
- return self ._get_branches ()
801
+ return self ._get_branches (remote = remote , all = all )
802
+
803
+ def _get_branches (self , remote : bool = False , all : bool = False ) -> Tuple [Branch , List [Branch ]]:
804
+ """
805
+ Gets the branches for this repository, optionally including remote branches, and optionally
806
+ including all.
807
+
808
+ :param remote: include remotely tracked branches. If all is false and remote is true, only
809
+ remotely track branches are returned. If all is true both local and remote are included.
810
+ Default is False
811
+ :param all: include both local and remotely tracked branches. Default is False
812
+ :return: active_branch, branches
813
+ """
814
+ local_dicts = read_rows_sql (self , sql = "select * from dolt_branches" )
815
+ remote_tracked = []
816
+ dicts = []
817
+ if remote or all :
818
+ remote_tracked = read_rows_sql (self , sql = "select * from dolt_remote_branches" )
819
+ if all :
820
+ dicts = local_dicts + remote_tracked
821
+ elif remote :
822
+ dicts = remote_tracked
823
+ else :
824
+ dicts = local_dicts
783
825
784
- def _get_branches (self ) -> Tuple [Branch , List [Branch ]]:
785
- dicts = read_rows_sql (self , sql = "select * from dolt_branches" )
786
- branches = [Branch (** d ) for d in dicts ]
826
+ # find active_branch
787
827
ab_dicts = read_rows_sql (
788
828
self , "select * from dolt_branches where name = (select active_branch())"
789
829
)
790
-
791
830
if len (ab_dicts ) != 1 :
792
831
raise ValueError (
793
832
"Ensure you have the latest version of Dolt installed, this is fixed as of 0.24.2"
794
833
)
795
-
796
834
active_branch = Branch (** ab_dicts [0 ])
797
-
798
835
if not active_branch :
799
836
raise DoltException ("Failed to set active branch" )
800
837
838
+ if remote and not all :
839
+ branches = [Branch (** d ) for d in remote_tracked ]
840
+ else :
841
+ branches = [Branch (** d ) for d in dicts ]
842
+
801
843
return active_branch , branches
802
844
803
845
def checkout (
0 commit comments