Skip to content

Commit

Permalink
Fix resolving the working tree of a linked worktree
Browse files Browse the repository at this point in the history
By default the old behaviour would always cause the working tree to be
the parent directory of the POM. The new method resolves the working
tree as the parent directory of the worktree’s `GIT_DIR`.
  • Loading branch information
koraktor committed Feb 12, 2019
1 parent 332896d commit 4c69043
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class JGitRepository extends AbstractGitRepository {

private static final int MAX_DESCRIBE_CANDIDATES = 10;
static final String COMMONDIR_FILE = "commondir";
static final String GITDIR_FILE = "gitdir";
private static final String INDEX_FILE = "index";
static final String REF_LINK_PREFIX = "ref: ";

Expand Down Expand Up @@ -118,7 +119,11 @@ final void buildRepository(File workTree, File gitDir) throws GitRepositoryExcep
realGitDir = new File(realGitDirPath);
}

if (realGitDir.exists()) {
File originalGitDirFile = new File(foundGitDir, GITDIR_FILE);
String originalGitDirPath = readFileToString(originalGitDirFile, "UTF-8").trim();
File originalGitDir = new File(originalGitDirPath);

if (originalGitDir.exists() && realGitDir.exists()) {
if (headRef.equals(HEAD)) {
File headFile = new File(foundGitDir, HEAD);
String rawHead = readFileToString(headFile, Charset.forName("UTF-8"));
Expand All @@ -127,7 +132,7 @@ final void buildRepository(File workTree, File gitDir) throws GitRepositoryExcep

repositoryBuilder.setGitDir(realGitDir);
repositoryBuilder.setIndexFile(new File(foundGitDir, INDEX_FILE));
repositoryBuilder.setWorkTree(workTree);
repositoryBuilder.setWorkTree(originalGitDir.getParentFile());
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -171,8 +172,12 @@ void testCreateWithLinkedWorktree() throws Exception {
workTree.deleteOnExit();
}

File originalGitDir = new File(workTree, DOT_GIT);
Files.createFile(originalGitDir.toPath());

FileUtils.writeStringToFile(new File(gitDir, HEAD), REF_LINK_PREFIX + R_HEADS + "test", Charset.forName("UTF-8"));
FileUtils.writeStringToFile(new File(gitDir, COMMONDIR_FILE), realGitDir.getAbsolutePath(), Charset.forName("UTF-8"));
FileUtils.writeStringToFile(new File(gitDir, GITDIR_FILE), originalGitDir.getAbsolutePath(), Charset.forName("UTF-8"));
FileRepositoryBuilder repoBuilder = mock(FileRepositoryBuilder.class, RETURNS_DEEP_STUBS);
when(repoBuilder.findGitDir(any())).thenReturn(repoBuilder);
when(repoBuilder.getGitDir()).thenReturn(gitDir);
Expand Down

0 comments on commit 4c69043

Please sign in to comment.