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,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 }
0 commit comments