Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[match] git storage: allow simultaneous usage of clone_branch_directly and shallow_clone #21716

Merged
merged 4 commits into from Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions match/lib/match/storage/git_storage.rb
Expand Up @@ -92,9 +92,14 @@ def download
command << " -c http.extraheader='Authorization: Bearer #{self.git_bearer_authorization}'" unless self.git_bearer_authorization.nil?

if self.shallow_clone
command << " --depth 1 --no-single-branch"
elsif self.clone_branch_directly
command << " --depth 1"
end

if self.clone_branch_directly
command += " -b #{self.branch.shellescape} --single-branch"
elsif self.shallow_clone
# shallow clone all branches if not cloning branch directly
command += " --no-single-branch"
end

command = command_from_private_key(command) unless self.git_private_key.nil?
Expand Down
40 changes: 40 additions & 0 deletions match/spec/storage/git_storage_spec.rb
Expand Up @@ -43,6 +43,46 @@
expect(File.exist?(File.join(storage.working_directory, 'README.md'))).to eq(false) # because the README is being added when committing the changes
end

it "directly clones a single branch wtih shallow clone" do
nekrich marked this conversation as resolved.
Show resolved Hide resolved
# GIVEN
Copy link
Collaborator

@lacostej lacostej Dec 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about being a bit more DRY and move those initializations in one place, either using a before or an around under the same describe ? I mention around in case the garbage collector kicks in.

describe "#download" do
  before(:each) do
    @path = Dir.mktmpdir # to have access to the actual path
    allow(Dir).to receive(:mktmpdir).and_return(@path)
  end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored tests in 85c8ace.
Combined your and @rogerluan comments.

Git spec helper:

  1. branch checkout commands were repeated all over the spec.
  2. Command execution check is too long, imo. Added a handful wrapper that accepts a single command string or array of commands.

Spec:

  1. Default git url and branch.
  2. Default temp dir stub.
  3. Shorter test cases.

path = Dir.mktmpdir # to have access to the actual path
allow(Dir).to receive(:mktmpdir).and_return(path)

git_url = "https://github.com/fastlane/fastlane/tree/master/certificates"
git_branch = "master"

storage = Match::Storage::GitStorage.new(
git_url: git_url,
branch: git_branch,
# Test case:
clone_branch_directly: true,
shallow_clone: true
)

# EXPECTATIONS
expected_commands = [
"git clone #{git_url.shellescape} #{path.shellescape} --depth 1 -b #{git_branch} --single-branch",
"git --no-pager branch --list origin/#{git_branch} --no-color -r",
"git checkout --orphan #{git_branch}",
"git reset --hard"
]
expected_commands.each do |command|
expect(FastlaneCore::CommandExecutor).to receive(:execute).once.with({
command: command,
print_all: nil,
print_command: nil
}).and_return("")
end

# WHEN
storage.download

# THEN
# expected_commands above are executed
expect(File.directory?(storage.working_directory)).to eq(true)
expect(File.exist?(File.join(storage.working_directory, 'README.md'))).to eq(false)
end

it "clones the repo (not shallow)" do
path = Dir.mktmpdir # to have access to the actual path
expect(Dir).to receive(:mktmpdir).and_return(path)
Expand Down