Skip to content

Commit 77679ab

Browse files
committed
Merge pull request #135 from maiergre/master
Improve gitdir handling with submodules
2 parents 33ce1f2 + fe371c5 commit 77679ab

File tree

2 files changed

+35
-68
lines changed

2 files changed

+35
-68
lines changed

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

+23-60
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,10 +41,9 @@ public GitDirLocator(MavenProject mavenProject, List<MavenProject> reactorProjec
4341

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

49-
// If manuallyConfiguredDir is a directory then we can use it as the git path.
46+
// If manuallyConfiguredDir is a directory then we can use it as the git path.
5047
if (manuallyConfiguredDir.isDirectory()) {
5148
return manuallyConfiguredDir;
5249
}
@@ -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
/**
@@ -158,7 +120,14 @@ private File processGitDirFile(@NotNull File file) {
158120
}
159121

160122
// All seems ok so return the "gitdir" value read from the file.
161-
return new File(file.getParentFile(), parts[1]);
123+
File gitDir = new File(parts[1]);
124+
if (gitDir.isAbsolute()) {
125+
// gitdir value is an absolute path. Return as-is
126+
return gitDir;
127+
} else {
128+
// gitdir value is relative.
129+
return new File(file.getParentFile(), parts[1]);
130+
}
162131
} catch (FileNotFoundException e) {
163132
return null;
164133
} finally {
@@ -171,12 +140,6 @@ private File processGitDirFile(@NotNull File file) {
171140
}
172141
}
173142

174-
@NotNull
175-
private static File getProjectGitDir(@NotNull MavenProject mavenProject) {
176-
// FIXME Shouldn't this look at the dotGitDirectory property (if set) for the given project?
177-
return new File(mavenProject.getBasedir(), Constants.DOT_GIT);
178-
}
179-
180143
private static boolean isExistingDirectory(@Nullable File fileLocation) {
181144
return fileLocation != null && fileLocation.exists() && fileLocation.isDirectory();
182145
}

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)