Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public GitInfo build(@Nullable String repositoryPath) {
return new LocalFSGitInfoExtractor()
.headCommit(Paths.get(repositoryPath, gitFolderName).toFile().getAbsolutePath());
}

@Override
public int order() {
return 2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public GitInfo build(@Nullable String repositoryPath) {
CIProviderInfo ciProviderInfo = ciProviderInfoFactory.createCIProviderInfo(currentPath);
return ciProviderInfo.buildCIGitInfo();
}

@Override
public int order() {
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ public GitInfo build(@Nullable String repositoryPath) {
committerTime),
gitProperties.getProperty("git.commit.message.full")));
}

@Override
public int order() {
return Integer.MAX_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

public interface GitInfoBuilder {
GitInfo build(@Nullable String repositoryPath);

int order();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import datadog.trace.api.cache.DDCaches;
import datadog.trace.util.Strings;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
Expand All @@ -21,7 +23,7 @@ public class GitInfoProvider {
INSTANCE.registerGitInfoBuilder(new EmbeddedGitInfoBuilder());
}

private final Collection<GitInfoBuilder> builders = new CopyOnWriteArrayList<>();
private volatile Collection<GitInfoBuilder> builders = Collections.emptyList();

// in regular cases git info has to be built only once,
// but there is a rare exception:
Expand Down Expand Up @@ -100,8 +102,11 @@ private static String firstNonBlankWithMatchingCommit(
return null;
}

public void registerGitInfoBuilder(GitInfoBuilder builder) {
builders.add(builder);
public synchronized void registerGitInfoBuilder(GitInfoBuilder builder) {
List<GitInfoBuilder> updatedBuilders = new ArrayList<>(builders);
updatedBuilders.add(builder);
updatedBuilders.sort(Comparator.comparingInt(GitInfoBuilder::order));
builders = updatedBuilders;
gitInfoCache.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,9 @@ public GitInfo build(@Nullable String repositoryPath) {

return gitInfo;
}

@Override
public int order() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ class GitInfoProviderTest extends Specification {
)

def gitInfoBuilderB = givenABuilderReturning(
new GitInfo(null, "branch", "tag", new CommitInfo("sha"))
new GitInfo(null, "branch", "tag", new CommitInfo("sha")), 2
)

def gitInfoProvider = new GitInfoProvider()
gitInfoProvider.registerGitInfoBuilder(gitInfoBuilderA)
// registering provider with higher order first, to check that the registration logic will do proper reordering after the second registration
gitInfoProvider.registerGitInfoBuilder(gitInfoBuilderB)
gitInfoProvider.registerGitInfoBuilder(gitInfoBuilderA)

when:
def actualGitInfo = gitInfoProvider.getGitInfo(REPO_PATH)
Expand Down Expand Up @@ -238,8 +239,13 @@ class GitInfoProviderTest extends Specification {
}

private GitInfoBuilder givenABuilderReturning(GitInfo gitInfo) {
givenABuilderReturning(gitInfo, 1)
}

private GitInfoBuilder givenABuilderReturning(GitInfo gitInfo, int order) {
def gitInfoBuilder = Stub(GitInfoBuilder)
gitInfoBuilder.build(REPO_PATH) >> gitInfo
return gitInfoBuilder
gitInfoBuilder.order() >> order
gitInfoBuilder
}
}