Skip to content

Commit

Permalink
Merge pull request #509 from JrGoodle/fork-output
Browse files Browse the repository at this point in the history
Better output for fork projects
  • Loading branch information
JrGoodle committed May 19, 2020
2 parents 66de815 + 90777f6 commit b30fda5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 41 deletions.
24 changes: 16 additions & 8 deletions src/clowder/cli/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ def add_branch_parser(subparsers: argparse._SubParsersAction) -> None: # noqa
arguments = [
(['projects'], dict(metavar='PROJECT', default='all', nargs='*', choices=CLOWDER_CONTROLLER.project_choices,
help=options_help_message(CLOWDER_CONTROLLER.project_names,
'projects and groups to show branches for'))),
(['--all', '-a'], dict(action='store_true', help='show local and remote branches')),
(['--remote', '-r'], dict(action='store_true', help='show remote branches'))
'projects and groups to show branches for')))
]

parser = subparsers.add_parser('branch', help='Display current branches')
add_parser_arguments(parser, arguments)

mutually_exclusive_arguments = [
(['--all', '-a'], dict(action='store_true', help='show local and remote branches')),
(['--remote', '-r'], dict(action='store_true', help='show remote branches'))
]
mutually_exclusive_group = parser.add_mutually_exclusive_group()
add_parser_arguments(mutually_exclusive_group, mutually_exclusive_arguments)

parser.set_defaults(func=branch)


Expand All @@ -43,13 +49,15 @@ def branch(args) -> None:
@print_clowder_repo_status
def _branch(args) -> None:
"""Clowder branch command private implementation"""
local = True
remote = False
if args.all:
local = True
if args.remote:
local = False
remote = True
elif args.remote:
elif args.all:
local = True
remote = True
else:
local = True
remote = False

projects = filter_projects(CLOWDER_CONTROLLER.projects, args.projects)
for project in projects:
Expand Down
1 change: 0 additions & 1 deletion src/clowder/cli/herd.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,4 @@ def _herd_impl(clowder: ClowderController, project_names: Tuple[str, ...], branc
projects = filter_projects(clowder.projects, project_names)
validate_projects(projects)
for project in projects:
print(project.status())
project.herd(branch=branch, tag=tag, depth=depth, rebase=rebase)
1 change: 0 additions & 1 deletion src/clowder/cli/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,4 @@ def _reset_impl(clowder: ClowderController, project_names: Tuple[str, ...], time
projects = filter_projects(clowder.projects, project_names)
validate_projects(projects)
for project in projects:
print(project.status())
project.reset(timestamp=timestamp)
44 changes: 16 additions & 28 deletions src/clowder/git/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,24 @@ def new_commits(self, upstream: bool = False) -> int:
index = 1 if upstream else 0
return int(str(rev_list_count).split()[index])

def print_branches(self, local: bool = False, remote: bool = False) -> None:
"""Print branches
def print_local_branches(self) -> None:
"""Print local git branches"""

:param bool local: Print local branches
:param bool remote: Print remote branches
"""
for branch in self.repo.git.branch().split('\n'):
if branch.startswith('* '):
print(f"* {colored(branch[2:], 'green')}")
else:
print(branch)

if local:
self._print_local_branches()
if remote:
self._print_remote_branches()
def print_remote_branches(self) -> None:
"""Print output if self._print_output is True"""

for branch in self.repo.git.branch('-r', '-l', f"{self.remote}*").split('\n'):
if ' -> ' in branch:
components = branch.split(' -> ')
print(f" {colored(components[0], 'red')} -> {components[1]}")
else:
print(colored(branch, 'red'))

def print_validation(self) -> None:
"""Print validation messages"""
Expand Down Expand Up @@ -537,25 +544,6 @@ def _print(self, val: str) -> None:
if self._print_output:
print(val)

def _print_local_branches(self) -> None:
"""Print local git branches"""

for branch in self.repo.git.branch().split('\n'):
if branch.startswith('* '):
print(f"* {colored(branch[2:], 'green')}")
else:
print(branch)

def _print_remote_branches(self) -> None:
"""Print output if self._print_output is True"""

for branch in self.repo.git.branch('-r').split('\n'):
if ' -> ' in branch:
components = branch.split(' -> ')
print(f" {colored(components[0], 'red')} -> {components[1]}")
else:
print(colored(branch), 'red')

def _repo(self) -> Repo:
"""Create Repo instance for self.repo_path
Expand Down
31 changes: 28 additions & 3 deletions src/clowder/model/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def branch(self, local: bool = False, remote: bool = False) -> None:
"""

repo = ProjectRepo(self.full_path(), self.remote, self.ref)

# TODO: Rethink aggressively fetching for printing remote branches
# if not is_offline() and remote:
# if self.fork is None:
Expand All @@ -132,7 +133,30 @@ def branch(self, local: bool = False, remote: bool = False) -> None:
# repo.fetch(self.fork.remote)
# repo.fetch(self.remote)

repo.print_branches(local=local, remote=remote)
if self.fork is None:
if local:
repo.print_local_branches()

if remote:
repo.print_remote_branches()
return

if local:
repo.print_local_branches()

if remote:
self._print(fmt.fork_string(self.fork.name))
# Modify repo to prefer fork
repo.default_ref = self.fork.ref
repo.remote = self.fork.remote
repo.print_remote_branches()

self._print(fmt.fork_string(self.name))
# Restore repo configuration
repo.default_ref = self.ref
repo.remote = self.remote
repo.print_remote_branches()


@project_repo_exists
def checkout(self, branch: str) -> None:
Expand Down Expand Up @@ -305,6 +329,7 @@ def herd(self, branch: Optional[str] = None, tag: Optional[str] = None, depth: O
repo = self._repo(self.recursive, parallel=parallel)

if self.fork is None:
self._print(self.status())
if branch:
repo.herd_branch(self._url(), branch, depth=herd_depth, rebase=rebase)
elif tag:
Expand All @@ -316,7 +341,7 @@ def herd(self, branch: Optional[str] = None, tag: Optional[str] = None, depth: O
self._print(self.fork.status())
repo.configure_remotes(self.remote, self._url(), self.fork.remote, self.fork.url())

self._print(fmt.fork_string(self.name))
self._print(fmt.fork_string(self.fork.name))
# Modify repo to prefer fork
repo.default_ref = self.fork.ref
repo.remote = self.fork.remote
Expand Down Expand Up @@ -412,7 +437,7 @@ def reset(self, timestamp: Optional[str] = None, parallel: bool = False) -> None
self._print(self.fork.status())
repo.configure_remotes(self.remote, self._url(), self.fork.remote, self.fork.url())

self._print(fmt.fork_string(self.name))
self._print(fmt.fork_string(self.fork.name))
if timestamp:
repo.reset_timestamp(timestamp, self._timestamp_author, self.ref)
return
Expand Down

0 comments on commit b30fda5

Please sign in to comment.