Ticket #11137: capistrano-distributed-git.diff
| File capistrano-distributed-git.diff, 9.9 kB (added by voidlock, 7 months ago) |
|---|
-
test/deploy/scm/git_test.rb
old new 19 19 assert_equal "master", @source.head 20 20 end 21 21 22 def origin 23 asser_equal "origin", @source.origin 24 @config[:remote] = "git" 25 assert_equal "git", @source.origin 26 end 27 22 28 def test_checkout 23 29 @config[:repository] = "git@somehost.com:project.git" 24 30 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) 26 33 27 34 # With branch 28 35 @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) 30 37 end 31 38 32 39 def test_diff … … 51 58 52 59 def test_sync 53 60 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) 55 63 56 64 # 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) 59 68 60 69 # 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) 63 73 end 64 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) 85 end 86 65 87 def test_shallow_clone 66 88 @config[:repository] = "git@somehost.com:project.git" 67 89 @config[:git_shallow_clone] = 1 68 90 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) 70 93 71 94 # With branch 72 95 @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) 74 98 end 75 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) 110 end 111 76 112 # Tests from base_test.rb, makin' sure we didn't break anything up there! 77 113 def test_command_should_default_to_default_command 78 114 assert_equal "git", @source.command -
lib/capistrano/recipes/deploy/scm/git.rb
old new 49 49 # You may set <tt>:branch</tt>, which is the reference to the branch, tag, 50 50 # or any SHA1 you are deploying, for example: 51 51 # 52 # set :branch, " origin/master"52 # set :branch, "master" 53 53 # 54 54 # Otherwise, HEAD is assumed. I strongly suggest you set this. HEAD is 55 55 # not always the best assumption. 56 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. 73 # 57 74 # The <tt>:scm_command</tt> configuration variable, if specified, will 58 75 # be used as the full path to the git executable on the *remote* machine: 59 76 # … … 78 95 # set :deploy_via, :remote_cache 79 96 # 80 97 # 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 82 99 # together with the :remote_cache strategy 83 100 # 84 101 # set :git_shallow_clone, 1 … … 88 105 # 89 106 # Garry Dolley http://scie.nti.st 90 107 # 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 93 111 class Git < Base 94 112 # Sets the default command name for this SCM on your *local* machine. 95 113 # Users may override this by setting the :scm_command variable. … … 102 120 configuration[:branch] || 'HEAD' 103 121 end 104 122 123 def origin 124 configuration[:remote] || 'origin' 125 end 126 105 127 # Performs a clone on the remote machine, then checkout on the branch 106 128 # you want to deploy. 107 129 def checkout(revision, destination) 108 git = command 130 git = command 131 remote = origin 109 132 110 branch = head 133 args = [] 134 args << "-o #{remote}" unless remote == 'origin' 135 if depth = configuration[:git_shallow_clone] 136 args << "--depth #{depth}" 137 end 111 138 112 fail "No branch specified, use for example 'set :branch, \"origin/master\"' in your deploy.rb" unless branch113 114 139 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}" 117 142 else 118 execute << "#{git} clone#{configuration[:repository]} #{destination}"143 execute << "#{git} clone #{args.join(' ')} #{configuration[:repository]} #{destination}" 119 144 end 120 145 121 execute << "cd #{destination}" 122 execute << "#{git} checkout #{branch}" 146 # checkout into a local branch rather than a detached HEAD 147 execute << "cd #{destination} && #{git} checkout -b deploy #{revision}" 148 123 149 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" 126 152 end 127 153 128 154 execute.join(" && ") … … 131 157 # Merges the changes to 'head' since the last fetch, for remote_cache 132 158 # deployment strategy 133 159 def sync(revision, destination) 134 git = command 160 git = command 161 remote = origin 162 135 163 execute = [] 136 execute << "cd #{destination} && #{git} fetch origin"164 execute << "cd #{destination}" 137 165 138 if head == 'HEAD' 139 execute << "#{git} checkout origin/HEAD" 140 else 141 execute << "#{git} checkout #{head}" 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}/*" 142 175 end 143 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 144 180 if configuration[:git_enable_submodules] 145 181 execute << "#{git} submodule update" 146 182 end