From 17aa4e1f645268677a3dde6e6bcb564d9b1b8170 Mon Sep 17 00:00:00 2001 From: Aidan Hobson Sayers Date: Thu, 13 Apr 2017 17:33:24 +0100 Subject: [PATCH] Make PR clones shallow and don't clone the branch first Interim fix for travis-ci/travis-ci#7589 --- lib/travis/build/git/clone.rb | 20 ++++++++------------ spec/build/git/clone_spec.rb | 28 ++++++++++++++-------------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/lib/travis/build/git/clone.rb b/lib/travis/build/git/clone.rb index a91c4fa6f8..b028918f10 100644 --- a/lib/travis/build/git/clone.rb +++ b/lib/travis/build/git/clone.rb @@ -13,7 +13,6 @@ def apply clone_or_fetch delete_netrc sh.cd dir - fetch_ref if fetch_ref? checkout end end @@ -22,7 +21,12 @@ 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 @@ -30,6 +34,7 @@ def clone_or_fetch 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 @@ -37,21 +42,12 @@ def clone_or_fetch 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 diff --git a/spec/build/git/clone_spec.rb b/spec/build/git/clone_spec.rb index a67e206577..a1ffb3711c 100644 --- a/spec/build/git/clone_spec.rb +++ b/spec/build/git/clone_spec.rb @@ -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 @@ -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 @@ -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] } @@ -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 }