Skip to content
This repository was archived by the owner on Nov 8, 2018. It is now read-only.

Commit a00e1a7

Browse files
committed
support git submodules and depth from in
1 parent 1512f49 commit a00e1a7

File tree

5 files changed

+84
-4
lines changed

5 files changed

+84
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ git config --get pullrequest.basebranch # returns the base branch used for the p
110110

111111
#### Parameters
112112

113+
* `git.depth`: *Optional.* If a positive integer is given, *shallow* clone the
114+
repository using the `--depth` option.
115+
116+
* `git.submodules`: *Optional.* If `none`, submodules will not be
117+
fetched. If specified as a list of paths, only the given paths will be
118+
fetched. If not specified, or if `all` is explicitly specified, all
119+
submodules are fetched.
120+
113121
* `fetch_merge`: *Optional*. If set to `true`, it will fetch what the result of PR
114122
would be otherwise it will fetch the origin branch.
115123
Defaults to `false`.

assets/lib/commands/base.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
require 'octokit'
88
require_relative '../input'
99

10-
1110
module Commands
1211
class Base
1312
attr_reader :input

assets/lib/commands/in.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ def initialize(destination:, input: Input.instance)
1515
end
1616

1717
def output
18-
id = pr['number']
18+
id = pr['number']
1919
branch_ref = "pr-#{pr['head']['ref']}"
2020

2121
raise 'PR has merge conflicts' if pr['mergeable'] == false && fetch_merge
2222

23-
system("git clone --depth 1 #{uri} #{destination} 1>&2")
23+
system("git clone #{depth_flag} #{uri} #{destination} 1>&2")
2424

2525
raise 'git clone failed' unless $CHILD_STATUS.exitstatus.zero?
2626

@@ -29,12 +29,20 @@ def output
2929

3030
system <<-BASH
3131
git checkout #{branch_ref} 1>&2
32-
git submodule update --init --recursive 1>&2
3332
git config --add pullrequest.url #{pr['html_url']} 1>&2
3433
git config --add pullrequest.id #{pr['number']} 1>&2
3534
git config --add pullrequest.branch #{pr['head']['ref']} 1>&2
3635
git config --add pullrequest.basebranch #{pr['base']['ref']} 1>&2
3736
BASH
37+
38+
case input.params.git.submodules
39+
when 'all', nil
40+
system("git submodule update --init --recursive #{depth_flag} 1>&2")
41+
when Array
42+
input.params.git.submodules.each do |path|
43+
system("git submodule update --init --recursive #{depth_flag} #{path} 1>&2")
44+
end
45+
end
3846
end
3947

4048
{
@@ -64,6 +72,14 @@ def remote_ref
6472
def fetch_merge
6573
input.params.fetch_merge
6674
end
75+
76+
def depth_flag
77+
if depth = input.params.git.depth
78+
"--depth #{depth}"
79+
else
80+
'--depth 1'
81+
end
82+
end
6783
end
6884
end
6985

assets/lib/input.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ def method
1212
def merge
1313
Merge.new(self['merge'] || {})
1414
end
15+
16+
def git
17+
OpenStruct.new(self['git'] || {})
18+
end
1519
end
1620

1721
def self.instance(payload: nil)

spec/commands/in_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,57 @@ def dest_dir
156156
end
157157
end
158158
end
159+
160+
fcontext 'with specific `git` params' do
161+
before do
162+
stub_json('https://api.github.com:443/repos/jtarchie/test/pulls/1',
163+
html_url: 'http://example.com', number: 1,
164+
head: { ref: 'foo' },
165+
base: { ref: 'master' })
166+
end
167+
168+
def expect_arg(*args)
169+
allow_any_instance_of(Commands::In).to receive(:system).and_call_original
170+
expect_any_instance_of(Commands::In).to receive(:system).with(*args).and_call_original
171+
end
172+
173+
def dont_expect_arg(*args)
174+
allow_any_instance_of(Commands::In).to receive(:system).and_call_original
175+
expect_any_instance_of(Commands::In).not_to receive(:system).with(*args).and_call_original
176+
end
177+
178+
it 'gets all the submodules' do
179+
expect_arg /git submodule update --init --recursive/
180+
get('version' => { 'ref' => @ref, 'pr' => '1' }, 'source' => { 'uri' => git_uri, 'repo' => 'jtarchie/test' }, 'params' => {})
181+
end
182+
183+
it 'gets all the submodules explicitly' do
184+
expect_arg /git submodule update --init --recursive/
185+
get('version' => { 'ref' => @ref, 'pr' => '1' }, 'source' => { 'uri' => git_uri, 'repo' => 'jtarchie/test' }, 'params' => { 'git' => { 'submodules' => 'all' } })
186+
end
187+
188+
it 'gets no submodules' do
189+
dont_expect_arg /git submodule update --init --recursive/
190+
get('version' => { 'ref' => @ref, 'pr' => '1' }, 'source' => { 'uri' => git_uri, 'repo' => 'jtarchie/test' }, 'params' => { 'git' => { 'submodules' => 'none' } })
191+
end
192+
193+
it 'get submodules with paths' do
194+
expect_arg /git submodule update --init --recursive --depth 1 path1/
195+
expect_arg /git submodule update --init --recursive --depth 1 path2/
196+
get('version' => { 'ref' => @ref, 'pr' => '1' }, 'source' => { 'uri' => git_uri, 'repo' => 'jtarchie/test' }, 'params' => { 'git' => { 'submodules' => %w(path1 path2) } })
197+
end
198+
199+
it 'checkouts everything by depth' do
200+
expect_arg /git submodule update --init --recursive --depth 100 path1/
201+
expect_arg /git clone --depth 100/
202+
get('version' => { 'ref' => @ref, 'pr' => '1' },
203+
'source' => { 'uri' => git_uri, 'repo' => 'jtarchie/test' },
204+
'params' => {
205+
'git' => {
206+
'submodules' => %w(path1 path2),
207+
'depth' => 100
208+
}
209+
})
210+
end
211+
end
159212
end

0 commit comments

Comments
 (0)