17
17
18
18
package pl .project13 .maven .git ;
19
19
20
+ import com .google .common .base .Optional ;
20
21
import org .apache .maven .artifact .Artifact ;
21
22
import org .apache .maven .project .MavenProject ;
22
23
import org .eclipse .jgit .lib .Constants ;
23
24
import org .jetbrains .annotations .NotNull ;
24
25
import org .jetbrains .annotations .Nullable ;
25
26
26
- import java .io .BufferedReader ;
27
- import java .io .File ;
28
- import java .io .FileNotFoundException ;
29
- import java .io .FileReader ;
30
- import java .io .IOException ;
27
+ import java .io .*;
31
28
import java .util .List ;
32
29
33
30
/**
34
- * Encapsulates logic to locate a valid .git directory
31
+ * Encapsulates logic to locate a valid .git directory.
32
+ *
35
33
* @author <a href="mailto:konrad.malawski@java.pl">Konrad 'ktoso' Malawski</a>
36
34
*/
37
35
public class GitDirLocator {
38
- final MavenProject mavenProject ;
36
+ final MavenProject mavenProject ;
39
37
final List <MavenProject > reactorProjects ;
40
-
41
- public GitDirLocator (MavenProject mavenProject , List <MavenProject > reactorProjects ){
42
- this .mavenProject = mavenProject ;
43
- this .reactorProjects = reactorProjects ;
38
+
39
+ public GitDirLocator (MavenProject mavenProject , List <MavenProject > reactorProjects ) {
40
+ this .mavenProject = mavenProject ;
41
+ this .reactorProjects = reactorProjects ;
44
42
}
45
-
43
+
46
44
@ Nullable
47
- public File lookupGitDirectory (File manuallyConfiguredDir ) {
45
+ public File lookupGitDirectory (@ NotNull File manuallyConfiguredDir ) {
46
+
47
+ if (manuallyConfiguredDir .exists ()) {
48
48
49
- if (manuallyConfiguredDir != null && manuallyConfiguredDir .exists ()) {
50
-
51
49
// If manuallyConfiguredDir is a directory then we can use it as the git path.
52
50
if (manuallyConfiguredDir .isDirectory ()) {
53
- return manuallyConfiguredDir ;
51
+ return manuallyConfiguredDir ;
54
52
}
55
-
53
+
56
54
// If the path exists but is not a directory it might be a git submodule "gitdir" link.
57
55
File gitDirLinkPath = processGitDirFile (manuallyConfiguredDir );
58
-
56
+
59
57
// If the linkPath was found from the file and it exists then use it.
60
- if (isExistingDirectory (gitDirLinkPath ))
61
- {
62
- return gitDirLinkPath ;
58
+ if (isExistingDirectory (gitDirLinkPath )) {
59
+ return gitDirLinkPath ;
63
60
}
64
-
61
+
65
62
/**
66
63
* FIXME: I think we should fail here because a manual path was set and it was not found
67
64
* but I'm leaving it falling back to searching for the git path because that is the current
68
65
* behaviour - Unluckypixie.
69
66
*/
70
67
}
71
68
72
- return findProjectGitDirectory ();
69
+ return findProjectGitDirectory ();
73
70
}
74
71
75
72
/**
76
73
* Search up all the maven parent project heirarchy until a .git
77
74
* directory is found.
78
- *
79
- * @return File the location of the .git directory or NULL if none found.
75
+ *
76
+ * @return File which represents the location of the .git directory or NULL if none found.
80
77
*/
81
- private File findProjectGitDirectory ()
82
- {
83
- MavenProject project = this .mavenProject ;
84
-
85
- while (project != null ) {
86
- File dir = getProjectGitDir (project );
87
-
78
+ @ Nullable
79
+ private File findProjectGitDirectory () {
80
+ MavenProject currentProject = this .mavenProject ;
81
+
82
+ while (currentProject != null ) {
83
+ File dir = getProjectGitDir (currentProject );
84
+
88
85
if (isExistingDirectory (dir )) {
89
86
return dir ;
90
- }
91
-
92
- /**
93
- * project.getParent always returns NULL for me, but if getParentArtifact returns
94
- * not null then there is actually a parent - seems like a bug in maven to me.
95
- */
96
- if (project .getParent () == null && project .getParentArtifact () != null ) {
97
- project = getReactorParentProject (project );
98
87
} else {
99
- // Get the parent, or NULL if no parent AND no parentArtifact.
100
- project = project .getParent ();
88
+ /**
89
+ * project.getParent always returns NULL for me, but if getParentArtifact returns
90
+ * not null then there is actually a parent - seems like a bug in maven to me.
91
+ */
92
+ if (currentProject .getParent () == null && currentProject .getParentArtifact () != null ) {
93
+ Optional <MavenProject > maybeFoundParentProject = getReactorParentProject (currentProject );
94
+
95
+ if (maybeFoundParentProject .isPresent ())
96
+ currentProject = maybeFoundParentProject .get ();
97
+
98
+ } else {
99
+ // Get the parent, or NULL if no parent AND no parentArtifact.
100
+ currentProject = currentProject .getParent ();
101
+ }
101
102
}
102
103
}
103
104
104
105
return null ;
105
106
}
106
-
107
+
107
108
/**
108
109
* Find a project in the reactor by its artifact, I'm new to maven coding
109
110
* so there may be a better way to do this, it would not be necessary
110
111
* if project.getParent() actually worked.
111
- *
112
- * @param MavenProject project
112
+ *
113
113
* @return MavenProject parent project or NULL if no parent available
114
114
*/
115
- private MavenProject getReactorParentProject (MavenProject project ) {
115
+ private Optional < MavenProject > getReactorParentProject (@ NotNull MavenProject project ) {
116
116
Artifact parentArtifact = project .getParentArtifact ();
117
-
117
+
118
118
if (parentArtifact != null ) {
119
- for (MavenProject reactorProject : this .reactorProjects ) {
120
- if (reactorProject .getArtifactId ().equals (parentArtifact .getArtifactId ())){
121
- return reactorProject ;
122
- }
123
- }
119
+ for (MavenProject reactorProject : this .reactorProjects ) {
120
+ if (reactorProject .getArtifactId ().equals (parentArtifact .getArtifactId ())) {
121
+ return Optional . of ( reactorProject ) ;
122
+ }
123
+ }
124
124
}
125
-
126
- return null ;
125
+
126
+ return Optional . absent () ;
127
127
}
128
-
128
+
129
129
/**
130
130
* Load a ".git" git submodule file and read the gitdir path from it.
131
- *
132
- * @param file
131
+ *
133
132
* @return File object with path loaded or null
134
133
*/
135
- private File processGitDirFile (@ NotNull File file ) {
136
- try {
137
- BufferedReader reader = null ;
138
-
139
- try {
134
+ private File processGitDirFile (@ NotNull File file ) {
135
+ try {
136
+ BufferedReader reader = null ;
137
+
138
+ try {
140
139
reader = new BufferedReader (new FileReader (file ));
141
140
142
141
// There should be just one line in the file, e.g.
@@ -145,40 +144,33 @@ private File processGitDirFile(@NotNull File file) {
145
144
146
145
// Separate the key and the value in the string.
147
146
String [] parts = line .split (": " );
148
-
147
+
149
148
// If we don't have 2 parts or if the key is not gitdir then give up.
150
- if (parts .length != 2 || !parts [0 ].equals ("gitdir" )) {
149
+ if (parts .length != 2 || !parts [0 ].equals ("gitdir" )) {
151
150
return null ;
152
151
}
153
152
154
153
// All seems ok so return the "gitdir" value read from the file.
155
154
return new File (parts [1 ]);
156
- }
157
- catch (FileNotFoundException e )
158
- {
155
+ } catch (FileNotFoundException e ) {
159
156
return null ;
157
+ } finally {
158
+ if (reader != null ) {
159
+ reader .close ();
160
+ }
160
161
}
161
- finally
162
- {
163
- if (reader != null )
164
- {
165
- reader .close ();
166
- }
167
- }
168
- }
169
- catch (IOException e )
170
- {
162
+ } catch (IOException e ) {
171
163
return null ;
172
- }
164
+ }
173
165
}
174
-
166
+
175
167
@ NotNull
176
- private File getProjectGitDir (@ NotNull MavenProject mavenProject ) {
168
+ private static File getProjectGitDir (@ NotNull MavenProject mavenProject ) {
177
169
// FIXME Shouldn't this look at the dotGitDirectory property (if set) for the given project?
178
170
return new File (mavenProject .getBasedir (), Constants .DOT_GIT );
179
171
}
180
172
181
- public boolean isExistingDirectory (@ Nullable File fileLocation ) {
173
+ private static boolean isExistingDirectory (@ Nullable File fileLocation ) {
182
174
return fileLocation != null && fileLocation .exists () && fileLocation .isDirectory ();
183
175
}
184
176
}
0 commit comments