Skip to content

Commit 9c4c478

Browse files
feat: add check for presence of commit before unshallowing
1 parent 0f0698d commit 9c4c478

File tree

5 files changed

+79
-14
lines changed

5 files changed

+79
-14
lines changed

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
@@ -17,7 +17,10 @@ public interface GitClient {
1717

1818
boolean isShallow() throws IOException, TimeoutException, InterruptedException;
1919

20-
void unshallow(@Nullable String remoteCommitReference, boolean shallowUntilCommit)
20+
boolean isCommitPresent(String commitReference)
21+
throws IOException, TimeoutException, InterruptedException;
22+
23+
void unshallow(@Nullable String remoteCommitReference, boolean unshallowUntilCommit)
2124
throws IOException, TimeoutException, InterruptedException;
2225

2326
@Nullable

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ public GitRepoUnshallow(Config config, GitClient gitClient) {
2222

2323
/**
2424
* 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.
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.
2627
*
2728
* @param boundaryCommitSha used as boundary for the unshallowing if provided.
2829
* @return false if unshallowing is not enabled or unnecessary, true otherwise
2930
*/
3031
public synchronized boolean unshallow(@Nullable String boundaryCommitSha)
3132
throws IOException, InterruptedException, TimeoutException {
32-
if (!config.isCiVisibilityGitUnshallowEnabled() || !gitClient.isShallow()) {
33+
if (!config.isCiVisibilityGitUnshallowEnabled()
34+
|| (boundaryCommitSha != null && gitClient.isCommitPresent(boundaryCommitSha))
35+
|| !gitClient.isShallow()) {
3336
return false;
3437
}
3538

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ public boolean isShallow() {
2121
}
2222

2323
@Override
24-
public void unshallow(@Nullable String remoteCommitReference, boolean shallowUntilCommit) {
24+
public boolean isCommitPresent(String commitReference) {
25+
return false;
26+
}
27+
28+
@Override
29+
public void unshallow(@Nullable String remoteCommitReference, boolean unshallowUntilCommit) {
2530
// no op
2631
}
2732

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,38 @@ public boolean isShallow() throws IOException, TimeoutException, InterruptedExce
9999
});
100100
}
101101

102+
/**
103+
* Checks whether the provided reference object is present or not.
104+
*
105+
* @param commitReference to check presence of
106+
* @return {@code true} if object is present, {@code false} otherwise
107+
* @throws IOException If an error was encountered while writing command input or reading output
108+
* @throws TimeoutException If timeout was reached while waiting for Git command to finish
109+
* @throws InterruptedException If current thread was interrupted while waiting for Git command to
110+
*/
111+
@Override
112+
public boolean isCommitPresent(String commitReference)
113+
throws IOException, TimeoutException, InterruptedException {
114+
if (!GitUtils.isValidRef(commitReference)) {
115+
return false;
116+
}
117+
return executeCommand(
118+
Command.OTHER,
119+
() -> {
120+
try {
121+
commandExecutor.executeCommand(
122+
ShellCommandExecutor.OutputParser.IGNORE,
123+
"git",
124+
"cat-file",
125+
"-e",
126+
commitReference + "^{commit}");
127+
return true;
128+
} catch (ShellCommandExecutor.ShellCommandFailedException ignored) {
129+
return false;
130+
}
131+
});
132+
}
133+
102134
/**
103135
* Returns the SHA of the head commit of the upstream (remote tracking) branch for the currently
104136
* checked-out local branch. If the local branch is not tracking any remote branches, a {@link
@@ -129,15 +161,15 @@ public String getUpstreamBranchSha() throws IOException, TimeoutException, Inter
129161
*
130162
* @param remoteCommitReference The commit to fetch from the remote repository, so local repo will
131163
* be updated with this commit and its ancestors. If {@code null}, everything will be fetched.
132-
* @param shallowUntilCommit Only unshallow up until the commit provided, or use the time limit
164+
* @param unshallowUntilCommit Only unshallow up until the commit provided, or use the time limit
133165
* configured if not. Ignored if no reference is provided.
134166
* @throws IOException If an error was encountered while writing command input or reading output
135167
* @throws TimeoutException If timeout was reached while waiting for Git command to finish
136168
* @throws InterruptedException If current thread was interrupted while waiting for Git command to
137169
* finish
138170
*/
139171
@Override
140-
public void unshallow(@Nullable String remoteCommitReference, boolean shallowUntilCommit)
172+
public void unshallow(@Nullable String remoteCommitReference, boolean unshallowUntilCommit)
141173
throws IOException, TimeoutException, InterruptedException {
142174
executeCommand(
143175
Command.UNSHALLOW,
@@ -150,7 +182,7 @@ public void unshallow(@Nullable String remoteCommitReference, boolean shallowUnt
150182
return (Void) null;
151183
}
152184
String boundaryArg =
153-
shallowUntilCommit
185+
unshallowUntilCommit
154186
? "--no-write-fetch-head"
155187
: String.format("--shallow-since='%s'", latestCommitsSince);
156188
String commitSha = getSha(remoteCommitReference);

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class GitClientTest extends Specification {
7070
upstreamBranch == "98b944cc44f18bfb78e3021de2999cdcda8efdf6"
7171
}
7272

73-
def "test unshallow: sha-#remoteSha fetchOnlyCommit-#fetchOnlyCommit"() {
73+
def "test unshallow: sha-#remoteSha"() {
7474
given:
7575
givenGitRepo("ci/git/shallow/git")
7676

@@ -84,7 +84,7 @@ class GitClientTest extends Specification {
8484
commits.size() == 1
8585

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

@@ -93,11 +93,33 @@ class GitClientTest extends Specification {
9393
commits.size() == numCommits
9494

9595
where:
96-
remoteSha | fetchOnlyCommit | isShallow | numCommits
97-
GitClient.HEAD | false | false | 10
98-
null | false | false | 10
99-
"f4377e97f10c2d58696192b170b2fef2a8464b04" | true | true | 1
100-
null | true | false | 10
96+
remoteSha | isShallow | numCommits
97+
GitClient.HEAD | false | 10
98+
null | false | 10
99+
}
100+
101+
def "test unshallow using commit as boundary"() {
102+
given:
103+
givenGitRepo("ci/git/shallow/git")
104+
105+
when:
106+
def commit = "f4377e97f10c2d58696192b170b2fef2a8464b04"
107+
def gitClient = givenGitClient()
108+
def shallow = gitClient.isShallow()
109+
def isPresent = gitClient.isCommitPresent(commit)
110+
111+
then:
112+
shallow
113+
!isPresent
114+
115+
when:
116+
gitClient.unshallow(commit, true)
117+
shallow = gitClient.isShallow()
118+
isPresent = gitClient.isCommitPresent(commit)
119+
120+
then:
121+
shallow
122+
isPresent
101123
}
102124

103125
def "test get git folder"() {

0 commit comments

Comments
 (0)