diff --git a/CHANGELOG.md b/CHANGELOG.md index 62d96394..d18d084a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ Reverse Chronological Order: https://github.com/capistrano/capistrano/compare/v3.2.1...HEAD +* Enhancement (@townsen) + * Added the variable `:repo_tree` which allows the specification of a sub-tree that + will be extracted from the repository. This is useful when deploying a project + that lives in a subdirectory of a larger repository. + Implemented only for git and hg. + If not defined then the behaviour is as previously and the whole repository is + extracted (subject to git-archive `.gitattributes` of course). + * Enhancements (@townsen) * Previously filtering would affect any generated configuration files so that files newly deployed would not be the same as those on the hosts previously diff --git a/README.md b/README.md index 99c3db3d..948f05fc 100644 --- a/README.md +++ b/README.md @@ -257,6 +257,7 @@ The following variables are settable: | Variable Name | Description | Notes | |:---------------------:|----------------------------------------------------------------------|-----------------------------------------------------------------| | `:repo_url` | The URL of your scm repository (git, hg, svn) | file://, https://, ssh://, or svn+ssh:// are all supported | +| `:repo_tree` | The subtree of the scm repository to deploy (git, hg) | Only implemented for git and hg repos. Extract just this tree | | `:branch` | The branch you wish to deploy | This only has meaning for git and hg repos, to specify the branch of an svn repo, set `:repo_url` to the branch location. | | `:scm` | The source control system used | `:git`, `:hg`, `:svn` are currently supported | | `:tmp_dir` | The (optional) temp directory that will be used (default: /tmp) | if you have a shared web host, this setting may need to be set (i.e. /home/user/tmp/capistrano). | diff --git a/lib/capistrano/git.rb b/lib/capistrano/git.rb index 93b2297f..a1c6ca92 100644 --- a/lib/capistrano/git.rb +++ b/lib/capistrano/git.rb @@ -30,7 +30,13 @@ class Capistrano::Git < Capistrano::SCM end def release - git :archive, fetch(:branch), '| tar -x -f - -C', release_path + if tree = fetch(:repo_tree) + tree = tree.slice %r#^/?(.*?)/?$#, 1 + components = tree.split('/').size + git :archive, fetch(:branch), tree, "| tar -x --strip-components #{components} -f - -C", release_path + else + git :archive, fetch(:branch), '| tar -x -f - -C', release_path + end end def fetch_revision diff --git a/lib/capistrano/hg.rb b/lib/capistrano/hg.rb index 2c5bca30..e7f02909 100644 --- a/lib/capistrano/hg.rb +++ b/lib/capistrano/hg.rb @@ -27,7 +27,13 @@ class Capistrano::Hg < Capistrano::SCM end def release - hg "archive", release_path, "--rev", fetch(:branch) + if tree = fetch(:repo_tree) + tree = tree.slice %r#^/?(.*?)/?$#, 1 + components = tree.split('/').size + hg "archive --type tgz -p . -I", tree, "--rev", fetch(:branch), "| tar -x --strip-components #{components} -f - -C", release_path + else + hg "archive", release_path, "--rev", fetch(:branch) + end end def fetch_revision diff --git a/spec/lib/capistrano/git_spec.rb b/spec/lib/capistrano/git_spec.rb index a474ad4b..35073082 100644 --- a/spec/lib/capistrano/git_spec.rb +++ b/spec/lib/capistrano/git_spec.rb @@ -57,14 +57,25 @@ module Capistrano end describe "#release" do - it "should run git archive" do - context.expects(:fetch).returns(:branch) + it "should run git archive without a subtree" do + context.expects(:fetch).with(:repo_tree).returns(nil) + context.expects(:fetch).with(:branch).returns(:branch) context.expects(:release_path).returns(:path) context.expects(:execute).with(:git, :archive, :branch, '| tar -x -f - -C', :path) subject.release end + + it "should run git archive with a subtree" do + context.expects(:fetch).with(:repo_tree).returns('tree') + context.expects(:fetch).with(:branch).returns(:branch) + context.expects(:release_path).returns(:path) + + context.expects(:execute).with(:git, :archive, :branch, 'tree', '| tar -x --strip-components 1 -f - -C', :path) + + subject.release + end end end end diff --git a/spec/lib/capistrano/hg_spec.rb b/spec/lib/capistrano/hg_spec.rb index 7519cffc..d7d5147d 100644 --- a/spec/lib/capistrano/hg_spec.rb +++ b/spec/lib/capistrano/hg_spec.rb @@ -57,14 +57,25 @@ module Capistrano end describe "#release" do - it "should run hg archive" do - context.expects(:fetch).returns(:branch) + it "should run hg archive without a subtree" do + context.expects(:fetch).with(:repo_tree).returns(nil) + context.expects(:fetch).with(:branch).returns(:branch) context.expects(:release_path).returns(:path) context.expects(:execute).with(:hg, "archive", :path, "--rev", :branch) subject.release end + + it "should run hg archive with a subtree" do + context.expects(:fetch).with(:repo_tree).returns('tree') + context.expects(:fetch).with(:branch).returns(:branch) + context.expects(:release_path).returns(:path) + + context.expects(:execute).with(:hg, "archive --type tgz -p . -I", 'tree', "--rev", :branch, '| tar -x --strip-components 1 -f - -C', :path) + + subject.release + end end end end