Add strategy option to git method

- Set force push as default strategy (avoid breaking change)
- Add submodule strategy
master
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|
deploy.method = :git
# Optional Settings
# deploy.remote = "custom-remote" # remote name or git url, default: origin
# deploy.branch = "custom-branch" # default: gh-pages
# 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

View File

@ -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:
@ -153,8 +157,13 @@ EOF
end
def deploy_git
remote = self.deploy_options.remote
branch = self.deploy_options.branch
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,14 +179,16 @@ EOF
end
Dir.chdir(self.inst.build_dir) do
unless File.exists?('.git')
`git init`
`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`
if strategy == :force_push
unless File.exists?('.git')
`git init`
`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
@ -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

View File

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