class EY::Serverside::Source::Git

Deploy source for git repository sourced deploy.

Public Instance Methods

create_revision_file_command(revision_file_path) click to toggle source
# File lib/engineyard-serverside/source/git.rb, line 7
def create_revision_file_command(revision_file_path)
  %Q{#{git} show --pretty=format:"%H" | head -1 > "#{revision_file_path}"}
end
gc_repository_cache() click to toggle source
# File lib/engineyard-serverside/source/git.rb, line 11
def gc_repository_cache
  shell.status "Garbage collecting cached git repository to reduce disk usage."
  run("#{git} gc")
end
same?(previous_revision, active_revision, paths=nil) click to toggle source

Check if there have been changes. git diff –exit-code returns

  • 0 when nothing has changed

  • 1 when there are changes

previous_revision - The previous ref string. active_revision - The current ref string.

Returns a boolean whether there has been a change.

# File lib/engineyard-serverside/source/git.rb, line 26
def same?(previous_revision, active_revision, paths=nil)
  run_and_success?("#{git} diff '#{previous_revision}'..'#{active_revision}' --exit-code --name-only -- #{Array(paths).join(' ')} >/dev/null 2>&1")
end
short_log_message(revision) click to toggle source

Get most recent commit message for revision.

# File lib/engineyard-serverside/source/git.rb, line 31
def short_log_message(revision)
  run_and_output("#{git} log --pretty=oneline --abbrev-commit -n 1 '#{revision}'").strip
end
update_repository_cache() click to toggle source
# File lib/engineyard-serverside/source/git.rb, line 35
def update_repository_cache
  unless fetch && checkout
    shell.fatal "git checkout #{to_checkout} failed."
    raise "git checkout #{to_checkout} failed."
  end
end

Protected Instance Methods

checkout() click to toggle source

Internal: Returns .

# File lib/engineyard-serverside/source/git.rb, line 46
def checkout
  shell.status "Deploying revision #{short_log_message(to_checkout)}"
  in_source_cache do
    (run_and_success?("git checkout --force #{quiet} '#{to_checkout}'") ||
      run_and_success?("git reset --hard #{quiet} '#{to_checkout}'")) &&
      run_and_success?("git submodule sync") &&
      run_and_success?("git submodule update --init --recursive") &&
      run_and_success?("git clean -dfq")
  end
end
clean_local_branch() click to toggle source

Remove a local branch with the same name as a branch that is being checked out. If there is already a local branch with the same name, then git checkout will checkout the possibly out-of-date local branch instead of the most current remote.

# File lib/engineyard-serverside/source/git.rb, line 61
def clean_local_branch
  run_and_success?("#{git} show-branch #{ref} > /dev/null 2>&1 && #{git} branch -D #{ref} > /dev/null 2>&1")
end
fetch() click to toggle source

Prune and then fetch origin

OR, if origin has changed locations

Remove and reclone the repository from url

# File lib/engineyard-serverside/source/git.rb, line 70
def fetch
  run_and_success?(fetch_command)
end
fetch_command() click to toggle source

Pruning before fetching makes sure that branches removed from remote are removed locally. This hopefully prevents problems where a branch name collides with a branch directory name (among other problems).

Note that –prune doesn’t succeed at doing this, even though it seems like it should.

# File lib/engineyard-serverside/source/git.rb, line 80
def fetch_command
  if usable_repository?
    prune_c = "#{git} remote prune origin 2>&1"
    fetch_c = "#{git} fetch --force --prune --update-head-ok #{quiet} origin '+refs/heads/*:refs/remotes/origin/*' '+refs/tags/*:refs/tags/*' 2>&1"

    "#{prune_c} && #{fetch_c}"
  else
    "rm -rf #{repository_cache} && git clone #{quiet} #{uri} #{repository_cache} 2>&1"
  end
end
git() click to toggle source
# File lib/engineyard-serverside/source/git.rb, line 91
def git
  @git ||= "git --git-dir #{repository_cache}/.git --work-tree #{repository_cache}"
end
quiet() click to toggle source
# File lib/engineyard-serverside/source/git.rb, line 117
def quiet
  @quiet ||= opts[:verbose] ? '' : '--quiet'
end
remote_branch?() click to toggle source
# File lib/engineyard-serverside/source/git.rb, line 113
def remote_branch?
  run_and_success?("#{git} show-branch origin/#{ref} > /dev/null 2>&1")
end
to_checkout() click to toggle source

Internal: .

Returns .

# File lib/engineyard-serverside/source/git.rb, line 106
def to_checkout
  @to_checkout ||= begin
    clean_local_branch
    remote_branch? ? "origin/#{ref}" : ref
  end
end
usable_repository?() click to toggle source

Internal: Check for valid git repository.

Returns a boolean.

# File lib/engineyard-serverside/source/git.rb, line 98
def usable_repository?
  repository_cache.directory? &&
    run_and_output("#{git} remote -v | grep origin").include?(uri)
end