Add strategy option to git method

- Set force push as default strategy (avoid breaking change)
- Add submodule strategy
This commit is contained in:
Cecile Veneziani 2013-11-30 21:28:35 +01:00
parent b0f7cde56a
commit 23c6b8c1f6
3 changed files with 55 additions and 19 deletions

View file

@ -63,8 +63,9 @@ following to `config.rb`:
activate :deploy do |deploy| activate :deploy do |deploy|
deploy.method = :git deploy.method = :git
# Optional Settings # Optional Settings
# deploy.remote = "custom-remote" # remote name or git url, default: origin # deploy.remote = "custom-remote" # remote name or git url, default: origin
# deploy.branch = "custom-branch" # default: gh-pages # deploy.branch = "custom-branch" # default: gh-pages
# deploy.strategy = :submodule # commit strategy: can be :force_push or :submodule, default: :force_push
end 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, `git remote -v` to see a list of possible remote names. If you use a git url,
it must end with '.git'. it must end with '.git'.
Afterwards, the `build` directory will become a git repo. This branch will be Afterwards, the `build` directory will become a git repo.
created on the remote if it doesn't already exist.
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 ### FTP

View file

@ -68,9 +68,13 @@ activate :deploy do |deploy|
# remote is optional (default is "origin") # remote is optional (default is "origin")
# run `git remote -v` to see a list of possible remotes # run `git remote -v` to see a list of possible remotes
deploy.remote = "some-other-remote-name" deploy.remote = "some-other-remote-name"
# branch is optional (default is "gh-pages") # branch is optional (default is "gh-pages")
# run `git branch -a` to see a list of possible branches # run `git branch -a` to see a list of possible branches
deploy.branch = "some-other-branch-name" deploy.branch = "some-other-branch-name"
# strategy is optional (default is :force_push)
deploy.strategy = :submodule
end end
# To deploy the build directory to a remote host via ftp: # To deploy the build directory to a remote host via ftp:
@ -153,8 +157,13 @@ EOF
end end
def deploy_git def deploy_git
remote = self.deploy_options.remote remote = self.deploy_options.remote
branch = self.deploy_options.branch 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}\"" puts "## Deploying via git to remote=\"#{remote}\" and branch=\"#{branch}\""
@ -170,14 +179,16 @@ EOF
end end
Dir.chdir(self.inst.build_dir) do Dir.chdir(self.inst.build_dir) do
unless File.exists?('.git') if strategy == :force_push
`git init` unless File.exists?('.git')
`git remote add origin #{remote}` `git init`
else
#check if the remote repo has changed
unless remote == `git config --get remote.origin.url`.chop
`git remote rm origin`
`git remote add origin #{remote}` `git remote add origin #{remote}`
else
#check if the remote repo has changed
unless remote == `git config --get remote.origin.url`.chop
`git remote rm origin`
`git remote add origin #{remote}`
end
end end
end end
@ -188,10 +199,29 @@ EOF
`git checkout -b #{branch}` `git checkout -b #{branch}`
end 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` `git add -A`
# '"message"' double quotes to fix windows issue `git commit --allow-empty -am "Automated commit at #{commit_time} by #{commit_signature}"`
`git commit --allow-empty -am '"Automated commit at #{Time.now.utc} by #{Middleman::Deploy::PACKAGE} #{Middleman::Deploy::VERSION}"'` `git push #{push_options} origin #{branch}`
`git push -f 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
end end

View file

@ -5,7 +5,7 @@ require "middleman-core"
module Middleman module Middleman
module Deploy 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 class << self
@ -22,8 +22,9 @@ module Middleman
options.clean ||= false options.clean ||= false
# Default options for the git method. # Default options for the git method.
options.remote ||= "origin" options.remote ||= "origin"
options.branch ||= "gh-pages" options.branch ||= "gh-pages"
options.strategy ||= :force_push
options.build_before ||= false options.build_before ||= false