Skip to content

Commit bef1740

Browse files
feat: update soft unshallow to use --no-write-fetch
1 parent 6b3cf19 commit bef1740

File tree

8 files changed

+53
-64
lines changed

8 files changed

+53
-64
lines changed

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import datadog.trace.api.civisibility.telemetry.tag.Provider;
77
import datadog.trace.api.git.CommitInfo;
88
import datadog.trace.api.git.GitInfoProvider;
9-
import datadog.trace.api.git.PersonInfo;
109
import datadog.trace.civisibility.ci.CIInfo;
1110
import datadog.trace.civisibility.ci.CIProviderInfo;
1211
import datadog.trace.civisibility.ci.CITagsProvider;
@@ -127,23 +126,13 @@ static PullRequestInfo buildPullRequestInfo(
127126

128127
// complete with CI vars if user didn't provide all information
129128
PullRequestInfo ciInfo = PullRequestInfo.merge(userInfo, ciProviderInfo.buildPullRequestInfo());
130-
if (Strings.isNotBlank(ciInfo.getGitCommitHead().getSha())) {
129+
String headSha = ciInfo.getGitCommitHead().getSha();
130+
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(true);
134-
String headSha = ciInfo.getGitCommitHead().getSha();
135-
PersonInfo authorInfo =
136-
new PersonInfo(
137-
gitClient.getAuthorName(headSha),
138-
gitClient.getAuthorEmail(headSha),
139-
gitClient.getAuthorDate(headSha));
140-
PersonInfo committerInfo =
141-
new PersonInfo(
142-
gitClient.getCommitterName(headSha),
143-
gitClient.getCommitterEmail(headSha),
144-
gitClient.getCommitterDate(headSha));
133+
gitRepoUnshallow.unshallow(headSha);
145134
CommitInfo commitInfo =
146-
new CommitInfo(headSha, authorInfo, committerInfo, gitClient.getFullMessage(headSha));
135+
gitClient.getCommitInfo(headSha);
147136
return PullRequestInfo.merge(ciInfo, new PullRequestInfo(null, null, commitInfo, null));
148137
} catch (Exception ignored) {
149138
}

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(false);
424+
gitRepoUnshallow.unshallow(null);
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface GitClient {
1717

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

20-
void unshallow(@Nullable String remoteCommitReference, boolean parentOnly)
20+
void unshallow(@Nullable String remoteCommitReference, boolean onlyFetchCommit)
2121
throws IOException, TimeoutException, InterruptedException;
2222

2323
@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(false);
94+
gitRepoUnshallow.unshallow(null);
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(false)) {
115+
if (config.isCiVisibilityGitUnshallowDefer() && gitRepoUnshallow.unshallow(null)) {
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: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import datadog.trace.civisibility.utils.ShellCommandExecutor;
55
import java.io.IOException;
66
import java.util.concurrent.TimeoutException;
7+
import javax.annotation.Nullable;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
910

@@ -19,29 +20,37 @@ public GitRepoUnshallow(Config config, GitClient gitClient) {
1920
this.gitClient = gitClient;
2021
}
2122

22-
public synchronized boolean unshallow(boolean parentOnly)
23+
public synchronized boolean unshallow(@Nullable String commit)
2324
throws IOException, InterruptedException, TimeoutException {
2425
if (!config.isCiVisibilityGitUnshallowEnabled() || !gitClient.isShallow()) {
2526
return false;
2627
}
2728

2829
long unshallowStart = System.currentTimeMillis();
29-
try {
30-
gitClient.unshallow(GitClient.HEAD, parentOnly);
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-
}
36-
37-
try {
38-
String upstreamBranch = gitClient.getUpstreamBranchSha();
39-
gitClient.unshallow(upstreamBranch, parentOnly);
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, parentOnly);
30+
if (commit == null) {
31+
try {
32+
gitClient.unshallow(GitClient.HEAD, false);
33+
} catch (ShellCommandExecutor.ShellCommandFailedException e) {
34+
LOGGER.debug(
35+
"Could not unshallow using HEAD - assuming HEAD points to a local commit that does not exist in the remote repo",
36+
e);
37+
}
38+
39+
try {
40+
String upstreamBranch = gitClient.getUpstreamBranchSha();
41+
gitClient.unshallow(upstreamBranch, false);
42+
} catch (ShellCommandExecutor.ShellCommandFailedException e) {
43+
LOGGER.debug(
44+
"Could not unshallow using upstream branch - assuming currently checked out local branch does not track any remote branch",
45+
e);
46+
gitClient.unshallow(null, false);
47+
}
48+
} else {
49+
try {
50+
gitClient.unshallow(commit, true);
51+
} catch (ShellCommandExecutor.ShellCommandFailedException e) {
52+
LOGGER.debug("Could not unshallow specific commit {}", commit, e);
53+
}
4554
}
4655
LOGGER.debug("Repository unshallowing took {} ms", System.currentTimeMillis() - unshallowStart);
4756
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
@@ -21,7 +21,7 @@ public boolean isShallow() {
2121
}
2222

2323
@Override
24-
public void unshallow(@Nullable String remoteCommitReference, boolean parentOnly) {
24+
public void unshallow(@Nullable String remoteCommitReference, boolean onlyFetchCommit) {
2525
// no op
2626
}
2727

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,55 +129,46 @@ public String getUpstreamBranchSha() throws IOException, TimeoutException, Inter
129129
*
130130
* @param remoteCommitReference The commit to fetch from the remote repository, so local repo will
131131
* be updated with this commit and its ancestors. If {@code null}, everything will be fetched.
132-
* @param parentOnly If only the parent commit should be unshallowed or the full {@code
133-
* latestCommitsSince}
132+
* @param onlyFetchCommit Only fetch the specific commit provided.
134133
* @throws IOException If an error was encountered while writing command input or reading output
135134
* @throws TimeoutException If timeout was reached while waiting for Git command to finish
136135
* @throws InterruptedException If current thread was interrupted while waiting for Git command to
137136
* finish
138137
*/
139138
@Override
140-
public void unshallow(@Nullable String remoteCommitReference, boolean parentOnly)
139+
public void unshallow(@Nullable String remoteCommitReference, boolean onlyFetchCommit)
141140
throws IOException, TimeoutException, InterruptedException {
142141
executeCommand(
143142
Command.UNSHALLOW,
144143
() -> {
145-
String remote =
146-
commandExecutor
147-
.executeCommand(
148-
IOUtils::readFully,
149-
"git",
150-
"config",
151-
"--default",
152-
"origin",
153-
"--get",
154-
"clone.defaultRemoteName")
155-
.trim();
144+
String remote = getRemoteName();
156145

157146
// refetch data from the server for the given period of time
158-
String depth =
159-
parentOnly ? "--deepen=1" : String.format("--shallow-since='%s'", latestCommitsSince);
160147
if (remoteCommitReference != null && GitUtils.isValidRef(remoteCommitReference)) {
161-
String headSha = getSha(remoteCommitReference);
148+
String onlyFetchCommitArg =
149+
onlyFetchCommit
150+
? "--no-write-fetch-head"
151+
: String.format("--shallow-since='%s'", latestCommitsSince);
152+
String commitSha = getSha(remoteCommitReference);
162153
commandExecutor.executeCommand(
163154
ShellCommandExecutor.OutputParser.IGNORE,
164155
"git",
165156
"fetch",
166-
depth,
167157
"--update-shallow",
168158
"--filter=blob:none",
169159
"--recurse-submodules=no",
160+
onlyFetchCommitArg,
170161
remote,
171-
headSha);
162+
commitSha);
172163
} else {
173164
commandExecutor.executeCommand(
174165
ShellCommandExecutor.OutputParser.IGNORE,
175166
"git",
176167
"fetch",
177-
depth,
178168
"--update-shallow",
179169
"--filter=blob:none",
180170
"--recurse-submodules=no",
171+
String.format("--shallow-since='%s'", latestCommitsSince),
181172
remote);
182173
}
183174

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

Lines changed: 7 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 parentOnly-#parentOnly"() {
73+
def "test unshallow: sha-#remoteSha fetchOnlyCommit-#fetchOnlyCommit"() {
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, parentOnly)
87+
gitClient.unshallow(remoteSha, fetchOnlyCommit)
8888
shallow = gitClient.isShallow()
8989
commits = gitClient.getLatestCommits()
9090

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

9595
where:
96-
remoteSha | parentOnly | isShallow | numCommits
97-
GitClient.HEAD | false | false | 10
98-
null | false | false | 10
99-
GitClient.HEAD | true | true | 2
100-
null | true | true | 2
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
101101
}
102102

103103
def "test get git folder"() {

0 commit comments

Comments
 (0)