Skip to content

Commit

Permalink
Merge pull request #218 from DataDog/anmarchenko/git_unshallowing_fal…
Browse files Browse the repository at this point in the history
…lbacks

[SDTEST-126] add different fallbacks for unshallowing remotes
  • Loading branch information
anmarchenko committed Aug 16, 2024
2 parents d56ec05 + c5ac583 commit 1e85028
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
45 changes: 32 additions & 13 deletions lib/datadog/ci/git/local_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,23 +238,42 @@ def self.git_unshallow
Telemetry.git_command(Ext::Telemetry::Command::UNSHALLOW)
res = nil

unshallow_command =
"git fetch " \
"--shallow-since=\"1 month ago\" " \
"--update-shallow " \
"--filter=\"blob:none\" " \
"--recurse-submodules=no " \
"$(git config --default origin --get clone.defaultRemoteName)"

unshallow_remotes = [
"$(git rev-parse HEAD)",
"$(git rev-parse --abbrev-ref --symbolic-full-name @{upstream})",
nil
]

duration_ms = Core::Utils::Time.measure(:float_millisecond) do
res = exec_git_command(
"git fetch " \
"--shallow-since=\"1 month ago\" " \
"--update-shallow " \
"--filter=\"blob:none\" " \
"--recurse-submodules=no " \
"$(git config --default origin --get clone.defaultRemoteName) $(git rev-parse HEAD)"
)
unshallow_remotes.each do |remote|
unshallowing_errored = false

res =
begin
exec_git_command(
"#{unshallow_command} #{remote}"
)
rescue => e
log_failure(e, "git unshallow")
telemetry_track_error(e, Ext::Telemetry::Command::UNSHALLOW)
unshallowing_errored = true
nil
end

break unless unshallowing_errored
end
end
Telemetry.git_command_ms(Ext::Telemetry::Command::UNSHALLOW, duration_ms)

Telemetry.git_command_ms(Ext::Telemetry::Command::UNSHALLOW, duration_ms)
res
rescue => e
log_failure(e, "git unshallow")
telemetry_track_error(e, Ext::Telemetry::Command::UNSHALLOW)
nil
end

# makes .exec_git_command private to make sure that this method
Expand Down
32 changes: 28 additions & 4 deletions spec/datadog/ci/git/local_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,34 @@ def with_clone_git_dir
with_clone_git_dir { described_class.git_commits }
end

it "unshallows the repository" do
expect(subject).to be_truthy
# additional commits plus the initial commit
expect(commits.size).to eq(commits_count + 1)
context "successful from the first try" do
before do
expect(Open3).to receive(:capture2e).and_call_original.at_most(2).times
end

it "unshallows the repository" do
expect(subject).to be_truthy
# additional commits plus the initial commit
expect(commits.size).to eq(commits_count + 1)
end
end

context "when unshallow command fails" do
before do
allow(Open3).to receive(:capture2e).and_call_original
allow(Open3).to receive(:capture2e)
.with("git fetch --shallow-since=\"1 month ago\" --update-shallow --filter=\"blob:none\" --recurse-submodules=no $(git config --default origin --get clone.defaultRemoteName) $(git rev-parse HEAD)", stdin_data: nil)
.and_return(["error", double(success?: false, to_i: 1)])
end

it "still unshallows the repository using the fallback command" do
expect(subject).to be_truthy
# additional commits plus the initial commit
expect(commits.size).to eq(commits_count + 1)
end

# it signals error to the telemetry
it_behaves_like "emits telemetry metric", :inc, "git.command_errors", 1
end
end
end
Expand Down

0 comments on commit 1e85028

Please sign in to comment.