Skip to content

Commit 0f0698d

Browse files
refactor: rename and refactor to clarify the unshallow execution paths
1 parent 72f9b91 commit 0f0698d

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

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 onlyFetchCommit)
20+
void unshallow(@Nullable String remoteCommitReference, boolean shallowUntilCommit)
2121
throws IOException, TimeoutException, InterruptedException;
2222

2323
@Nullable

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,27 @@ public GitRepoUnshallow(Config config, GitClient gitClient) {
2020
this.gitClient = gitClient;
2121
}
2222

23-
public synchronized boolean unshallow(@Nullable String commit)
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.
26+
*
27+
* @param boundaryCommitSha used as boundary for the unshallowing if provided.
28+
* @return false if unshallowing is not enabled or unnecessary, true otherwise
29+
*/
30+
public synchronized boolean unshallow(@Nullable String boundaryCommitSha)
2431
throws IOException, InterruptedException, TimeoutException {
2532
if (!config.isCiVisibilityGitUnshallowEnabled() || !gitClient.isShallow()) {
2633
return false;
2734
}
2835

2936
long unshallowStart = System.currentTimeMillis();
30-
if (commit == null) {
37+
if (boundaryCommitSha != null) {
38+
try {
39+
gitClient.unshallow(boundaryCommitSha, true);
40+
} catch (ShellCommandExecutor.ShellCommandFailedException e) {
41+
LOGGER.debug("Could not unshallow to specific boundary {}", boundaryCommitSha, e);
42+
}
43+
} else {
3144
try {
3245
gitClient.unshallow(GitClient.HEAD, false);
3346
} catch (ShellCommandExecutor.ShellCommandFailedException e) {
@@ -45,12 +58,6 @@ public synchronized boolean unshallow(@Nullable String commit)
4558
e);
4659
gitClient.unshallow(null, false);
4760
}
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-
}
5461
}
5562
LOGGER.debug("Repository unshallowing took {} ms", System.currentTimeMillis() - unshallowStart);
5663
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 onlyFetchCommit) {
24+
public void unshallow(@Nullable String remoteCommitReference, boolean shallowUntilCommit) {
2525
// no op
2626
}
2727

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,28 @@ 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 onlyFetchCommit Only fetch the specific commit provided.
132+
* @param shallowUntilCommit Only unshallow up until the commit provided, or use the time limit
133+
* configured if not. Ignored if no reference is provided.
133134
* @throws IOException If an error was encountered while writing command input or reading output
134135
* @throws TimeoutException If timeout was reached while waiting for Git command to finish
135136
* @throws InterruptedException If current thread was interrupted while waiting for Git command to
136137
* finish
137138
*/
138139
@Override
139-
public void unshallow(@Nullable String remoteCommitReference, boolean onlyFetchCommit)
140+
public void unshallow(@Nullable String remoteCommitReference, boolean shallowUntilCommit)
140141
throws IOException, TimeoutException, InterruptedException {
141142
executeCommand(
142143
Command.UNSHALLOW,
143144
() -> {
144145
String remote = getRemoteName();
145146

146147
// refetch data from the server for the given period of time
147-
if (remoteCommitReference != null && GitUtils.isValidRef(remoteCommitReference)) {
148-
String onlyFetchCommitArg =
149-
onlyFetchCommit
148+
if (remoteCommitReference != null) {
149+
if (!GitUtils.isValidRef(remoteCommitReference)) {
150+
return (Void) null;
151+
}
152+
String boundaryArg =
153+
shallowUntilCommit
150154
? "--no-write-fetch-head"
151155
: String.format("--shallow-since='%s'", latestCommitsSince);
152156
String commitSha = getSha(remoteCommitReference);
@@ -157,7 +161,7 @@ public void unshallow(@Nullable String remoteCommitReference, boolean onlyFetchC
157161
"--update-shallow",
158162
"--filter=blob:none",
159163
"--recurse-submodules=no",
160-
onlyFetchCommitArg,
164+
boundaryArg,
161165
remote,
162166
commitSha);
163167
} else {

0 commit comments

Comments
 (0)