Skip to content

Commit

Permalink
feat: 'ratchetFrom' in git submodule supported (diffplug#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
conderls committed Dec 23, 2020
1 parent 75fdd3e commit 6771692
Showing 1 changed file with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -183,13 +185,36 @@ protected Repository repositoryFor(Project project) throws IOException {
return null;
}

/**
* When populating a new submodule directory with "git submodule init", the $GIT_DIR meta-information directory
* for submodules is created inside $GIT_DIR/modules// directory of the super-project
* and referenced via the git-file mechanism.
*/
private static @Nullable File getDotGitDir(File dir, String dotGit) {
File dotGitPath = new File(dir, dotGit);

if (dotGitPath.isDirectory()) {
return dotGitPath;
} else if (dotGitPath.isFile()){
try {
String relativePath = new String(Files.readAllBytes(dir.toPath()), StandardCharsets.UTF_8)
.split(":")[1].trim();
return getDotGitDir(dir, relativePath);
} catch (IOException e) {
return null;
}
} else {
return null;
}
}

private static boolean isGitRoot(File dir) {
File dotGit = new File(dir, Constants.DOT_GIT);
return dotGit.isDirectory() && RepositoryCache.FileKey.isGitRepository(dotGit, FS.DETECTED);
File dotGit = getDotGitDir(dir, Constants.DOT_GIT);
return dotGit != null && RepositoryCache.FileKey.isGitRepository(dotGit, FS.DETECTED);
}

static Repository createRepo(File dir) throws IOException {
return FileRepositoryBuilder.create(new File(dir, Constants.DOT_GIT));
return FileRepositoryBuilder.create(getDotGitDir(dir, Constants.DOT_GIT));
}

/**
Expand Down

0 comments on commit 6771692

Please sign in to comment.