Skip to content

Commit

Permalink
Merge pull request #364 from JrGoodle/checkout
Browse files Browse the repository at this point in the history
Add `clowder checkout` command
  • Loading branch information
JrGoodle committed Oct 31, 2017
2 parents 681aaab + ae9bfa2 commit 8fccdd3
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ $ clowder status

```bash
$ clowder branch # Print all local branches
$ clowder checkout my_branch # Checkout my_branch in projects
$ clowder clean # Discard any changes in projects
$ clowder diff # Print git diff for all projects
$ clowder forall -c 'git status' # Run command in all project directories
Expand Down
26 changes: 26 additions & 0 deletions clowder/clowder/clowder_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,32 @@ def branch(self, group_names, **kwargs):
for project in projects:
self._run_project_command(project, skip, 'branch', local=local, remote=remote)

def checkout(self, branch, group_names, **kwargs):
"""Checkout branches
:param str branch: Branch to checkout
:param list of str group_names: Group names to checkout branches for
Keyword Args:
project_names (list of str): Project names to clean
skip (list of str): Project names to skip
:return:
"""

project_names = kwargs.get('project_names', None)
skip = kwargs.get('skip', [])

if project_names is None:
groups = [g for g in self.groups if g.name in group_names]
for group in groups:
self._run_group_command(group, skip, 'checkout', branch)
return

projects = [p for g in self.groups for p in g.projects if p.name in project_names]
for project in projects:
self._run_project_command(project, skip, 'checkout', branch)

def clean(self, group_names, **kwargs):
"""Discard changes
Expand Down
7 changes: 7 additions & 0 deletions clowder/clowder/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ def branch(self):
self.clowder.branch(group_names=self._args.groups, project_names=self._args.projects,
skip=self._args.skip, local=True)

@valid_clowder_yaml_required
@print_clowder_repo_status
def checkout(self):
"""clowder checkout command"""

self.clowder.checkout(self._args.branch, group_names=self._args.groups, project_names=self._args.projects)

@valid_clowder_yaml_required
@print_clowder_repo_status
def clean(self):
Expand Down
7 changes: 5 additions & 2 deletions clowder/clowder/git/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ def add(self, files):
else:
self.status_verbose()

def checkout(self, truncated_ref):
def checkout(self, truncated_ref, allow_failure=False):
"""Checkout git ref
:param str truncated_ref: Ref to git checkout
:param Optional[bool] allow_failure: Whether to allow failing to checkout branch. Defaults to False
:return:
"""

Expand All @@ -85,8 +86,10 @@ def checkout(self, truncated_ref):
return
self.repo.git.checkout(truncated_ref)
except GitError as err:
message = colored(' - Failed to checkout ref ', 'red')
message = colored(' - Failed to checkout ', 'red')
self._print(message + ref_output)
if allow_failure:
return
self._print(fmt.error(err))
self._exit(fmt.parallel_exception_error(self.repo_path, message, ref_output))
except (KeyboardInterrupt, SystemExit):
Expand Down
11 changes: 10 additions & 1 deletion clowder/clowder/model/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ def branch(self, local=False, remote=False):

repo.print_branches(local=local, remote=remote)

@project_repo_exists
def checkout(self, branch):
"""Checkout branch
:param str branch: Branch to check out
:return:
"""

self._repo(self.full_path(), self._remote, self._ref, self._recursive).checkout(branch, allow_failure=True)

@project_repo_exists
def clean(self, args='', recursive=False):
"""Discard changes for project
Expand All @@ -102,7 +112,6 @@ def clean(self, args='', recursive=False):
- ``X`` Remove only files ignored by git
- ``x`` Remove all untracked files
:param Optional[bool] recursive: Clean submodules recursively. Defaults to False
:return:
"""

Expand Down
2 changes: 1 addition & 1 deletion clowder/clowder/util/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def wrapper(*args, **kwargs):


def project_repo_exists(func):
"""If no git repo exists, print error message and exit"""
"""If no git repo exists, print message and return"""

def wrapper(*args, **kwargs):
"""Wrapper"""
Expand Down
32 changes: 32 additions & 0 deletions clowder/clowder/util/subparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def _configure_subparsers(subparsers, clowder, versions):
"""

_configure_subparser_branch(subparsers, clowder)
_configure_subparser_checkout(subparsers, clowder)
_configure_subparser_clean(subparsers, clowder)
_configure_subparser_diff(subparsers, clowder)
_configure_subparser_forall(subparsers, clowder)
Expand Down Expand Up @@ -83,6 +84,37 @@ def _configure_subparser_branch(subparsers, clowder):
help=branch_help_projects)


def _configure_subparser_checkout(subparsers, clowder):
"""Configure clowder checkout subparser and arguments
:param ArgumentParser subparsers: ArgParse ArgumentParser instance returned from ``add_subparsers()``
:param ClowderController clowder: ClowderController instance
:return:
"""

group_names = _group_names(clowder)
project_names = _project_names(clowder)
checkout_help = 'Checkout local branch in projects'
parser_checkout = subparsers.add_parser('checkout', help=checkout_help)

parser_checkout.add_argument('branch', help='branch to checkout', metavar='BRANCH')

checkout_help_skip = _options_help_message(project_names, 'projects to skip')
parser_checkout.add_argument('--skip', '-s', choices=project_names, nargs='+', metavar='PROJECT', default=[],
help=checkout_help_skip)

group_checkout = parser_checkout.add_mutually_exclusive_group()

checkout_help_groups = _options_help_message(group_names, 'groups to checkout branches for')
group_checkout.add_argument('--groups', '-g', choices=group_names,
default=group_names, nargs='+', metavar='GROUP',
help=checkout_help_groups)

checkout_help_projects = _options_help_message(project_names, 'projects to checkout branches for')
group_checkout.add_argument('--projects', '-p', choices=project_names, nargs='+', metavar='PROJECT',
help=checkout_help_projects)


def _configure_subparser_clean(subparsers, clowder):
"""Configure clowder clean subparser and arguments
Expand Down
7 changes: 7 additions & 0 deletions clowder_test/clowder_test/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ def cats_branch(self, path):
return_code = self._execute_command('./branch.sh', path)
self._exit(return_code)

def cats_checkout(self, path):
"""clowder cats checkout tests"""

return_code = self._execute_command('./checkout.sh', path)
self._exit(return_code)

def cats_clean(self, path):
"""clowder cats clean tests"""

Expand Down Expand Up @@ -385,6 +391,7 @@ def _configure_cats_subparser(self):
cats_subparser = parser.add_subparsers(dest='cats_command', metavar='SUBCOMMAND')
cats_subparser.add_parser('all', help='Run all cats tests')
cats_subparser.add_parser('branch', help='Run cats branch tests')
cats_subparser.add_parser('checkout', help='Run cats checkout tests')
cats_subparser.add_parser('clean', help='Run cats clean tests')
cats_subparser.add_parser('diff', help='Run cats diff tests')
cats_subparser.add_parser('forall', help='Run cats forall tests')
Expand Down
14 changes: 14 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# `clowder` Commands

- [clowder branch](#clowder-branch)
- [clowder checkout](#clowder-checkout)
- [clowder clean](#clowder-clean)
- [clowder diff](#clowder-diff)
- [clowder forall](#clowder-forall)
Expand Down Expand Up @@ -41,6 +42,19 @@ $ clowder branch -p apple/swift

---

```bash
# Checkout branches
$ clowder checkout branch_name

# Checkout branches in llvm group
$ clowder checkout branch_name -g llvm

# Checkout branches in swift project
$ clowder checkout branch_name -p apple/swift
```

---

## `clowder clean`

Discards changes in dirty repositories
Expand Down
71 changes: 71 additions & 0 deletions test/scripts/cats/checkout.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bash

cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/.." || exit 1

. test_utilities.sh

export cats_projects=( 'duke' 'mu' )

export black_cats_projects=( 'black-cats/kit' \
'black-cats/kishka' \
'black-cats/sasha' \
'black-cats/jules' )

export all_projects=( 'mu' 'duke' \
'black-cats/kit' \
'black-cats/kishka' \
'black-cats/sasha' \
'black-cats/jules' )

test_cats_default_herd_branches() {
echo "TEST: cats projects on default branches"
for project in "${black_cats_projects[@]}"; do
pushd $project || exit 1
test_branch master
popd || exit 1
done
pushd mu || exit 1
test_branch knead
popd || exit 1
pushd duke || exit 1
test_branch purr
popd || exit 1
}

print_double_separator
echo "TEST: Test clowder checkout"

cd "$CATS_EXAMPLE_DIR" || exit 1
./clean.sh
./init.sh

test_checkout() {
print_single_separator
echo "TEST: Check projects are on correct branches"
clowder herd $PARALLEL || exit 1
local branch='checkout_branch'

pushd duke || exit 1
git branch $branch || exit 1
popd || exit 1
pushd mu || exit 1
git branch $branch || exit 1
popd || exit 1

test_cats_default_herd_branches

clowder checkout $branch || exit 1

pushd duke || exit 1
test_branch $branch || exit 1
popd || exit 1
pushd mu || exit 1
test_branch $branch || exit 1
popd || exit 1
for project in "${black_cats_projects[@]}"; do
pushd $project || exit 1
test_branch master
popd || exit 1
done
}
test_checkout
3 changes: 3 additions & 0 deletions test/scripts/cats/offline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ print_single_separator
echo 'TEST: clowder branch -a'
clowder branch -a || enable_connection_exit
print_single_separator
echo 'TEST: clowder checkout'
clowder checkout branch_name || enable_connection_exit
print_single_separator
echo 'TEST: clowder clean'
clowder clean || enable_connection_exit
print_single_separator
Expand Down
1 change: 1 addition & 0 deletions test/scripts/test_example_cats.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test_command

"$TEST_SCRIPT_DIR/cats/init.sh" || exit 1
"$TEST_SCRIPT_DIR/cats/branch.sh" || exit 1
"$TEST_SCRIPT_DIR/cats/checkout.sh" || exit 1
"$TEST_SCRIPT_DIR/cats/status.sh" || exit 1
"$TEST_SCRIPT_DIR/cats/clean.sh" || exit 1
"$TEST_SCRIPT_DIR/cats/herd.sh" || exit 1
Expand Down

0 comments on commit 8fccdd3

Please sign in to comment.