1717
1818package pl .project13 .maven .git ;
1919
20- import com .google .common .base .Optional ;
21- import org .apache .maven .artifact .Artifact ;
2220import org .apache .maven .project .MavenProject ;
2321import org .eclipse .jgit .lib .Constants ;
2422import 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 }
0 commit comments