17
17
18
18
package pl .project13 .maven .git ;
19
19
20
- import com .google .common .base .Optional ;
21
- import org .apache .maven .artifact .Artifact ;
22
20
import org .apache .maven .project .MavenProject ;
23
21
import org .eclipse .jgit .lib .Constants ;
24
22
import org .jetbrains .annotations .NotNull ;
@@ -43,10 +41,9 @@ public GitDirLocator(MavenProject mavenProject, List<MavenProject> reactorProjec
43
41
44
42
@ Nullable
45
43
public File lookupGitDirectory (@ NotNull File manuallyConfiguredDir ) {
46
-
47
44
if (manuallyConfiguredDir .exists ()) {
48
45
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.
50
47
if (manuallyConfiguredDir .isDirectory ()) {
51
48
return manuallyConfiguredDir ;
52
49
}
@@ -77,60 +74,25 @@ public File lookupGitDirectory(@NotNull File manuallyConfiguredDir) {
77
74
*/
78
75
@ Nullable
79
76
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 ;
110
79
}
111
80
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 ;
129
91
}
130
92
}
93
+ basedir = basedir .getParentFile ();
131
94
}
132
-
133
- return Optional .absent ();
95
+ return null ;
134
96
}
135
97
136
98
/**
@@ -158,7 +120,14 @@ private File processGitDirFile(@NotNull File file) {
158
120
}
159
121
160
122
// 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
+ }
162
131
} catch (FileNotFoundException e ) {
163
132
return null ;
164
133
} finally {
@@ -171,12 +140,6 @@ private File processGitDirFile(@NotNull File file) {
171
140
}
172
141
}
173
142
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
-
180
143
private static boolean isExistingDirectory (@ Nullable File fileLocation ) {
181
144
return fileLocation != null && fileLocation .exists () && fileLocation .isDirectory ();
182
145
}
0 commit comments