Skip to content

Commit

Permalink
Merge pull request #62 from JrGoodle/sync-branch
Browse files Browse the repository at this point in the history
Add -b option to `clowder sync`
  • Loading branch information
JrGoodle committed Sep 20, 2015
2 parents 531b14a + eac5e81 commit ac8e22d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 6 deletions.
37 changes: 36 additions & 1 deletion clowder/clowder_repo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Clowder repo management"""
import emoji, os
from git import Repo
from termcolor import colored
from clowder.utility.git_utilities import (
git_branches,
git_clone_url_at_path,
git_is_detached,
git_pull
Expand All @@ -21,6 +23,10 @@ def __init__(self, root_directory):
self.root_directory = root_directory
self.clowder_path = os.path.join(self.root_directory, 'clowder')

def branches(self):
"""Return current local branches"""
return git_branches(self.clowder_path)

def breed(self, url):
"""Clone clowder repo from url"""
git_clone_url_at_path(url, self.clowder_path, 'refs/heads/master', 'origin')
Expand Down Expand Up @@ -59,14 +65,43 @@ def symlink_yaml(self, version=None):
def sync(self):
"""Sync clowder repo"""
self._validate()
self.print_status()
if not git_is_detached(self.clowder_path):
print(' - Pulling latest changes')
git_pull(self.clowder_path)
self.symlink_yaml()
else:
print(' - HEAD is detached')
print_exiting()

# Disable errors shown by pylint for no specified exception types
# pylint: disable=W0702
def sync_branch(self, branch):
"""Sync clowder repo to specified branch"""
try:
repo = Repo(self.clowder_path)
except:
repo_path_output = colored(self.clowder_path, 'cyan')
print("Failed to create Repo instance for " + repo_path_output)
else:
if git_is_detached(self.clowder_path):
try:
repo.git.checkout(branch)
except:
print("Failed to checkout branch " + branch)
print_exiting()

if repo.active_branch.name != branch:
try:
repo.git.checkout(branch)
except:
print("Failed to checkout branch " + branch)
print_exiting()

self._validate()
self.print_status()
git_pull(self.clowder_path)
self.symlink_yaml()

def _validate(self):
"""Validate status of clowder repo"""
if not validate_repo_state(self.clowder_path):
Expand Down
15 changes: 11 additions & 4 deletions clowder/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ def __init__(self):
self.versions = None
self.group_names = ''
self.project_names = ''
self.branches = ''
# Load current clowder.yml config if it exists
if os.path.isdir(os.path.join(self.root_directory, 'clowder')):
clowder_path = os.path.join(self.root_directory, 'clowder')
if os.path.isdir(clowder_path):
self.clowder_repo = ClowderRepo(self.root_directory)
self.clowder = ClowderController(self.root_directory)
self.versions = self.clowder.get_fixed_version_names()
self.branches = self.clowder_repo.branches()
if self.clowder.get_all_group_names() is not None:
self.group_names = self.clowder.get_all_group_names()
if self.clowder.get_all_project_names() is not None:
Expand Down Expand Up @@ -134,8 +137,10 @@ def sync(self):
"""clowder sync command"""
if self.clowder_repo is not None:
cprint('Sync...\n', 'yellow')
self.clowder_repo.print_status()
self.clowder_repo.sync()
if self.args.branch is None:
self.clowder_repo.sync()
else:
self.clowder_repo.sync_branch(self.args.branch)
else:
exit_clowder_not_found()

Expand Down Expand Up @@ -197,7 +202,9 @@ def _configure_subparsers(self, subparsers):
group_stash.add_argument('--projects', '-p', choices=self.project_names,
nargs='+', help='Projects to stash')
# clowder sync
subparsers.add_parser('sync', add_help=False, help='Sync clowder repo')
parser_sync = subparsers.add_parser('sync', add_help=False, help='Sync clowder repo')
parser_sync.add_argument('--branch', '-b', choices=self.branches,
help='Groups to print status for')

def exit_unrecognized_command(parser):
"""Print unrecognized command message and exit"""
Expand Down
10 changes: 10 additions & 0 deletions clowder/utility/git_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
# Disable errors shown by pylint for no specified exception types
# pylint: disable=W0702

def git_branches(repo_path):
"""Get list of current branches"""
try:
repo = Repo(repo_path)
except:
repo_path_output = colored(repo_path, 'cyan')
print("Failed to create Repo instance for " + repo_path_output)
else:
return repo.branches

def git_checkout_branch(repo_path, branch, remote):
"""Checkout branch, and create if it doesn't exist"""
try:
Expand Down
20 changes: 20 additions & 0 deletions scripts/test_cats_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ test_no_versions()
popd &>/dev/null
}

test_sync_branch()
{
print_separator
echo "TEST: Test syncing branch from current branch"
pushd clowder &>/dev/null
git checkout master
popd &>/dev/null
clowder sync -b master || exit 1
echo "TEST: Test syncing other branch"
clowder sync -b tags || exit 1
echo "TEST: Test syncing missing branch"
clowder sync -b sync-missing-branch && exit 1
echo "TEST: Test syncing branch from detached HEAD"
pushd clowder &>/dev/null
git checkout master~2
popd &>/dev/null
clowder sync -b master || exit 1
}

# export projects=( 'black-cats/kit' \
# 'black-cats/kishka' \
# 'black-cats/sasha' \
Expand Down Expand Up @@ -144,5 +163,6 @@ test_invalid_yaml
test_herd_sha
test_herd_tag
test_herd_missing_groups
test_sync_branch

print_help
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
setup(
name='clowder',
description='A tool for managing code',
version='0.7.3',
version='0.7.4',
url='http://clowder.cat',
author='joe DeCapo',
author_email='joe@polka.cat',
Expand Down

0 comments on commit ac8e22d

Please sign in to comment.