Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8923

Show
Ignore:
Timestamp:
02/22/08 03:38:08 (5 months ago)
Author:
minam
Message:

Distributed git support for better operability with remote_cache strategy (closes #11137)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tools/capistrano/CHANGELOG

    r8920 r8923  
    11*SVN* 
     2 
     3* Distributed git support for better operability with remote_cache strategy [voidlock] 
    24 
    35* Use a default line length in help text if line length is otherwise too small [Jamis Buck] 
  • tools/capistrano/lib/capistrano/recipes/deploy/scm/git.rb

    r8901 r8923  
    5050      # or any SHA1 you are deploying, for example: 
    5151      # 
    52       #   set :branch, "origin/master" 
     52      #   set :branch, "master" 
    5353      # 
    5454      # Otherwise, HEAD is assumed.  I strongly suggest you set this.  HEAD is 
    5555      # not always the best assumption. 
     56      # 
     57      # You may also set <tt>:remote</tt>, which will be used as a name for remote 
     58      # tracking of repositories. This option is intended for use with the 
     59      # <tt>:remote_cache</tt> strategy in a distributed git environment. 
     60      # 
     61      # For example in the projects <tt>config/deploy.rb</tt>: 
     62      # 
     63      #   set :repository, "#{scm_user}@somehost:~/projects/project.git" 
     64      #   set :remote, "#{scm_user}" 
     65      # 
     66      # Then each person with deploy priveledges can add the following to their 
     67      # local <tt>~/.caprc</tt> file: 
     68      # 
     69      #   set :scm_user, 'someuser' 
     70      # 
     71      # Now any time a person deploys the project, their repository will be 
     72      # setup as a remote git repository within the cached repository. 
    5673      # 
    5774      # The <tt>:scm_command</tt> configuration variable, if specified, will 
     
    7996      # 
    8097      # For faster clone, you can also use shallow cloning.  This will set the 
    81       # '--depth' flag using the depth specified.  This *cannot* be used  
     98      # '--depth' flag using the depth specified.  This *cannot* be used 
    8299      # together with the :remote_cache strategy 
    83100      # 
     
    89106      # Garry Dolley http://scie.nti.st 
    90107      # Contributions by Geoffrey Grosenbach http://topfunky.com 
    91       #              and Scott Chacon http://jointheconversation.org 
    92        
     108      #              Scott Chacon http://jointheconversation.org 
     109      #                      and Alex Arnell http://twologic.com 
     110 
    93111      class Git < Base 
    94112        # Sets the default command name for this SCM on your *local* machine. 
     
    103121        end 
    104122 
     123        def origin 
     124          configuration[:remote] || 'origin' 
     125        end 
     126 
    105127        # Performs a clone on the remote machine, then checkout on the branch 
    106128        # you want to deploy. 
    107129        def checkout(revision, destination) 
    108           git      = command 
    109  
    110           branch   = head 
    111  
    112           fail "No branch specified, use for example 'set :branch, \"origin/master\"' in your deploy.rb" unless branch 
     130          git    = command 
     131          remote = origin 
     132 
     133          args = [] 
     134          args << "-o #{remote}" unless remote == 'origin' 
     135          if depth = configuration[:git_shallow_clone] 
     136            args << "--depth #{depth}" 
     137          end 
    113138 
    114139          execute = [] 
    115           if depth = configuration[:git_shallow_clone] 
    116             execute << "#{git} clone --depth #{depth} #{configuration[:repository]} #{destination}" 
     140          if args.empty? 
     141            execute << "#{git} clone #{configuration[:repository]} #{destination}" 
    117142          else 
    118             execute  << "#{git} clone #{configuration[:repository]} #{destination}" 
    119           end 
    120  
    121           execute << "cd #{destination}" 
    122           execute << "#{git} checkout #{branch}"  
     143            execute << "#{git} clone #{args.join(' ')} #{configuration[:repository]} #{destination}" 
     144          end 
     145 
     146          # checkout into a local branch rather than a detached HEAD 
     147          execute << "cd #{destination} && #{git} checkout -b deploy #{revision}" 
     148           
    123149          if configuration[:git_enable_submodules] 
    124             execute << "#{git} submodule init"  
    125             execute << "#{git} submodule update"  
     150            execute << "#{git} submodule init" 
     151            execute << "#{git} submodule update" 
    126152          end 
    127153 
     
    132158        # deployment strategy 
    133159        def sync(revision, destination) 
    134           git      = command 
     160          git     = command 
     161          remote  = origin 
     162 
    135163          execute = [] 
    136           execute << "cd #{destination} && #{git} fetch origin" 
    137  
    138           if head == 'HEAD' 
    139             execute << "#{git} checkout origin/HEAD" 
    140           else 
    141             execute << "#{git} checkout #{head}" 
    142           end 
    143            
     164          execute << "cd #{destination}" 
     165 
     166          # Use git-config to setup a remote tracking branches. Could use 
     167          # git-remote but it complains when a remote of the same name already 
     168          # exists, git-config will just silenty overwrite the setting every 
     169          # time. This could cause wierd-ness in the remote cache if the url 
     170          # changes between calls, but as long as the repositories are all 
     171          # based from each other it should still work fine. 
     172          if remote != 'origin' 
     173            execute << "#{git} config remote.#{remote}.url #{configuration[:repository]}" 
     174            execute << "#{git} config remote.#{remote}.fetch +refs/heads/*:refs/remotes/#{remote}/*" 
     175          end 
     176 
     177          # since we're in a local branch already, just reset to specified revision rather than merge 
     178          execute << "#{git} fetch #{remote} && #{git} reset --hard #{revision}" 
     179 
    144180          if configuration[:git_enable_submodules] 
    145181            execute << "#{git} submodule update" 
  • tools/capistrano/test/deploy/scm/git_test.rb

    r8904 r8923  
    2020  end 
    2121 
     22  def origin 
     23    asser_equal "origin", @source.origin 
     24    @config[:remote] = "git" 
     25    assert_equal "git", @source.origin 
     26  end 
     27 
    2228  def test_checkout 
    2329    @config[:repository] = "git@somehost.com:project.git" 
    2430    dest = "/var/www" 
    25     assert_equal "git clone git@somehost.com:project.git /var/www && cd /var/www && git checkout HEAD", @source.checkout('Not used', dest) 
     31    rev = 'c2d9e79' 
     32    assert_equal "git clone git@somehost.com:project.git /var/www && cd /var/www && git checkout -b deploy #{rev}", @source.checkout(rev, dest) 
    2633 
    2734    # With branch 
    2835    @config[:branch] = "origin/foo" 
    29     assert_equal "git clone git@somehost.com:project.git /var/www && cd /var/www && git checkout origin/foo", @source.checkout('Not used', dest) 
     36    assert_equal "git clone git@somehost.com:project.git /var/www && cd /var/www && git checkout -b deploy #{rev}", @source.checkout(rev, dest) 
    3037  end 
    3138 
     
    5259  def test_sync 
    5360    dest = "/var/www" 
    54     assert_equal "cd #{dest} && git fetch origin && git checkout origin/HEAD", @source.sync('Not used', dest) 
     61    rev = 'c2d9e79' 
     62    assert_equal "cd #{dest} && git fetch origin && git reset --hard #{rev}", @source.sync(rev, dest) 
    5563 
    5664    # With branch 
    57     @config[:branch] = "origin/foo" 
    58     assert_equal "cd #{dest} && git fetch origin && git checkout origin/foo", @source.sync('Not used', dest) 
     65    @config[:branch] = "foo" 
     66    rev = '92d9e79' # simulate rev change 
     67    assert_equal "cd #{dest} && git fetch origin && git reset --hard #{rev}", @source.sync(rev, dest) 
    5968 
    6069    # With :scm_command 
    61     @config[:scm_command] = "/opt/local/bin/git" 
    62     assert_equal "cd #{dest} && /opt/local/bin/git fetch origin && /opt/local/bin/git checkout origin/foo", @source.sync('Not used', dest) 
     70    git = "/opt/local/bin/git" 
     71    @config[:scm_command] = git 
     72    assert_equal "cd #{dest} && #{git} fetch origin && #{git} reset --hard #{rev}", @source.sync(rev, dest) 
     73  end 
     74 
     75  def test_sync_with_remote 
     76    dest = "/var/www" 
     77    rev = 'c2d9e79' 
     78    remote = "username" 
     79    repository = "git@somehost.com:project.git" 
     80 
     81    @config[:repository] = repository 
     82    @config[:remote] = remote 
     83 
     84    assert_equal "cd #{dest} && git config remote.#{remote}.url #{repository} && git config remote.#{remote}.fetch +refs/heads/*:refs/remotes/#{remote}/* && git fetch #{remote} && git reset --hard #{rev}", @source.sync(rev, dest) 
    6385  end 
    6486 
     
    6789    @config[:git_shallow_clone] = 1 
    6890    dest = "/var/www" 
    69     assert_equal "git clone --depth 1 git@somehost.com:project.git /var/www && cd /var/www && git checkout HEAD", @source.checkout('Not used', dest) 
     91    rev = 'c2d9e79' 
     92    assert_equal "git clone --depth 1 git@somehost.com:project.git /var/www && cd /var/www && git checkout -b deploy #{rev}", @source.checkout(rev, dest) 
    7093 
    7194    # With branch 
    7295    @config[:branch] = "origin/foo" 
    73     assert_equal "git clone --depth 1 git@somehost.com:project.git /var/www && cd /var/www && git checkout origin/foo", @source.checkout('Not used', dest) 
     96    rev = '92d9e79' # simulate rev change 
     97    assert_equal "git clone --depth 1 git@somehost.com:project.git /var/www && cd /var/www && git checkout -b deploy #{rev}", @source.checkout(rev, dest) 
     98  end 
     99 
     100  def test_remote_clone 
     101    @config[:repository] = "git@somehost.com:project.git" 
     102    @config[:remote] = "username" 
     103    dest = "/var/www" 
     104    rev = 'c2d9e79' 
     105    assert_equal "git clone -o username git@somehost.com:project.git /var/www && cd /var/www && git checkout -b deploy #{rev}", @source.checkout(rev, dest) 
     106 
     107    # With branch 
     108    @config[:branch] = "foo" 
     109    assert_equal "git clone -o username git@somehost.com:project.git /var/www && cd /var/www && git checkout -b deploy #{rev}", @source.checkout(rev, dest) 
    74110  end 
    75111