Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Commit 2657651

Browse files
Merge pull request #20 from lumicks/19-add-missing-branch-arguments
Add remote and all options to branch
2 parents 9a49d4b + 8ec52eb commit 2657651

File tree

2 files changed

+60
-17
lines changed

2 files changed

+60
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG](https://kee
1616

1717
### Added
1818
- Add CODEOWNERS file. [#24](https://github.com/lumicks/doltcli/pull/24)
19+
- Add 'remote' and 'all' options to branch command. [#20](https://github.com/lumicks/doltcli/pull/20)
1920

2021
### Changed
2122
- Update tests to work with dolt 1.15.0. [#22](https://github.com/lumicks/doltcli/pull/22)

doltcli/dolt.py

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -711,18 +711,35 @@ def branch(
711711
delete: bool = False,
712712
copy: bool = False,
713713
move: bool = False,
714+
remote: bool = False,
715+
all: bool = False,
714716
**kwargs,
715717
):
716718
"""
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
726743
"""
727744
switch_count = [el for el in [delete, copy, move] if el]
728745
if len(switch_count) > 1:
@@ -733,7 +750,7 @@ def branch(
733750
raise ValueError(
734751
"force is not valid without providing a new branch name, or copy, move, or delete being true"
735752
)
736-
return self._get_branches()
753+
return self._get_branches(remote=remote, all=all)
737754

738755
args = ["branch"]
739756
if force:
@@ -762,6 +779,8 @@ def execute_wrapper(command_args: List[str]):
762779
if not branch_name:
763780
raise ValueError("must provide branch_name when deleting")
764781
args.extend(["--delete", branch_name])
782+
if remote:
783+
args.append("--remote")
765784
return execute_wrapper(args)
766785

767786
if move:
@@ -779,25 +798,48 @@ def execute_wrapper(command_args: List[str]):
779798
args.append(start_point)
780799
return execute_wrapper(args)
781800

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
783825

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
787827
ab_dicts = read_rows_sql(
788828
self, "select * from dolt_branches where name = (select active_branch())"
789829
)
790-
791830
if len(ab_dicts) != 1:
792831
raise ValueError(
793832
"Ensure you have the latest version of Dolt installed, this is fixed as of 0.24.2"
794833
)
795-
796834
active_branch = Branch(**ab_dicts[0])
797-
798835
if not active_branch:
799836
raise DoltException("Failed to set active branch")
800837

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+
801843
return active_branch, branches
802844

803845
def checkout(

0 commit comments

Comments
 (0)