Merge pull request #49 from cveneziani/git_submodule_strategy
Add strategy option to git method
This commit is contained in:
commit
064b17add2
3 changed files with 55 additions and 19 deletions
|
@ -65,6 +65,7 @@ activate :deploy do |deploy|
|
|||
# Optional Settings
|
||||
# deploy.remote = "custom-remote" # remote name or git url, default: origin
|
||||
# deploy.branch = "custom-branch" # default: gh-pages
|
||||
# deploy.strategy = :submodule # commit strategy: can be :force_push or :submodule, default: :force_push
|
||||
end
|
||||
```
|
||||
|
||||
|
@ -72,8 +73,12 @@ If you use a remote name, you must first add it using `git remote add`. Run
|
|||
`git remote -v` to see a list of possible remote names. If you use a git url,
|
||||
it must end with '.git'.
|
||||
|
||||
Afterwards, the `build` directory will become a git repo. This branch will be
|
||||
created on the remote if it doesn't already exist.
|
||||
Afterwards, the `build` directory will become a git repo.
|
||||
|
||||
If you use the force push strategy, this branch will be created on the remote if
|
||||
it doesn't already exist.
|
||||
But if you use the submodule strategy, you must first initialize build folder as
|
||||
a submodule. See `git submodule add` documentation.
|
||||
|
||||
### FTP
|
||||
|
||||
|
|
|
@ -68,9 +68,13 @@ activate :deploy do |deploy|
|
|||
# remote is optional (default is "origin")
|
||||
# run `git remote -v` to see a list of possible remotes
|
||||
deploy.remote = "some-other-remote-name"
|
||||
|
||||
# branch is optional (default is "gh-pages")
|
||||
# run `git branch -a` to see a list of possible branches
|
||||
deploy.branch = "some-other-branch-name"
|
||||
|
||||
# strategy is optional (default is :force_push)
|
||||
deploy.strategy = :submodule
|
||||
end
|
||||
|
||||
# To deploy the build directory to a remote host via ftp:
|
||||
|
@ -155,6 +159,11 @@ EOF
|
|||
def deploy_git
|
||||
remote = self.deploy_options.remote
|
||||
branch = self.deploy_options.branch
|
||||
strategy = self.deploy_options.strategy
|
||||
|
||||
commit_signature = "#{Middleman::Deploy::PACKAGE} #{Middleman::Deploy::VERSION}"
|
||||
commit_time = "#{Time.now.utc}"
|
||||
push_options = (strategy == :force_push ? ' -f' : nil)
|
||||
|
||||
puts "## Deploying via git to remote=\"#{remote}\" and branch=\"#{branch}\""
|
||||
|
||||
|
@ -170,6 +179,7 @@ EOF
|
|||
end
|
||||
|
||||
Dir.chdir(self.inst.build_dir) do
|
||||
if strategy == :force_push
|
||||
unless File.exists?('.git')
|
||||
`git init`
|
||||
`git remote add origin #{remote}`
|
||||
|
@ -180,6 +190,7 @@ EOF
|
|||
`git remote add origin #{remote}`
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#if there is a branch with that name, switch to it, otherwise create a new one and switch to it
|
||||
if `git branch`.split("\n").any? { |b| b =~ /#{branch}/i }
|
||||
|
@ -188,10 +199,29 @@ EOF
|
|||
`git checkout -b #{branch}`
|
||||
end
|
||||
|
||||
if strategy == :submodule
|
||||
`git fetch`
|
||||
`git stash`
|
||||
`git rebase #{self.deploy_options.remote}/#{branch}`
|
||||
`git stash pop`
|
||||
|
||||
if $?.exitstatus == 1
|
||||
puts "Can't deploy! Please resolve conflicts. Then process to manual commit and push on #{branch} branch."
|
||||
exit
|
||||
end
|
||||
end
|
||||
|
||||
`git add -A`
|
||||
# '"message"' double quotes to fix windows issue
|
||||
`git commit --allow-empty -am '"Automated commit at #{Time.now.utc} by #{Middleman::Deploy::PACKAGE} #{Middleman::Deploy::VERSION}"'`
|
||||
`git push -f origin #{branch}`
|
||||
`git commit --allow-empty -am "Automated commit at #{commit_time} by #{commit_signature}"`
|
||||
`git push #{push_options} origin #{branch}`
|
||||
end
|
||||
|
||||
if strategy == :submodule
|
||||
current_branch = `git rev-parse --abbrev-ref HEAD`
|
||||
|
||||
`git add #{self.inst.build_dir}`
|
||||
`git commit --allow-empty -m "Deployed at #{commit_time} by #{commit_signature}"`
|
||||
`git push origin #{current_branch}`
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ require "middleman-core"
|
|||
module Middleman
|
||||
module Deploy
|
||||
|
||||
class Options < Struct.new(:whatisthis, :method, :host, :port, :user, :password, :path, :clean, :remote, :branch, :build_before, :flags); end
|
||||
class Options < Struct.new(:whatisthis, :method, :host, :port, :user, :password, :path, :clean, :remote, :branch, :strategy, :build_before, :flags); end
|
||||
|
||||
class << self
|
||||
|
||||
|
@ -24,6 +24,7 @@ module Middleman
|
|||
# Default options for the git method.
|
||||
options.remote ||= "origin"
|
||||
options.branch ||= "gh-pages"
|
||||
options.strategy ||= :force_push
|
||||
|
||||
options.build_before ||= false
|
||||
|
||||
|
|
Loading…
Reference in a new issue