Skip to content

Commit 01b3fbd

Browse files
committed
cleaning up and releasing update to support submodules
initial resolution of #65 resolves #65
1 parent 78a3dd8 commit 01b3fbd

File tree

5 files changed

+232
-102
lines changed

5 files changed

+232
-102
lines changed

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,31 @@
1717

1818
package pl.project13.maven.git;
1919

20-
import java.io.File;
21-
import java.io.FileWriter;
22-
import java.io.IOException;
23-
import java.text.SimpleDateFormat;
24-
import java.util.Date;
25-
import java.util.Iterator;
26-
import java.util.List;
27-
import java.util.Properties;
28-
20+
import com.google.common.annotations.VisibleForTesting;
21+
import com.google.common.io.Closeables;
22+
import com.google.common.io.Files;
2923
import org.apache.maven.plugin.AbstractMojo;
3024
import org.apache.maven.plugin.MojoExecutionException;
3125
import org.apache.maven.project.MavenProject;
3226
import org.eclipse.jgit.api.errors.GitAPIException;
33-
import org.eclipse.jgit.lib.AbbreviatedObjectId;
34-
import org.eclipse.jgit.lib.Constants;
35-
import org.eclipse.jgit.lib.ObjectReader;
36-
import org.eclipse.jgit.lib.Ref;
37-
import org.eclipse.jgit.lib.Repository;
27+
import org.eclipse.jgit.lib.*;
3828
import org.eclipse.jgit.revwalk.RevCommit;
3929
import org.eclipse.jgit.revwalk.RevWalk;
4030
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
4131
import org.jetbrains.annotations.NotNull;
4232
import org.jetbrains.annotations.Nullable;
43-
4433
import pl.project13.jgit.DescribeCommand;
4534
import pl.project13.jgit.DescribeResult;
4635
import pl.project13.maven.git.log.LoggerBridge;
4736
import pl.project13.maven.git.log.MavenLoggerBridge;
4837

49-
import com.google.common.annotations.VisibleForTesting;
50-
import com.google.common.io.Closeables;
51-
import com.google.common.io.Files;
38+
import java.io.File;
39+
import java.io.FileWriter;
40+
import java.io.IOException;
41+
import java.text.SimpleDateFormat;
42+
import java.util.Date;
43+
import java.util.List;
44+
import java.util.Properties;
5245

5346
/**
5447
* Goal which puts git build-time information into property files or maven's properties.
@@ -92,12 +85,16 @@ public class GitCommitIdMojo extends AbstractMojo {
9285
* @parameter expression="${reactorProjects}"
9386
* @readonly
9487
*/
88+
@SuppressWarnings("UnusedDeclaration")
9589
private List<MavenProject> reactorProjects;
9690

9791
/**
9892
* Tell git-commit-id to inject the git properties into all
9993
* reactor projects not just the current one.
10094
*
95+
* For details about why you might want to skip this, read this issue: https://github.com/ktoso/maven-git-commit-id-plugin/pull/65
96+
* Basically, injecting into all projects may slow down the build and you don't always need this feature.
97+
*
10198
* @parameter default-value="true"
10299
*/
103100
@SuppressWarnings("UnusedDeclaration")
@@ -271,10 +268,9 @@ public void execute() throws MojoExecutionException {
271268
log("Finished running.");
272269
}
273270

274-
private void appendPropertiesToReactorProjects(@NotNull Properties properies) {
271+
private void appendPropertiesToReactorProjects(@NotNull Properties properties) {
275272
log("Appending git properties to all reactor projects:");
276273

277-
if (reactorProjects != null) {
278274
for (MavenProject mavenProject : reactorProjects) {
279275
Properties mavenProperties = mavenProject.getProperties();
280276

@@ -284,7 +280,6 @@ private void appendPropertiesToReactorProjects(@NotNull Properties properies) {
284280
mavenProperties.put(key, properties.get(key));
285281
}
286282
}
287-
}
288283
}
289284

290285
private void throwWhenRequiredDirectoryNotFound(File dotGitDirectory, Boolean required, String message) throws MojoExecutionException {

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

Lines changed: 73 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -17,126 +17,125 @@
1717

1818
package pl.project13.maven.git;
1919

20+
import com.google.common.base.Optional;
2021
import org.apache.maven.artifact.Artifact;
2122
import org.apache.maven.project.MavenProject;
2223
import org.eclipse.jgit.lib.Constants;
2324
import org.jetbrains.annotations.NotNull;
2425
import org.jetbrains.annotations.Nullable;
2526

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.*;
3128
import java.util.List;
3229

3330
/**
34-
* Encapsulates logic to locate a valid .git directory
31+
* Encapsulates logic to locate a valid .git directory.
32+
*
3533
* @author <a href="mailto:konrad.malawski@java.pl">Konrad 'ktoso' Malawski</a>
3634
*/
3735
public class GitDirLocator {
38-
final MavenProject mavenProject;
36+
final MavenProject mavenProject;
3937
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;
4442
}
45-
43+
4644
@Nullable
47-
public File lookupGitDirectory(File manuallyConfiguredDir) {
45+
public File lookupGitDirectory(@NotNull File manuallyConfiguredDir) {
46+
47+
if (manuallyConfiguredDir.exists()) {
4848

49-
if (manuallyConfiguredDir != null && manuallyConfiguredDir.exists()) {
50-
5149
// If manuallyConfiguredDir is a directory then we can use it as the git path.
5250
if (manuallyConfiguredDir.isDirectory()) {
53-
return manuallyConfiguredDir;
51+
return manuallyConfiguredDir;
5452
}
55-
53+
5654
// If the path exists but is not a directory it might be a git submodule "gitdir" link.
5755
File gitDirLinkPath = processGitDirFile(manuallyConfiguredDir);
58-
56+
5957
// 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;
6360
}
64-
61+
6562
/**
6663
* FIXME: I think we should fail here because a manual path was set and it was not found
6764
* but I'm leaving it falling back to searching for the git path because that is the current
6865
* behaviour - Unluckypixie.
6966
*/
7067
}
7168

72-
return findProjectGitDirectory();
69+
return findProjectGitDirectory();
7370
}
7471

7572
/**
7673
* Search up all the maven parent project heirarchy until a .git
7774
* 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.
8077
*/
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+
8885
if (isExistingDirectory(dir)) {
8986
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);
9887
} 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+
}
101102
}
102103
}
103104

104105
return null;
105106
}
106-
107+
107108
/**
108109
* Find a project in the reactor by its artifact, I'm new to maven coding
109110
* so there may be a better way to do this, it would not be necessary
110111
* if project.getParent() actually worked.
111-
*
112-
* @param MavenProject project
112+
*
113113
* @return MavenProject parent project or NULL if no parent available
114114
*/
115-
private MavenProject getReactorParentProject(MavenProject project) {
115+
private Optional<MavenProject> getReactorParentProject(@NotNull MavenProject project) {
116116
Artifact parentArtifact = project.getParentArtifact();
117-
117+
118118
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+
}
124124
}
125-
126-
return null;
125+
126+
return Optional.absent();
127127
}
128-
128+
129129
/**
130130
* Load a ".git" git submodule file and read the gitdir path from it.
131-
*
132-
* @param file
131+
*
133132
* @return File object with path loaded or null
134133
*/
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 {
140139
reader = new BufferedReader(new FileReader(file));
141140

142141
// There should be just one line in the file, e.g.
@@ -145,40 +144,33 @@ private File processGitDirFile(@NotNull File file) {
145144

146145
// Separate the key and the value in the string.
147146
String[] parts = line.split(": ");
148-
147+
149148
// 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")) {
151150
return null;
152151
}
153152

154153
// All seems ok so return the "gitdir" value read from the file.
155154
return new File(parts[1]);
156-
}
157-
catch (FileNotFoundException e)
158-
{
155+
} catch (FileNotFoundException e) {
159156
return null;
157+
} finally {
158+
if (reader != null) {
159+
reader.close();
160+
}
160161
}
161-
finally
162-
{
163-
if (reader != null)
164-
{
165-
reader.close();
166-
}
167-
}
168-
}
169-
catch (IOException e)
170-
{
162+
} catch (IOException e) {
171163
return null;
172-
}
164+
}
173165
}
174-
166+
175167
@NotNull
176-
private File getProjectGitDir(@NotNull MavenProject mavenProject) {
168+
private static File getProjectGitDir(@NotNull MavenProject mavenProject) {
177169
// FIXME Shouldn't this look at the dotGitDirectory property (if set) for the given project?
178170
return new File(mavenProject.getBasedir(), Constants.DOT_GIT);
179171
}
180172

181-
public boolean isExistingDirectory(@Nullable File fileLocation) {
173+
private static boolean isExistingDirectory(@Nullable File fileLocation) {
182174
return fileLocation != null && fileLocation.exists() && fileLocation.isDirectory();
183175
}
184176
}

src/test/java/pl/project13/maven/git/AvailableGitTestRepo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public enum AvailableGitTestRepo {
4747
*/
4848
WITH_COMMIT_THAT_HAS_TWO_TAGS("src/test/resources/_git_with_commit_that_has_two_tags"),
4949
ON_A_TAG_DIRTY("src/test/resources/_git_on_a_tag_dirty"),
50+
WITH_SUBMODULES("src/test/resources/_git_with_submodules"),
5051
/**
5152
* <pre>
5253
* b6a73ed - (HEAD, master) third addition (4 minutes ago) <Konrad Malawski>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.maven.git;
19+
20+
import com.google.common.io.Files;
21+
import org.apache.maven.project.MavenProject;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.mockito.MockitoAnnotations.Mock;
25+
import org.mockito.runners.MockitoJUnitRunner;
26+
27+
import java.io.File;
28+
import java.util.Collections;
29+
import java.util.List;
30+
31+
import static org.fest.assertions.Assertions.assertThat;
32+
33+
@RunWith(MockitoJUnitRunner.class)
34+
public class GitDirLocatorTest {
35+
36+
@Mock
37+
MavenProject project;
38+
39+
List<MavenProject> reactorProjects = Collections.emptyList();
40+
41+
@Test
42+
public void shouldUseTheManualySpecifiedDirectory() throws Exception {
43+
// given
44+
File dotGitDir = Files.createTempDir();
45+
46+
// when
47+
GitDirLocator locator = new GitDirLocator(project, reactorProjects);
48+
File foundDirectory = locator.lookupGitDirectory(dotGitDir);
49+
50+
// then
51+
assertThat(foundDirectory.getAbsolutePath()).isEqualTo(dotGitDir.getAbsolutePath());
52+
}
53+
54+
}

0 commit comments

Comments
 (0)