Skip to content

Commit fe371c5

Browse files
committed
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)
1 parent 876490b commit fe371c5

File tree

2 files changed

+26
-66
lines changed

2 files changed

+26
-66
lines changed

src/main/java/pl/project13/maven/git/GitDirLocator.java

+14-58
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
package pl.project13.maven.git;
1919

20-
import com.google.common.base.Optional;
21-
import org.apache.maven.artifact.Artifact;
2220
import org.apache.maven.project.MavenProject;
2321
import org.eclipse.jgit.lib.Constants;
2422
import org.jetbrains.annotations.NotNull;
@@ -43,7 +41,6 @@ public GitDirLocator(MavenProject mavenProject, List<MavenProject> reactorProjec
4341

4442
@Nullable
4543
public File lookupGitDirectory(@NotNull File manuallyConfiguredDir) {
46-
4744
if (manuallyConfiguredDir.exists()) {
4845

4946
// If manuallyConfiguredDir is a directory then we can use it as the git path.
@@ -77,60 +74,25 @@ public File lookupGitDirectory(@NotNull File manuallyConfiguredDir) {
7774
*/
7875
@Nullable
7976
private File findProjectGitDirectory() {
80-
MavenProject currentProject = this.mavenProject;
81-
82-
while (currentProject != null) {
83-
File dir = getProjectGitDir(currentProject);
84-
85-
if (isExistingDirectory(dir)) {
86-
return dir;
87-
}
88-
// If the path exists but is not a directory it might be a git submodule "gitdir" link.
89-
File gitDirLinkPath = processGitDirFile(dir);
90-
91-
// If the linkPath was found from the file and it exists then use it.
92-
if (isExistingDirectory(gitDirLinkPath)) {
93-
return gitDirLinkPath;
94-
}
95-
96-
/**
97-
* project.getParent always returns NULL for me, but if getParentArtifact returns
98-
* not null then there is actually a parent - seems like a bug in maven to me.
99-
*/
100-
if (currentProject.getParent() == null && currentProject.getParentArtifact() != null) {
101-
Optional<MavenProject> maybeFoundParentProject = getReactorParentProject(currentProject);
102-
103-
if (maybeFoundParentProject.isPresent())
104-
currentProject = maybeFoundParentProject.get();
105-
106-
} else {
107-
// Get the parent, or NULL if no parent AND no parentArtifact.
108-
currentProject = currentProject.getParent();
109-
}
77+
if (this.mavenProject == null) {
78+
return null;
11079
}
11180

112-
return null;
113-
}
114-
115-
/**
116-
* Find a project in the reactor by its artifact, I'm new to maven coding
117-
* so there may be a better way to do this, it would not be necessary
118-
* if project.getParent() actually worked.
119-
*
120-
* @return MavenProject parent project or NULL if no parent available
121-
*/
122-
private Optional<MavenProject> getReactorParentProject(@NotNull MavenProject project) {
123-
Artifact parentArtifact = project.getParentArtifact();
124-
125-
if (parentArtifact != null) {
126-
for (MavenProject reactorProject : this.reactorProjects) {
127-
if (reactorProject.getArtifactId().equals(parentArtifact.getArtifactId())) {
128-
return Optional.of(reactorProject);
81+
File basedir = mavenProject.getBasedir();
82+
while (basedir != null) {
83+
File gitdir = new File(basedir, Constants.DOT_GIT);
84+
if (gitdir != null && gitdir.exists()) {
85+
if (gitdir.isDirectory()) {
86+
return gitdir;
87+
} else if (gitdir.isFile()) {
88+
return processGitDirFile(gitdir);
89+
} else {
90+
return null;
12991
}
13092
}
93+
basedir = basedir.getParentFile();
13194
}
132-
133-
return Optional.absent();
95+
return null;
13496
}
13597

13698
/**
@@ -178,12 +140,6 @@ private File processGitDirFile(@NotNull File file) {
178140
}
179141
}
180142

181-
@NotNull
182-
private static File getProjectGitDir(@NotNull MavenProject mavenProject) {
183-
// FIXME Shouldn't this look at the dotGitDirectory property (if set) for the given project?
184-
return new File(mavenProject.getBasedir(), Constants.DOT_GIT);
185-
}
186-
187143
private static boolean isExistingDirectory(@Nullable File fileLocation) {
188144
return fileLocation != null && fileLocation.exists() && fileLocation.isDirectory();
189145
}

src/test/java/pl/project13/maven/git/GitDirLocatorTest.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@ public class GitDirLocatorTest {
4242
public void shouldUseTheManuallySpecifiedDirectory() throws Exception {
4343
// given
4444
File dotGitDir = Files.createTempDir();
45-
46-
// when
47-
GitDirLocator locator = new GitDirLocator(project, reactorProjects);
48-
File foundDirectory = locator.lookupGitDirectory(dotGitDir);
49-
50-
// then
51-
assert foundDirectory != null;
52-
assertThat(foundDirectory.getAbsolutePath()).isEqualTo(dotGitDir.getAbsolutePath());
45+
try {
46+
47+
// when
48+
GitDirLocator locator = new GitDirLocator(project, reactorProjects);
49+
File foundDirectory = locator.lookupGitDirectory(dotGitDir);
50+
51+
// then
52+
assert foundDirectory != null;
53+
assertThat(foundDirectory.getAbsolutePath()).isEqualTo(dotGitDir.getAbsolutePath());
54+
} finally {
55+
dotGitDir.delete();
56+
}
5357
}
5458

5559
}

0 commit comments

Comments
 (0)