From 23c6b8c1f6623087b12af971b868c835a0377dfe Mon Sep 17 00:00:00 2001 From: Cecile Veneziani Date: Sat, 30 Nov 2013 21:28:35 +0100 Subject: [PATCH] Add strategy option to git method - Set force push as default strategy (avoid breaking change) - Add submodule strategy --- README.md | 13 +++++--- lib/middleman-deploy/commands.rb | 54 ++++++++++++++++++++++++------- lib/middleman-deploy/extension.rb | 7 ++-- 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 0cdfd20..b669d5b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/middleman-deploy/commands.rb b/lib/middleman-deploy/commands.rb index 517411f..d2df8d6 100644 --- a/lib/middleman-deploy/commands.rb +++ b/lib/middleman-deploy/commands.rb @@ -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 diff --git a/lib/middleman-deploy/extension.rb b/lib/middleman-deploy/extension.rb index 12eed41..9882b06 100644 --- a/lib/middleman-deploy/extension.rb +++ b/lib/middleman-deploy/extension.rb @@ -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