-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Use merge base to find original version of transport resources #135564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,17 +76,16 @@ public interface Parameters extends BuildServiceParameters { | |
|
||
private final Path transportResourcesDir; | ||
private final Path rootDir; | ||
private final AtomicReference<String> upstreamRefName = new AtomicReference<>(); | ||
private final String upstreamRefOverride; | ||
private final AtomicReference<String> baseRefName = new AtomicReference<>(); | ||
private final AtomicReference<Set<String>> upstreamResources = new AtomicReference<>(null); | ||
private final AtomicReference<Set<String>> changedResources = new AtomicReference<>(null); | ||
|
||
@Inject | ||
public TransportVersionResourcesService(Parameters params) { | ||
this.transportResourcesDir = params.getTransportResourcesDirectory().get().getAsFile().toPath(); | ||
this.rootDir = params.getRootDirectory().get().getAsFile().toPath(); | ||
if (params.getUpstreamRefOverride().isPresent()) { | ||
upstreamRefName.set(params.getUpstreamRefOverride().get()); | ||
} | ||
upstreamRefOverride = params.getUpstreamRefOverride().getOrNull(); | ||
} | ||
|
||
/** | ||
|
@@ -240,52 +239,66 @@ private Path getUpperBoundRelativePath(String name) { | |
return UPPER_BOUNDS_DIR.resolve(name + ".csv"); | ||
} | ||
|
||
private String getUpstreamRefName() { | ||
if (upstreamRefName.get() == null) { | ||
synchronized (upstreamRefName) { | ||
String remotesOutput = gitCommand("remote").strip(); | ||
|
||
private String getBaseRefName() { | ||
if (baseRefName.get() == null) { | ||
synchronized (baseRefName) { | ||
String refName; | ||
if (remotesOutput.isEmpty()) { | ||
refName = "main"; // fallback to local main if no remotes, this happens in tests | ||
// the existence of the MERGE_HEAD ref means we are in the middle of a merge, and should use that as our base | ||
String gitDir = gitCommand("rev-parse", "--git-dir").strip(); | ||
if (Files.exists(Path.of(gitDir).resolve("MERGE_HEAD"))) { | ||
refName = gitCommand("rev-parse", "--verify", "MERGE_HEAD").strip(); | ||
} else { | ||
List<String> remoteNames = List.of(remotesOutput.split("\n")); | ||
String transportVersionRemoteName = "transport-version-resources-upstream"; | ||
if (remoteNames.contains(transportVersionRemoteName) == false) { | ||
// our special remote doesn't exist yet, so create it | ||
String upstreamUrl = null; | ||
for (String remoteName : remoteNames) { | ||
String getUrlOutput = gitCommand("remote", "get-url", remoteName).strip(); | ||
if (getUrlOutput.startsWith("[email protected]:elastic/") | ||
|| getUrlOutput.startsWith("https://github.com/elastic/")) { | ||
upstreamUrl = getUrlOutput; | ||
} | ||
} | ||
|
||
if (upstreamUrl != null) { | ||
gitCommand("remote", "add", transportVersionRemoteName, upstreamUrl); | ||
} else { | ||
throw new RuntimeException("No elastic github remotes found to copy"); | ||
} | ||
} | ||
|
||
// make sure the remote main ref is up to date | ||
gitCommand("fetch", transportVersionRemoteName, "main"); | ||
|
||
refName = transportVersionRemoteName + "/main"; | ||
String upstreamRef = findUpstreamRef(); | ||
refName = gitCommand("merge-base", upstreamRef, "HEAD").strip(); | ||
} | ||
|
||
baseRefName.set(refName); | ||
} | ||
} | ||
return baseRefName.get(); | ||
} | ||
|
||
private String findUpstreamRef() { | ||
if (upstreamRefOverride != null) { | ||
return upstreamRefOverride; | ||
} | ||
|
||
String remotesOutput = gitCommand("remote").strip(); | ||
if (remotesOutput.isEmpty()) { | ||
throw new RuntimeException( | ||
"No remotes found. If this is a test set gradle property " + "org.elasticsearch.transport.upstreamRef" | ||
); | ||
} | ||
List<String> remoteNames = List.of(remotesOutput.split("\n")); | ||
String transportVersionRemoteName = "transport-version-resources-upstream"; | ||
if (remoteNames.contains(transportVersionRemoteName) == false) { | ||
// our special remote doesn't exist yet, so create it | ||
String upstreamUrl = null; | ||
for (String remoteName : remoteNames) { | ||
String getUrlOutput = gitCommand("remote", "get-url", remoteName).strip(); | ||
if (getUrlOutput.startsWith("[email protected]:elastic/") || getUrlOutput.startsWith("https://github.com/elastic/")) { | ||
upstreamUrl = getUrlOutput; | ||
} | ||
upstreamRefName.set(refName); | ||
} | ||
|
||
if (upstreamUrl != null) { | ||
gitCommand("remote", "add", transportVersionRemoteName, upstreamUrl); | ||
} else { | ||
throw new RuntimeException("No elastic github remotes found to copy"); | ||
} | ||
} | ||
return upstreamRefName.get(); | ||
|
||
// make sure the remote main ref is up to date | ||
gitCommand("fetch", transportVersionRemoteName, "main"); | ||
|
||
return transportVersionRemoteName + "/main"; | ||
} | ||
|
||
// Return the transport version resources paths that exist in upstream | ||
private Set<String> getUpstreamResources() { | ||
if (upstreamResources.get() == null) { | ||
synchronized (upstreamResources) { | ||
String output = gitCommand("ls-tree", "--name-only", "-r", getUpstreamRefName(), "."); | ||
String output = gitCommand("ls-tree", "--name-only", "-r", getBaseRefName(), "."); | ||
|
||
HashSet<String> resources = new HashSet<>(); | ||
Collections.addAll(resources, output.split("\n")); // git always outputs LF | ||
|
@@ -301,7 +314,7 @@ private Set<String> getChangedResources() { | |
synchronized (changedResources) { | ||
HashSet<String> resources = new HashSet<>(); | ||
|
||
String diffOutput = gitCommand("diff", "--name-only", "--relative", getUpstreamRefName(), "."); | ||
String diffOutput = gitCommand("diff", "--name-only", "--relative", getBaseRefName(), "."); | ||
if (diffOutput.strip().isEmpty() == false) { | ||
Collections.addAll(resources, diffOutput.split("\n")); // git always outputs LF | ||
} | ||
|
@@ -324,7 +337,7 @@ private <T> T getUpstreamFile(Path resourcePath, BiFunction<Path, String, T> par | |
return null; | ||
} | ||
|
||
String content = gitCommand("show", getUpstreamRefName() + ":./" + pathString).strip(); | ||
String content = gitCommand("show", getBaseRefName() + ":./" + pathString).strip(); | ||
return parser.apply(resourcePath, content); | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this just be a constant?