From 876490b689803633df21913017418bb8f61775fe Mon Sep 17 00:00:00 2001 From: Gregor Maier Date: Fri, 24 Oct 2014 13:23:23 -0700 Subject: [PATCH 1/2] Support absoulte path in git submodule gitdirs When using git submodules, some versions of git (e.g., 1.7.9) use abosulate path in the gitdir reference of the submodules. This patch adds support to handle absoluate path correctly. --- .../java/pl/project13/maven/git/GitDirLocator.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/pl/project13/maven/git/GitDirLocator.java b/src/main/java/pl/project13/maven/git/GitDirLocator.java index dad40d73..6f71c0e2 100644 --- a/src/main/java/pl/project13/maven/git/GitDirLocator.java +++ b/src/main/java/pl/project13/maven/git/GitDirLocator.java @@ -46,7 +46,7 @@ public File lookupGitDirectory(@NotNull File manuallyConfiguredDir) { if (manuallyConfiguredDir.exists()) { - // If manuallyConfiguredDir is a directory then we can use it as the git path. + // If manuallyConfiguredDir is a directory then we can use it as the git path. if (manuallyConfiguredDir.isDirectory()) { return manuallyConfiguredDir; } @@ -158,7 +158,14 @@ private File processGitDirFile(@NotNull File file) { } // All seems ok so return the "gitdir" value read from the file. - return new File(file.getParentFile(), parts[1]); + File gitDir = new File(parts[1]); + if (gitDir.isAbsolute()) { + // gitdir value is an absolute path. Return as-is + return gitDir; + } else { + // gitdir value is relative. + return new File(file.getParentFile(), parts[1]); + } } catch (FileNotFoundException e) { return null; } finally { From fe371c5375872ea75c55ba923e4f43541fade903 Mon Sep 17 00:00:00 2001 From: Gregor Maier Date: Wed, 29 Oct 2014 10:32:47 -0700 Subject: [PATCH 2/2] Use parent directory not parent project to locate gitdir When searching for the git directory of a project the search follows the project's basedir parent directory rather than following the project hierarchy (which is not correct) --- .../pl/project13/maven/git/GitDirLocator.java | 72 ++++--------------- .../maven/git/GitDirLocatorTest.java | 20 +++--- 2 files changed, 26 insertions(+), 66 deletions(-) diff --git a/src/main/java/pl/project13/maven/git/GitDirLocator.java b/src/main/java/pl/project13/maven/git/GitDirLocator.java index 6f71c0e2..03eb6f6b 100644 --- a/src/main/java/pl/project13/maven/git/GitDirLocator.java +++ b/src/main/java/pl/project13/maven/git/GitDirLocator.java @@ -17,8 +17,6 @@ package pl.project13.maven.git; -import com.google.common.base.Optional; -import org.apache.maven.artifact.Artifact; import org.apache.maven.project.MavenProject; import org.eclipse.jgit.lib.Constants; import org.jetbrains.annotations.NotNull; @@ -43,7 +41,6 @@ public GitDirLocator(MavenProject mavenProject, List reactorProjec @Nullable public File lookupGitDirectory(@NotNull File manuallyConfiguredDir) { - if (manuallyConfiguredDir.exists()) { // If manuallyConfiguredDir is a directory then we can use it as the git path. @@ -77,60 +74,25 @@ public File lookupGitDirectory(@NotNull File manuallyConfiguredDir) { */ @Nullable private File findProjectGitDirectory() { - MavenProject currentProject = this.mavenProject; - - while (currentProject != null) { - File dir = getProjectGitDir(currentProject); - - if (isExistingDirectory(dir)) { - return dir; - } - // If the path exists but is not a directory it might be a git submodule "gitdir" link. - File gitDirLinkPath = processGitDirFile(dir); - - // If the linkPath was found from the file and it exists then use it. - if (isExistingDirectory(gitDirLinkPath)) { - return gitDirLinkPath; - } - - /** - * project.getParent always returns NULL for me, but if getParentArtifact returns - * not null then there is actually a parent - seems like a bug in maven to me. - */ - if (currentProject.getParent() == null && currentProject.getParentArtifact() != null) { - Optional maybeFoundParentProject = getReactorParentProject(currentProject); - - if (maybeFoundParentProject.isPresent()) - currentProject = maybeFoundParentProject.get(); - - } else { - // Get the parent, or NULL if no parent AND no parentArtifact. - currentProject = currentProject.getParent(); - } + if (this.mavenProject == null) { + return null; } - return null; - } - - /** - * Find a project in the reactor by its artifact, I'm new to maven coding - * so there may be a better way to do this, it would not be necessary - * if project.getParent() actually worked. - * - * @return MavenProject parent project or NULL if no parent available - */ - private Optional getReactorParentProject(@NotNull MavenProject project) { - Artifact parentArtifact = project.getParentArtifact(); - - if (parentArtifact != null) { - for (MavenProject reactorProject : this.reactorProjects) { - if (reactorProject.getArtifactId().equals(parentArtifact.getArtifactId())) { - return Optional.of(reactorProject); + File basedir = mavenProject.getBasedir(); + while (basedir != null) { + File gitdir = new File(basedir, Constants.DOT_GIT); + if (gitdir != null && gitdir.exists()) { + if (gitdir.isDirectory()) { + return gitdir; + } else if (gitdir.isFile()) { + return processGitDirFile(gitdir); + } else { + return null; } } + basedir = basedir.getParentFile(); } - - return Optional.absent(); + return null; } /** @@ -178,12 +140,6 @@ private File processGitDirFile(@NotNull File file) { } } - @NotNull - private static File getProjectGitDir(@NotNull MavenProject mavenProject) { - // FIXME Shouldn't this look at the dotGitDirectory property (if set) for the given project? - return new File(mavenProject.getBasedir(), Constants.DOT_GIT); - } - private static boolean isExistingDirectory(@Nullable File fileLocation) { return fileLocation != null && fileLocation.exists() && fileLocation.isDirectory(); } diff --git a/src/test/java/pl/project13/maven/git/GitDirLocatorTest.java b/src/test/java/pl/project13/maven/git/GitDirLocatorTest.java index c71ef3be..6799dd61 100644 --- a/src/test/java/pl/project13/maven/git/GitDirLocatorTest.java +++ b/src/test/java/pl/project13/maven/git/GitDirLocatorTest.java @@ -42,14 +42,18 @@ public class GitDirLocatorTest { public void shouldUseTheManuallySpecifiedDirectory() throws Exception { // given File dotGitDir = Files.createTempDir(); - - // when - GitDirLocator locator = new GitDirLocator(project, reactorProjects); - File foundDirectory = locator.lookupGitDirectory(dotGitDir); - - // then - assert foundDirectory != null; - assertThat(foundDirectory.getAbsolutePath()).isEqualTo(dotGitDir.getAbsolutePath()); + try { + + // when + GitDirLocator locator = new GitDirLocator(project, reactorProjects); + File foundDirectory = locator.lookupGitDirectory(dotGitDir); + + // then + assert foundDirectory != null; + assertThat(foundDirectory.getAbsolutePath()).isEqualTo(dotGitDir.getAbsolutePath()); + } finally { + dotGitDir.delete(); + } } }