Skip to content

Commit ef249c8

Browse files
refactor: separate fetching and unshallowing functionality
1 parent 9c4c478 commit ef249c8

File tree

8 files changed

+71
-66
lines changed

8 files changed

+71
-66
lines changed

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/CiVisibilityRepoServices.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ static PullRequestInfo buildPullRequestInfo(
130130
if (Strings.isNotBlank(headSha)) {
131131
// if head sha present try to populate author, committer and message info through git client
132132
try {
133-
gitRepoUnshallow.unshallow(headSha);
133+
if (!gitClient.isCommitPresent(headSha)) {
134+
gitClient.fetchCommit(headSha);
135+
}
134136
CommitInfo commitInfo = gitClient.getCommitInfo(headSha);
135137
return PullRequestInfo.merge(ciInfo, new PullRequestInfo(null, null, commitInfo, null));
136138
} catch (Exception ignored) {

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/ExecutionSettingsFactoryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ private Diff getPullRequestDiff(boolean impactedTestsDetectionEnabled, String de
421421
try {
422422
if (repositoryRoot != null) {
423423
// ensure repo is not shallow before attempting to get git diff
424-
gitRepoUnshallow.unshallow(null);
424+
gitRepoUnshallow.unshallow();
425425

426426
String baseCommitSha = pullRequestInfo.getPullRequestBaseBranchSha();
427427
if (baseCommitSha == null) {

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/GitClient.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ public interface GitClient {
2020
boolean isCommitPresent(String commitReference)
2121
throws IOException, TimeoutException, InterruptedException;
2222

23-
void unshallow(@Nullable String remoteCommitReference, boolean unshallowUntilCommit)
23+
void unshallow(@Nullable String remoteCommitReference)
24+
throws IOException, TimeoutException, InterruptedException;
25+
26+
void fetchCommit(String remoteCommitReference)
2427
throws IOException, TimeoutException, InterruptedException;
2528

2629
@Nullable

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/GitDataUploaderImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private void uploadGitData() {
9191
LOGGER.debug("Starting git data upload, {}", gitClient);
9292

9393
if (!config.isCiVisibilityGitUnshallowDefer()) {
94-
gitRepoUnshallow.unshallow(null);
94+
gitRepoUnshallow.unshallow();
9595
}
9696

9797
GitInfo gitInfo = gitInfoProvider.getGitInfo(repoRoot);
@@ -112,7 +112,7 @@ private void uploadGitData() {
112112
return;
113113
}
114114

115-
if (config.isCiVisibilityGitUnshallowDefer() && gitRepoUnshallow.unshallow(null)) {
115+
if (config.isCiVisibilityGitUnshallowDefer() && gitRepoUnshallow.unshallow()) {
116116
latestCommits = gitClient.getLatestCommits();
117117
commitsToSkip = gitDataApi.searchCommits(remoteUrl, latestCommits);
118118
}

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/GitRepoUnshallow.java

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import datadog.trace.civisibility.utils.ShellCommandExecutor;
55
import java.io.IOException;
66
import java.util.concurrent.TimeoutException;
7-
import javax.annotation.Nullable;
87
import org.slf4j.Logger;
98
import org.slf4j.LoggerFactory;
109

@@ -20,47 +19,29 @@ public GitRepoUnshallow(Config config, GitClient gitClient) {
2019
this.gitClient = gitClient;
2120
}
2221

23-
/**
24-
* Unshallows git repo up to a specific boundary commit, if provided, or up to the time limit
25-
* configured in the git client if not. Won't perform an unshallow action when a boundary is
26-
* provided and the object is already present, or if the repository is already unshallowed.
27-
*
28-
* @param boundaryCommitSha used as boundary for the unshallowing if provided.
29-
* @return false if unshallowing is not enabled or unnecessary, true otherwise
30-
*/
31-
public synchronized boolean unshallow(@Nullable String boundaryCommitSha)
22+
public synchronized boolean unshallow()
3223
throws IOException, InterruptedException, TimeoutException {
33-
if (!config.isCiVisibilityGitUnshallowEnabled()
34-
|| (boundaryCommitSha != null && gitClient.isCommitPresent(boundaryCommitSha))
35-
|| !gitClient.isShallow()) {
24+
if (!config.isCiVisibilityGitUnshallowEnabled() || !gitClient.isShallow()) {
3625
return false;
3726
}
3827

3928
long unshallowStart = System.currentTimeMillis();
40-
if (boundaryCommitSha != null) {
41-
try {
42-
gitClient.unshallow(boundaryCommitSha, true);
43-
} catch (ShellCommandExecutor.ShellCommandFailedException e) {
44-
LOGGER.debug("Could not unshallow to specific boundary {}", boundaryCommitSha, e);
45-
}
46-
} else {
47-
try {
48-
gitClient.unshallow(GitClient.HEAD, false);
49-
} catch (ShellCommandExecutor.ShellCommandFailedException e) {
50-
LOGGER.debug(
51-
"Could not unshallow using HEAD - assuming HEAD points to a local commit that does not exist in the remote repo",
52-
e);
53-
}
29+
try {
30+
gitClient.unshallow(GitClient.HEAD);
31+
} catch (ShellCommandExecutor.ShellCommandFailedException e) {
32+
LOGGER.debug(
33+
"Could not unshallow using HEAD - assuming HEAD points to a local commit that does not exist in the remote repo",
34+
e);
35+
}
5436

55-
try {
56-
String upstreamBranch = gitClient.getUpstreamBranchSha();
57-
gitClient.unshallow(upstreamBranch, false);
58-
} catch (ShellCommandExecutor.ShellCommandFailedException e) {
59-
LOGGER.debug(
60-
"Could not unshallow using upstream branch - assuming currently checked out local branch does not track any remote branch",
61-
e);
62-
gitClient.unshallow(null, false);
63-
}
37+
try {
38+
String upstreamBranch = gitClient.getUpstreamBranchSha();
39+
gitClient.unshallow(upstreamBranch);
40+
} catch (ShellCommandExecutor.ShellCommandFailedException e) {
41+
LOGGER.debug(
42+
"Could not unshallow using upstream branch - assuming currently checked out local branch does not track any remote branch",
43+
e);
44+
gitClient.unshallow(null);
6445
}
6546
LOGGER.debug("Repository unshallowing took {} ms", System.currentTimeMillis() - unshallowStart);
6647
return true;

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/NoOpGitClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public boolean isCommitPresent(String commitReference) {
2626
}
2727

2828
@Override
29-
public void unshallow(@Nullable String remoteCommitReference, boolean unshallowUntilCommit) {
29+
public void unshallow(@Nullable String remoteCommitReference) {
3030
// no op
3131
}
3232

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/ShellGitClient.java

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,30 +161,21 @@ public String getUpstreamBranchSha() throws IOException, TimeoutException, Inter
161161
*
162162
* @param remoteCommitReference The commit to fetch from the remote repository, so local repo will
163163
* be updated with this commit and its ancestors. If {@code null}, everything will be fetched.
164-
* @param unshallowUntilCommit Only unshallow up until the commit provided, or use the time limit
165-
* configured if not. Ignored if no reference is provided.
166164
* @throws IOException If an error was encountered while writing command input or reading output
167165
* @throws TimeoutException If timeout was reached while waiting for Git command to finish
168166
* @throws InterruptedException If current thread was interrupted while waiting for Git command to
169167
* finish
170168
*/
171169
@Override
172-
public void unshallow(@Nullable String remoteCommitReference, boolean unshallowUntilCommit)
170+
public void unshallow(@Nullable String remoteCommitReference)
173171
throws IOException, TimeoutException, InterruptedException {
174172
executeCommand(
175173
Command.UNSHALLOW,
176174
() -> {
177175
String remote = getRemoteName();
178176

179177
// refetch data from the server for the given period of time
180-
if (remoteCommitReference != null) {
181-
if (!GitUtils.isValidRef(remoteCommitReference)) {
182-
return (Void) null;
183-
}
184-
String boundaryArg =
185-
unshallowUntilCommit
186-
? "--no-write-fetch-head"
187-
: String.format("--shallow-since='%s'", latestCommitsSince);
178+
if (remoteCommitReference != null && GitUtils.isValidRef(remoteCommitReference)) {
188179
String commitSha = getSha(remoteCommitReference);
189180
commandExecutor.executeCommand(
190181
ShellCommandExecutor.OutputParser.IGNORE,
@@ -193,7 +184,7 @@ public void unshallow(@Nullable String remoteCommitReference, boolean unshallowU
193184
"--update-shallow",
194185
"--filter=blob:none",
195186
"--recurse-submodules=no",
196-
boundaryArg,
187+
String.format("--shallow-since='%s'", latestCommitsSince),
197188
remote,
198189
commitSha);
199190
} else {
@@ -212,6 +203,40 @@ public void unshallow(@Nullable String remoteCommitReference, boolean unshallowU
212203
});
213204
}
214205

206+
/**
207+
* Fetches provided commit object from the server.
208+
*
209+
* @param commitReference Commit to fetch from the remote repository
210+
* @throws IOException If an error was encountered while writing command input or reading output
211+
* @throws TimeoutException If timeout was reached while waiting for Git command to finish
212+
* @throws InterruptedException If current thread was interrupted while waiting for Git command to
213+
*/
214+
@Override
215+
public void fetchCommit(String commitReference)
216+
throws IOException, TimeoutException, InterruptedException {
217+
if (!GitUtils.isValidRef(commitReference)) {
218+
return;
219+
}
220+
executeCommand(
221+
Command.OTHER,
222+
() -> {
223+
String remote = getRemoteName();
224+
225+
commandExecutor.executeCommand(
226+
ShellCommandExecutor.OutputParser.IGNORE,
227+
"git",
228+
"fetch",
229+
"--update-shallow",
230+
"--filter=blob:none",
231+
"--recurse-submodules=no",
232+
"--no-write-fetch-head",
233+
remote,
234+
commitReference);
235+
236+
return (Void) null;
237+
});
238+
}
239+
215240
/**
216241
* Returns the absolute path of the .git directory.
217242
*

dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/git/tree/GitClientTest.groovy

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,41 +84,35 @@ class GitClientTest extends Specification {
8484
commits.size() == 1
8585

8686
when:
87-
gitClient.unshallow(remoteSha, false)
87+
gitClient.unshallow(remoteSha)
8888
shallow = gitClient.isShallow()
8989
commits = gitClient.getLatestCommits()
9090

9191
then:
92-
shallow == isShallow
93-
commits.size() == numCommits
92+
!shallow
93+
commits.size() == 10
9494

9595
where:
96-
remoteSha | isShallow | numCommits
97-
GitClient.HEAD | false | 10
98-
null | false | 10
96+
remoteSha << [GitClient.HEAD, null]
9997
}
10098

101-
def "test unshallow using commit as boundary"() {
99+
def "test commit fetch"() {
102100
given:
103101
givenGitRepo("ci/git/shallow/git")
104102

105103
when:
106104
def commit = "f4377e97f10c2d58696192b170b2fef2a8464b04"
107105
def gitClient = givenGitClient()
108-
def shallow = gitClient.isShallow()
109106
def isPresent = gitClient.isCommitPresent(commit)
110107

111108
then:
112-
shallow
113109
!isPresent
114110

115111
when:
116-
gitClient.unshallow(commit, true)
117-
shallow = gitClient.isShallow()
112+
gitClient.fetchCommit(commit)
118113
isPresent = gitClient.isCommitPresent(commit)
119114

120115
then:
121-
shallow
122116
isPresent
123117
}
124118

0 commit comments

Comments
 (0)