Skip to content

Commit

Permalink
CHANGED: It is now checked as precondition, if a version already exis…
Browse files Browse the repository at this point in the history
…ts in the repository (should help with perf)
  • Loading branch information
WinPlay02 committed Oct 20, 2023
1 parent 43d3c43 commit 1315224
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 104 deletions.
7 changes: 3 additions & 4 deletions src/main/groovy/com/github/winplay02/gitcraft/GitCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,16 @@ public static void main(String[] args) throws Exception {
MiscHelper.println("Decompiler log output is suppressed!");
GitCraft.versionGraph = doVersionGraphOperations(GitCraft.versionGraph);
try (RepoWrapper repo = GitCraft.getRepository()) {
STEP_COMMIT.prepare(GitCraft.versionGraph, repo);
runMainPipeline(GitCraft.DEFAULT_PIPELINE);
runMainPipeline(GitCraft.DEFAULT_PIPELINE, repo);
if (repo != null) {
MiscHelper.println("Repo can be found at: %s", repo.getRootPath().toString());
}
}
}

private static void runMainPipeline(List<Step> pipeline) {
private static void runMainPipeline(List<Step> pipeline, RepoWrapper repo) {
for (OrderedVersion mcv : versionGraph) {
Step.executePipeline(pipeline, mcv, GitCraft.config.getMappingsForMinecraftVersion(mcv).orElseThrow());
Step.executePipeline(pipeline, mcv, GitCraft.config.getMappingsForMinecraftVersion(mcv).orElseThrow(), versionGraph, repo);
}
}

Expand Down
124 changes: 44 additions & 80 deletions src/main/groovy/com/github/winplay02/gitcraft/pipeline/CommitStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,53 +33,44 @@

public class CommitStep extends Step {

private MinecraftVersionGraph versionGraph = null;
private RepoWrapper repo = null;

public void prepare(MinecraftVersionGraph versionGraph, RepoWrapper repo) {
this.versionGraph = versionGraph;
this.repo = repo;
}

@Override
public String getName() {
return STEP_COMMIT;
}

@Override
public boolean preconditionsShouldRun(PipelineCache pipelineCache, OrderedVersion mcVersion, MappingFlavour mappingFlavour) {
return !GitCraft.config.noRepo && super.preconditionsShouldRun(pipelineCache, mcVersion, mappingFlavour);
public boolean preconditionsShouldRun(PipelineCache pipelineCache, OrderedVersion mcVersion, MappingFlavour mappingFlavour, MinecraftVersionGraph versionGraph, RepoWrapper repo) {
return !GitCraft.config.noRepo && super.preconditionsShouldRun(pipelineCache, mcVersion, mappingFlavour, versionGraph, repo);
}

@Override
public StepResult run(PipelineCache pipelineCache, OrderedVersion mcVersion, MappingFlavour mappingFlavour) throws Exception {
public StepResult run(PipelineCache pipelineCache, OrderedVersion mcVersion, MappingFlavour mappingFlavour, MinecraftVersionGraph versionGraph, RepoWrapper repo) throws Exception {
// Check validity of prepared args
Objects.requireNonNull(this.versionGraph);
Objects.requireNonNull(this.repo);
Objects.requireNonNull(repo);
// Clean First
MiscHelper.executeTimedStep("Clearing working directory...", this::clearWorkingTree);
MiscHelper.executeTimedStep("Clearing working directory...", () -> this.clearWorkingTree(repo));
// Switch Branch
Optional<String> target_branch = switchBranchIfNeeded(mcVersion);
Optional<String> target_branch = switchBranchIfNeeded(mcVersion, versionGraph, repo);
if (target_branch.isEmpty()) {
return StepResult.UP_TO_DATE;
}
// Copy to repository
MiscHelper.executeTimedStep("Moving files to repo...", () -> {
// Copy decompiled MC code to repo directory
copyCode(pipelineCache, mcVersion);
copyCode(pipelineCache, mcVersion, repo);
// Copy assets & data (it makes sense to track them, atleast the data)
copyAssets(pipelineCache, mcVersion);
copyAssets(pipelineCache, mcVersion, repo);
// External Assets
copyExternalAssets(pipelineCache, mcVersion);
copyExternalAssets(pipelineCache, mcVersion, repo);
});
// Commit
MiscHelper.executeTimedStep("Committing files to repo...", () -> createCommit(mcVersion));
MiscHelper.executeTimedStep("Committing files to repo...", () -> createCommit(mcVersion, repo));
MiscHelper.println("Committed %s to the repository! (Target Branch is %s)", mcVersion.launcherFriendlyVersionName(), target_branch.orElseThrow() + (MinecraftVersionGraph.isVersionNonLinearSnapshot(mcVersion) ? " (non-linear)" : ""));
return StepResult.SUCCESS;
}

private void clearWorkingTree() throws IOException {
for (Path innerPath : MiscHelper.listDirectly(this.repo.getRootPath())) {
private void clearWorkingTree(RepoWrapper repo) throws IOException {
for (Path innerPath : MiscHelper.listDirectly(repo.getRootPath())) {
if (!innerPath.toString().endsWith(".git")) {
if (Files.isDirectory(innerPath)) {
MiscHelper.deleteDirectory(innerPath);
Expand All @@ -90,116 +81,112 @@ private void clearWorkingTree() throws IOException {
}
}

private Optional<String> switchBranchIfNeeded(OrderedVersion mcVersion) throws IOException, GitAPIException {
private Optional<String> switchBranchIfNeeded(OrderedVersion mcVersion, MinecraftVersionGraph versionGraph, RepoWrapper repo) throws IOException, GitAPIException {
String target_branch;
if (this.repo.getGit().getRepository().resolve(Constants.HEAD) != null) { // Don't run on empty repo
if (repo.getGit().getRepository().resolve(Constants.HEAD) != null) { // Don't run on empty repo
target_branch = MinecraftVersionGraph.isVersionNonLinearSnapshot(mcVersion) ? mcVersion.launcherFriendlyVersionName() : GitCraft.config.gitMainlineLinearBranch;
checkoutVersionBranch(target_branch.replace(" ", "-"), mcVersion);
if (findVersionCurrentBranch(mcVersion)) {
checkoutVersionBranch(target_branch.replace(" ", "-"), mcVersion, versionGraph, repo);
if (findVersionRev(mcVersion, repo)) {
MiscHelper.println("Version %s already exists in repo, skipping", mcVersion.launcherFriendlyVersionName());
return Optional.empty();
}
if (mcVersion.equals(this.versionGraph.getRootVersion())) {
if (mcVersion.equals(versionGraph.getRootVersion())) {
MiscHelper.panic("HEAD is not empty, but the current version is the root version, and should be the initial commit");
}
Optional<RevCommit> tip_commit = StreamSupport.stream(this.repo.getGit().log().setMaxCount(1).call().spliterator(), false).findFirst();
Optional<RevCommit> tip_commit = StreamSupport.stream(repo.getGit().log().setMaxCount(1).call().spliterator(), false).findFirst();
if (tip_commit.isEmpty()) {
MiscHelper.panic("HEAD is not empty, but a root commit can not be found");
}
Optional<OrderedVersion> prev_version = this.versionGraph.getPreviousNode(mcVersion);
Optional<OrderedVersion> prev_version = versionGraph.getPreviousNode(mcVersion);
if (prev_version.isEmpty()) {
MiscHelper.panic("HEAD is not empty, but current version does not have a preceding version");
}
if (!Objects.equals(tip_commit.get().getFullMessage(), prev_version.get().toCommitMessage())) {
MiscHelper.panic("This repository is wrongly ordered. Please remove the unordered commits or delete the entire repository");
}
} else {
if (!mcVersion.equals(this.versionGraph.getRootVersion())) {
if (!mcVersion.equals(versionGraph.getRootVersion())) {
MiscHelper.panic("A non-root version is committed as the root commit to the repository");
}
target_branch = GitCraft.config.gitMainlineLinearBranch;
}
return Optional.of(target_branch);
}

private boolean findVersionCurrentBranch(OrderedVersion mcVersion) throws GitAPIException, IOException {
return this.repo.getGit().log().all().setRevFilter(new CommitMsgFilter(mcVersion.toCommitMessage())).call().iterator().hasNext();
}

private void checkoutVersionBranch(String target_branch, OrderedVersion mcVersion) throws IOException, GitAPIException {
if (!Objects.equals(this.repo.getGit().getRepository().getBranch(), target_branch)) {
Ref target_ref = this.repo.getGit().getRepository().getRefDatabase().findRef(target_branch);
private void checkoutVersionBranch(String target_branch, OrderedVersion mcVersion, MinecraftVersionGraph versionGraph, RepoWrapper repo) throws IOException, GitAPIException {
if (!Objects.equals(repo.getGit().getRepository().getBranch(), target_branch)) {
Ref target_ref = repo.getGit().getRepository().getRefDatabase().findRef(target_branch);
if (target_ref == null) {
RevCommit branchPoint = findBaseForNonLinearVersion(mcVersion);
RevCommit branchPoint = findBaseForNonLinearVersion(mcVersion, versionGraph, repo);
if (branchPoint == null) {
MiscHelper.panic("Could not find branching point for non-linear version: %s (%s)", mcVersion.launcherFriendlyVersionName(), mcVersion.semanticVersion());
}
target_ref = this.repo.getGit().branchCreate().setStartPoint(branchPoint).setName(target_branch).call();
target_ref = repo.getGit().branchCreate().setStartPoint(branchPoint).setName(target_branch).call();
}
switchHEAD(target_ref);
switchHEAD(target_ref, repo);
}
}

private RevCommit findBaseForNonLinearVersion(OrderedVersion mcVersion) throws IOException, GitAPIException {
Optional<OrderedVersion> previousVersion = this.versionGraph.getPreviousNode(mcVersion);
private RevCommit findBaseForNonLinearVersion(OrderedVersion mcVersion, MinecraftVersionGraph versionGraph, RepoWrapper repo) throws IOException, GitAPIException {
Optional<OrderedVersion> previousVersion = versionGraph.getPreviousNode(mcVersion);
if (previousVersion.isEmpty()) {
MiscHelper.panic("Cannot commit non-linear version %s, as the base version was not found", mcVersion.launcherFriendlyVersionName());
}
return findVersionObjectCurrentBranch(previousVersion.get());
return findVersionObjectRev(previousVersion.get(), repo);
}

private RevCommit findVersionObjectCurrentBranch(OrderedVersion mcVersion) throws GitAPIException, IOException {
Iterator<RevCommit> iterator = this.repo.getGit().log().all().setRevFilter(new CommitMsgFilter(mcVersion.toCommitMessage())).call().iterator();
private RevCommit findVersionObjectRev(OrderedVersion mcVersion, RepoWrapper repo) throws GitAPIException, IOException {
Iterator<RevCommit> iterator = repo.getGit().log().all().setRevFilter(new CommitMsgFilter(mcVersion.toCommitMessage())).call().iterator();
if (iterator.hasNext()) {
return iterator.next();
}
return null;
}

private void switchHEAD(Ref ref) throws IOException {
RefUpdate refUpdate = this.repo.getGit().getRepository().getRefDatabase().newUpdate(Constants.HEAD, false);
private void switchHEAD(Ref ref, RepoWrapper repo) throws IOException {
RefUpdate refUpdate = repo.getGit().getRepository().getRefDatabase().newUpdate(Constants.HEAD, false);
RefUpdate.Result result = refUpdate.link(ref.getName());
if (result != RefUpdate.Result.FORCED) {
MiscHelper.panic("Unsuccessfully changed HEAD to %s, result was: %s", ref, result);
}
}

private void copyCode(PipelineCache pipelineCache, OrderedVersion mcVersion) throws IOException {
private void copyCode(PipelineCache pipelineCache, OrderedVersion mcVersion, RepoWrapper repo) throws IOException {
Path decompiledJarPath = pipelineCache.getForKey(Step.STEP_DECOMPILE);
if (decompiledJarPath == null) {
MiscHelper.panic("A decompiled JAR for version %s does not exist", mcVersion.launcherFriendlyVersionName());
}
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(decompiledJarPath)) {
MiscHelper.copyLargeDir(fs.get().getPath("."), this.repo.getRootPath().resolve("minecraft").resolve("src"));
MiscHelper.copyLargeDir(fs.get().getPath("."), repo.getRootPath().resolve("minecraft").resolve("src"));
}
}

private void copyAssets(PipelineCache pipelineCache, OrderedVersion mcVersion) throws IOException {
private void copyAssets(PipelineCache pipelineCache, OrderedVersion mcVersion, RepoWrapper repo) throws IOException {
if (GitCraft.config.loadAssets || GitCraft.config.loadIntegratedDatapack) {
Path mergedJarPath = pipelineCache.getForKey(Step.STEP_MERGE);
if (mergedJarPath == null) { // Client JAR could also work
MiscHelper.panic("A merged JAR for version %s does not exist", mcVersion.launcherFriendlyVersionName());
}
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(mergedJarPath)) {
if (GitCraft.config.loadAssets) {
MiscHelper.copyLargeDir(fs.get().getPath("assets"), this.repo.getRootPath().resolve("minecraft").resolve("resources").resolve("assets"));
MiscHelper.copyLargeDir(fs.get().getPath("assets"), repo.getRootPath().resolve("minecraft").resolve("resources").resolve("assets"));
}
if (GitCraft.config.loadIntegratedDatapack) {
MiscHelper.copyLargeDir(fs.get().getPath("data"), this.repo.getRootPath().resolve("minecraft").resolve("resources").resolve("data"));
MiscHelper.copyLargeDir(fs.get().getPath("data"), repo.getRootPath().resolve("minecraft").resolve("resources").resolve("data"));
}
}
}
}

private void copyExternalAssets(PipelineCache pipelineCache, OrderedVersion mcVersion) throws IOException {
private void copyExternalAssets(PipelineCache pipelineCache, OrderedVersion mcVersion, RepoWrapper repo) throws IOException {
if (GitCraft.config.loadAssets && GitCraft.config.loadAssetsExtern) {
AssetsIndex assetsIndex = pipelineCache.getAssetsIndex();
Path artifactObjectStore = pipelineCache.getForKey(STEP_FETCH_ASSETS);
if (artifactObjectStore == null) {
MiscHelper.panic("Assets for version %s do not exist", mcVersion.launcherFriendlyVersionName());
}
// Copy Assets
Path targetRoot = this.repo.getRootPath().resolve("minecraft").resolve("external-resources").resolve("assets");
Path targetRoot = repo.getRootPath().resolve("minecraft").resolve("external-resources").resolve("assets");
if (GitCraft.config.useHardlinks) {
for (Map.Entry<String, AssetsIndexMeta.AssetsIndexEntry> entry : assetsIndex.assetsIndex().objects().entrySet()) {
Path sourcePath = GitCraft.ASSETS_OBJECTS.resolve(entry.getValue().hash());
Expand All @@ -218,36 +205,13 @@ private void copyExternalAssets(PipelineCache pipelineCache, OrderedVersion mcVe
}
}

private void createCommit(OrderedVersion mcVersion) throws GitAPIException {
private void createCommit(OrderedVersion mcVersion, RepoWrapper repo) throws GitAPIException {
// Remove removed files from index
this.repo.getGit().add().addFilepattern(".").setRenormalize(false).setUpdate(true).call();
repo.getGit().add().addFilepattern(".").setRenormalize(false).setUpdate(true).call();
// Stage new files
this.repo.getGit().add().addFilepattern(".").setRenormalize(false).call();
repo.getGit().add().addFilepattern(".").setRenormalize(false).call();
Date version_date = new Date(OffsetDateTime.parse(mcVersion.timestamp()).toInstant().toEpochMilli());
PersonIdent author = new PersonIdent(GitCraft.config.gitUser, GitCraft.config.gitMail, version_date, TimeZone.getTimeZone("UTC"));
this.repo.getGit().commit().setMessage(mcVersion.toCommitMessage()).setAuthor(author).setCommitter(author).setSign(false).call();
}

private static final class CommitMsgFilter extends RevFilter {
String msg;

CommitMsgFilter(String msg) {
this.msg = msg;
}

@Override
public boolean include(RevWalk walker, RevCommit c) {
return Objects.equals(c.getFullMessage(), this.msg);
}

@Override
public RevFilter clone() {
return this;
}

@Override
public String toString() {
return "MSG FILTER";
}
repo.getGit().commit().setMessage(mcVersion.toCommitMessage()).setAuthor(author).setCommitter(author).setSign(false).call();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.github.winplay02.gitcraft.pipeline;

import com.github.winplay02.gitcraft.GitCraft;
import com.github.winplay02.gitcraft.MinecraftVersionGraph;
import com.github.winplay02.gitcraft.mappings.MappingFlavour;
import com.github.winplay02.gitcraft.types.Artifact;
import com.github.winplay02.gitcraft.types.OrderedVersion;
import com.github.winplay02.gitcraft.util.FFNIODirectoryResultSaver;
import com.github.winplay02.gitcraft.util.MiscHelper;
import com.github.winplay02.gitcraft.util.RepoWrapper;
import com.github.winplay02.gitcraft.util.SerializationHelper;
import net.fabricmc.fernflower.api.IFabricJavadocProvider;
import net.fabricmc.loom.decompilers.vineflower.TinyJavadocProvider;
Expand Down Expand Up @@ -54,7 +56,7 @@ protected Path getInternalArtifactPath(OrderedVersion mcVersion, MappingFlavour
private static final PrintStream NULL_IS = new PrintStream(OutputStream.nullOutputStream());

@Override
public StepResult run(PipelineCache pipelineCache, OrderedVersion mcVersion, MappingFlavour mappingFlavour) throws Exception {
public StepResult run(PipelineCache pipelineCache, OrderedVersion mcVersion, MappingFlavour mappingFlavour, MinecraftVersionGraph versionGraph, RepoWrapper repo) throws Exception {
Path decompiledPath = getInternalArtifactPath(mcVersion, mappingFlavour);
if (Files.exists(decompiledPath) && Files.size(decompiledPath) > 22 /* not empty jar */) {
return StepResult.UP_TO_DATE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.winplay02.gitcraft.pipeline;

import com.github.winplay02.gitcraft.GitCraft;
import com.github.winplay02.gitcraft.MinecraftVersionGraph;
import com.github.winplay02.gitcraft.mappings.MappingFlavour;
import com.github.winplay02.gitcraft.types.OrderedVersion;
import com.github.winplay02.gitcraft.util.RepoWrapper;

import java.nio.file.Path;

Expand Down Expand Up @@ -34,7 +36,7 @@ protected Path getInternalArtifactPath(OrderedVersion mcVersion, MappingFlavour
}

@Override
public StepResult run(PipelineCache pipelineCache, OrderedVersion mcVersion, MappingFlavour mappingFlavour) throws Exception {
public StepResult run(PipelineCache pipelineCache, OrderedVersion mcVersion, MappingFlavour mappingFlavour, MinecraftVersionGraph versionGraph, RepoWrapper repo) throws Exception {
Path rootPath = getInternalArtifactPath(mcVersion, mappingFlavour);
StepResult clientJar = null;
StepResult serverJar = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.github.winplay02.gitcraft.pipeline;

import com.github.winplay02.gitcraft.GitCraft;
import com.github.winplay02.gitcraft.MinecraftVersionGraph;
import com.github.winplay02.gitcraft.mappings.MappingFlavour;
import com.github.winplay02.gitcraft.meta.AssetsIndexMeta;
import com.github.winplay02.gitcraft.types.Artifact;
import com.github.winplay02.gitcraft.types.AssetsIndex;
import com.github.winplay02.gitcraft.types.OrderedVersion;
import com.github.winplay02.gitcraft.util.RepoWrapper;
import com.github.winplay02.gitcraft.util.SerializationHelper;

import java.nio.file.Path;
Expand Down Expand Up @@ -35,12 +37,12 @@ public boolean ignoresMappings() {
}

@Override
public boolean preconditionsShouldRun(PipelineCache pipelineCache, OrderedVersion mcVersion, MappingFlavour mappingFlavour) {
return GitCraft.config.loadAssetsExtern && GitCraft.config.loadAssets && super.preconditionsShouldRun(pipelineCache, mcVersion, mappingFlavour);
public boolean preconditionsShouldRun(PipelineCache pipelineCache, OrderedVersion mcVersion, MappingFlavour mappingFlavour, MinecraftVersionGraph versionGraph, RepoWrapper repo) {
return GitCraft.config.loadAssetsExtern && GitCraft.config.loadAssets && super.preconditionsShouldRun(pipelineCache, mcVersion, mappingFlavour, versionGraph, repo);
}

@Override
public StepResult run(PipelineCache pipelineCache, OrderedVersion mcVersion, MappingFlavour mappingFlavour) throws Exception {
public StepResult run(PipelineCache pipelineCache, OrderedVersion mcVersion, MappingFlavour mappingFlavour, MinecraftVersionGraph versionGraph, RepoWrapper repo) throws Exception {
Path assetsIndexPath = mcVersion.assetsIndex().resolve(this.rootPathAssetsIndex);
StepResult assetsIndexResult = mcVersion.assetsIndex().fetchArtifact(this.rootPathAssetsIndex, "assets index");
AssetsIndex assetsIndex = AssetsIndex.from(SerializationHelper.deserialize(SerializationHelper.fetchAllFromPath(assetsIndexPath), AssetsIndexMeta.class));
Expand Down
Loading

0 comments on commit 1315224

Please sign in to comment.