Skip to content

Commit

Permalink
Make PR clones shallow and don't clone the branch first
Browse files Browse the repository at this point in the history
Interim fix for travis-ci/travis-ci#7589
  • Loading branch information
aidanhs committed May 15, 2017
1 parent b0d3b73 commit e224de7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
20 changes: 8 additions & 12 deletions lib/travis/build/git/clone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def apply
clone_or_fetch
delete_netrc
sh.cd dir
fetch_ref if fetch_ref?
checkout
end
end
Expand All @@ -22,36 +21,33 @@ def apply

def clone_or_fetch
sh.if "! -d #{dir}/.git" do
sh.cmd "git clone #{clone_args} #{data.source_url} #{dir}", assert: true, retry: true
sh.cmd "mkdir #{dir}", assert: true
sh.cd dir
# Need to manually perform the fetch steps - https://github.com/travis-ci/travis-ci/issues/7589
sh.cmd "git init", assert: true, timing: false
sh.cmd "git remote add origin #{data.source_url}", assert: true, timing: false
sh.cmd "git fetch #{fetch_args} origin +#{data.ref || branch}:", assert: false, retry: true
if github?
sh.if "$? -ne 0" do
sh.echo "Failed to clone from GitHub.", ansi: :red
sh.echo "Checking GitHub status (https://status.github.com/api/last-message.json):"
sh.raw "curl -sL https://status.github.com/api/last-message.json | jq -r .[]"
end
end
sh.cd ".."
end
sh.else do
sh.cmd "git -C #{dir} fetch origin", assert: true, retry: true
sh.cmd "git -C #{dir} reset --hard", assert: true, timing: false
end
end

def fetch_ref
sh.cmd "git fetch origin +#{data.ref}:", assert: true, retry: true
end

def fetch_ref?
!!data.ref
end

def checkout
sh.cmd "git checkout -qf #{data.pull_request ? 'FETCH_HEAD' : data.commit}", timing: false
end

def clone_args
def fetch_args
args = "--depth=#{depth}"
args << " --branch=#{branch}" unless data.ref
args << " --quiet" if quiet?
args
end
Expand Down
28 changes: 14 additions & 14 deletions spec/build/git/clone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,24 @@
end


describe 'when the repository is not yet cloned' do
let(:args) { "--depth=#{depth} --branch=#{branch.shellescape}" }
let(:cmd) { "git clone #{args} #{url} #{dir}" }
describe 'when the repository is not yet cloned and is a branch' do
let(:args) { "--depth=#{depth}" }
let(:cmd) { "git fetch #{args} origin +#{branch.shellescape}:" }
subject { sexp_find(sexp, [:if, "! -d #{dir}/.git"]) }

let(:clone) { [:cmd, cmd, assert: true, echo: true, retry: true, timing: true] }
let(:init) { [:cmd, "git init", assert: true, echo: true] }
let(:fetch) { [:cmd, cmd, echo: true, retry: true, timing: true] }

describe 'with no depth specified' do
it { should include_sexp clone }
it { should include_sexp init }
it { should include_sexp fetch }
end

describe 'with a custom depth' do
let(:depth) { 1 }
before { payload[:config][:git]['depth'] = depth }
it { should include_sexp clone }
it { should include_sexp init }
it { should include_sexp fetch }
end

describe 'with lfs_skip_smudge true' do
Expand All @@ -106,15 +109,15 @@

describe 'escapes the branch name' do
before { payload[:job][:branch] = 'foo->bar' }
it { should include_sexp clone }
it { should include_sexp fetch }
end

context 'when git.quiet is true' do
before :each do
payload[:config][:git].merge!({ quiet: true })
end
let(:args) { "--depth=#{depth} --branch=#{branch.shellescape} --quiet" }
it { should include_sexp clone }
let(:args) { "--depth=#{depth} --quiet" }
it { should include_sexp fetch }
end
end

Expand All @@ -129,7 +132,8 @@
end

let(:cd) { [:cd, 'travis-ci/travis-ci', echo: true] }
let(:fetch_ref) { [:cmd, %r(git fetch origin \+[\w/]+:), assert: true, echo: true, retry: true, timing: true] }
let(:fetch_args) { "--depth=#{depth}" }
let(:fetch_ref) { [:cmd, %r(git fetch #{fetch_args} origin \+[\w/]+:), echo: true, retry: true, timing: true] }
let(:checkout_push) { [:cmd, 'git checkout -qf 313f61b', assert: true, echo: true] }
let(:checkout_pull) { [:cmd, 'git checkout -qf FETCH_HEAD', assert: true, echo: true] }

Expand All @@ -140,10 +144,6 @@
it { should include_sexp fetch_ref }
end

describe 'with no ref given' do
it { should_not include_sexp fetch_ref }
end

describe 'checks out the given commit for a push request' do
before { payload[:job][:pull_request] = false }
it { should include_sexp checkout_push }
Expand Down

0 comments on commit e224de7

Please sign in to comment.