Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Issue #2: Support hg/git tags

  • Loading branch information...
commit 6f3e0e5d9ffc8911aa8d10b04bef227434600d51 1 parent af56000
@vmalloc authored
View
3  README.md
@@ -45,6 +45,7 @@ Local paths get included as bind-mounts. If the path specified is a relative pat
Git and Mercurial repositories can be included. They are designated by using resource paths starting with `git://`, `ssh+git://`, `http+hg://` or `https+hg://`. They also take additional optional parameters, for example:
Include("/path", "git://server/git/repository", branch="development") # cloning a specific branch
+ Include("/path", "git://server/git/repository", tag="rc1") # clone a specific tag
Include("/path", "git://server/git/repository", commit="4ff7a0565964eb428e5b45479922f164a5ee941b") # specific commit
### Squashfs Images
@@ -67,7 +68,7 @@ You can control the environment variables set up by dwight using the `ENVIRON` v
# Known Issues
-* Currently including a single git/hg repository multiple times with different commits/branches will cause separate copies of the repository in the cache
+* Currently including a single git/hg repository multiple times with different commits/branches/tags will cause separate copies of the repository in the cache
# Extending, Modifying and Testing the Code
View
70 dwight_chroot/resources.py
@@ -57,31 +57,25 @@ def refresh(self, path):
raise NotImplementedError() # pragma: no cover
class DVCSResource(CacheableResource):
- def __init__(self, repo_url, commit=None, branch=None):
+ def __init__(self, repo_url, commit=None, branch=None, tag=None):
super(DVCSResource, self).__init__()
self.repo_url = repo_url
- if commit is not None and branch is not None:
- raise UsageException("Cannot specify both branch and commit for git resources")
self.commit = commit
self.branch = branch
+ self.tag = tag
+ self._check_parameters()
+ self._needs_pull = (commit is None and tag is None)
+ def _check_parameters(self):
+ if len([x for x in (self.commit, self.branch, self.tag) if x is not None]) > 1:
+ raise UsageException("Can only specify at most one of (commit/branch/tag) for SCM resources")
def get_cache_key(self):
- return dict(url=self.repo_url, commit=self.commit, branch=self.branch)
+ return dict(url=self.repo_url, commit=self.commit, branch=self.branch, tag=self.tag)
def fetch(self, path):
- self._refresh(path, initialize=True)
+ shutil.rmtree(path)
+ self._clone(path)
def refresh(self, path):
- self._refresh(path, initialize=False)
- def _refresh(self, path, initialize):
- if initialize:
- shutil.rmtree(path)
- self._clone_into(path)
- if self.commit:
- # a commit never changes, so we only do this upon
- # initialization
- self._checkout_commit(path)
- if self.branch:
- self._checkout_branch(path)
- elif not self.commit:
- self._pull_changes(path)
+ if self._needs_pull:
+ self._pull(path)
class MercurialResource(DVCSResource):
def __init__(self, *args, **kwargs):
@@ -95,37 +89,31 @@ def _fix_url_scheme(self):
if self.repo_url.startswith(prefix):
self.repo_url = fix + self.repo_url[len(prefix):]
break
- def _clone_into(self, path):
+ def _clone(self, path):
cmd = "hg clone"
- if self.commit:
- cmd += " -r {0}".format(self.commit)
+ if self.commit or self.tag:
+ cmd += " -r {0}".format(self.commit or self.tag)
if self.branch:
cmd += " -b {0}".format(self.branch)
cmd += " {0} {1}".format(self.repo_url, path)
execute_command_assert_success(cmd)
- def _checkout_branch(self, path):
- pass
- def _checkout_commit(self, path):
- pass
- def _pull_changes(self, path):
- if not self.commit:
- execute_command_assert_success("hg pull", cwd=path)
+ def _pull(self, path):
+ execute_command_assert_success("hg pull", cwd=path)
class GitResource(DVCSResource):
- def _clone_into(self, path):
+ def _clone(self, path):
execute_command_assert_success("git clone {0} {1}".format(self.repo_url, path))
- def _checkout_branch(self, path):
- assert self.branch
- execute_command_assert_success(
- "git fetch origin && git checkout -b {0} origin/{0}".format(self.branch),
- cwd=path)
- def _checkout_commit(self, path):
- assert self.commit
- execute_command_assert_success(
- "git fetch origin && git checkout {0} && git reset --hard".format(self.commit),
- cwd=path
- )
- def _pull_changes(self, path):
+ if self.branch:
+ execute_command_assert_success(
+ "git checkout -b {0} origin/{0}".format(self.branch),
+ cwd=path
+ )
+ if self.commit or self.tag:
+ execute_command_assert_success(
+ "git checkout {0}".format(self.commit or self.tag),
+ cwd=path
+ )
+ def _pull(self, path):
execute_command_assert_success(
"git pull",
cwd=path
View
17 tests/test__resources.py
@@ -1,4 +1,6 @@
from .test_utils import TestCase
+import functools
+import itertools
from dwight_chroot import resources
from dwight_chroot.exceptions import UsageException
@@ -17,7 +19,14 @@ def test__web_resource(self):
def assertDetectedAs(self, string, resource_type):
self.assertIs(resource_type, resources.Resource.get_resource_type_from_string(string))
-class GitResourceTest(TestCase):
- def test__cannot_specify_commit_and_branch_together(self):
- with self.assertRaises(UsageException):
- resources.GitResource("repo", commit="a", branch="b")
+class DVCSResourceTest(TestCase):
+ def test__cannot_specify_multiple_checkout_options(self):
+ for url in ("git://a/b", "http+hg://a/b"):
+ resource_type = resources.Resource.get_resource_type_from_string(url)
+ for branch, tag, commit in itertools.product([None, "a"], [None, "b"], [None, "c"]):
+ r = functools.partial(resource_type, url, branch=branch, tag=tag, commit=commit)
+ if len(set([branch, tag, commit]) - set([None])) <= 1:
+ r()
+ else:
+ with self.assertRaises(UsageException):
+ r()
View
12 vagrant_setup_server.sh
@@ -27,10 +27,16 @@ pushd $HOME/hgrepo
touch fetched_from_hg_file
hg add fetched_from_hg_file
hg commit -m "default commit"
+# hg branch
hg branch branch
hg rm fetched_from_hg_file
+touch fetched_from_hg_tag_file
+hg add .
+hg commit -m "tag commit"
+hg tag tag
+hg rm fetched_from_hg_tag_file
touch fetched_from_hg_branch_file
-hg add fetched_from_hg_branch_file
+hg add .
hg commit -m "branch commit"
hg serve -d --prefix repository
popd
@@ -64,6 +70,10 @@ git init git_repository
pushd git_repository
git commit -a --allow-empty -m init
git checkout -b branch
+touch fetched_from_git_tag_file
+git add .
+git commit -a -m "tag commit"
+git rm fetched_from_git_tag_file
touch fetched_from_git_branch_file
git add .
git commit -a -m "branch commit"
Please sign in to comment.
Something went wrong with that request. Please try again.