Class: Capistrano::Deploy::SCM::Git

Inherits:
Base
  • Object
show all
Defined in:
lib/alpha_omega/deploy/scm/git.rb

Overview

An SCM module for using Git as your source control tool with Capistrano 2.0.

Assumes you are using a shared Git repository.

Parts of this plugin borrowed from Scott Chacon’s version, which I found on the Capistrano mailing list but failed to be able to get working.

FEATURES:

* Very simple, only requiring 2 lines in your deploy.rb.
* Can deploy different branches, tags, or any SHA1 easily.
* Supports :scm_command Capistrano directive.

CONFIGURATION


Use this plugin by adding the following line in your config/deploy.rb:

set :scm, :git

Set :repository to the path of your Git repo:

set :repository, "someuser@somehost:/home/myproject"

The above two options are required to be set, the ones below are optional.

You may set :branch, which is the reference to the branch, tag, or any SHA1 you are deploying, for example:

set :branch, "master"

Otherwise, HEAD is assumed. I strongly suggest you set this. HEAD is not always the best assumption.

The :scm_command configuration variable, if specified, will be used as the full path to the git executable on the remote machine:

set :scm_command, "/opt/local/bin/git"

AUTHORS


Garry Dolley scie.nti.st Contributions by Geoffrey Grosenbach topfunky.com

Scott Chacon http://jointheconversation.org
            Alex Arnell http://twologic.com
                     and Phillip Goldenburg

Instance Attribute Summary

Attributes inherited from Base

#configuration

Instance Method Summary collapse

Methods inherited from Base

#command, default_command, #initialize, #local, #local?, #next_revision, #scm

Constructor Details

This class inherits a constructor from Capistrano::Deploy::SCM::Base

Instance Method Details

#checkout(revision, destination) ⇒ Object

Performs a clone on the remote machine, then checkout on the branch you want to deploy.



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/alpha_omega/deploy/scm/git.rb', line 72

def checkout(revision, destination)
  git = command

  execute = []

  execute << "[[ -d #{destination}/.git ]] || #{git} clone #{verbose} #{variable(:repository)} #{destination}"
  execute << "cd #{destination} && #{git} fetch -q && #{git} checkout -q --force #{revision}"
  execute << "cd #{destination} && #{git} reset --hard #{revision} && #{git} submodule update --init --recursive"

  execute
end

#diff(from, to = nil) ⇒ Object

Returns a string of diffs between two revisions



85
86
87
88
# File 'lib/alpha_omega/deploy/scm/git.rb', line 85

def diff(from, to=nil)
  from << "..#{to}" if to
  scm :diff, from
end

#headObject

When referencing “head”, use the branch we want to deploy or, by default, Git’s reference of HEAD (the latest changeset in the default branch, usually called “master”).



66
67
68
# File 'lib/alpha_omega/deploy/scm/git.rb', line 66

def head
  variable(:branch) || 'HEAD'
end

#log(from, to = nil) ⇒ Object

Returns a log of changes between the two revisions (inclusive).



91
92
93
# File 'lib/alpha_omega/deploy/scm/git.rb', line 91

def log(from, to=nil)
  scm :log, "#{from}..#{to}"
end

#query_revision(revision) ⇒ Object

Getting the actual commit id, in case we were passed a tag or partial sha or something - it will return the sha if you pass a sha, too

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/alpha_omega/deploy/scm/git.rb', line 97

def query_revision(revision)
  raise ArgumentError, "Deploying remote branches is no longer supported.  Specify the remote branch as a local branch for the git repository you're deploying from (ie: '#{revision.gsub('origin/', '')}' rather than '#{revision}')." if revision =~ /^origin\//
  return revision if revision =~ /^[0-9a-f]{40}$/
  command = scm('ls-remote', repository, revision)
  result = yield(command)
  revdata = result.split(/[\t\n]/)
  newrev = nil
  revdata.each_slice(2) do |refs|
    rev, ref = *refs
    if ref.sub(/refs\/.*?\//, '').strip == revision.to_s
      newrev = rev
      break
    end
  end
  raise "Unable to resolve revision for '#{revision}' on repository '#{repository}'." unless newrev =~ /^[0-9a-f]{40}$/
  return newrev
end