Skip to content

Commit 7517a2a

Browse files
author
TheSnoozer
committed
move the logic if we should fail into the GitDirLocator
1 parent 7cac71d commit 7517a2a

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/main/java/pl/project13/core/GitCommitIdPlugin.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -359,28 +359,17 @@ protected static void loadGitData(@Nonnull Callback cb, @Nonnull Properties prop
359359
}
360360
}
361361

362-
private static File findDotGitDirectory(@Nonnull Callback cb) throws GitCommitIdExecutionException {
363-
File dotGitDirectory = lookupGitDirectory(cb);
364-
if (cb.shouldFailOnNoGitDirectory() && !directoryExists(dotGitDirectory)) {
365-
throw new GitCommitIdExecutionException(
366-
".git directory is not found! Please specify a valid [dotGitDirectory] in your"
367-
+ " project");
368-
}
369-
return dotGitDirectory;
370-
}
371-
372-
private static boolean directoryExists(@Nullable File fileLocation) {
373-
return fileLocation != null && fileLocation.exists() && fileLocation.isDirectory();
374-
}
375-
376362
/**
377363
* Find the git directory of the currently used project. If it's not already specified, this
378364
* method will try to find it.
379365
*
380366
* @return the File representation of the .git directory
381367
*/
382-
private static File lookupGitDirectory(@Nonnull Callback cb) throws GitCommitIdExecutionException {
383-
return new GitDirLocator(cb.getProjectBaseDir()).lookupGitDirectory(cb.getDotGitDirectory());
368+
private static File findDotGitDirectory(@Nonnull Callback cb) throws GitCommitIdExecutionException {
369+
return new GitDirLocator(
370+
cb.getProjectBaseDir(),
371+
cb.shouldFailOnNoGitDirectory()
372+
).lookupGitDirectory(cb.getDotGitDirectory());
384373
}
385374

386375
private static void loadGitDataWithNativeGit(

src/main/java/pl/project13/core/util/GitDirLocator.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,25 @@
2525
import javax.annotation.Nonnull;
2626
import javax.annotation.Nullable;
2727
import org.eclipse.jgit.lib.Constants;
28+
import pl.project13.core.GitCommitIdExecutionException;
2829

2930
/**
3031
* This class encapsulates logic to locate a valid .git directory of the currently used project. If
3132
* it's not already specified, this logic will try to find it.
3233
*/
3334
public class GitDirLocator {
3435
final File projectBasedir;
36+
final boolean shouldFailOnNoGitDirectory;
3537

3638
/**
3739
* Constructor to encapsulates all references required to locate a valid .git directory
3840
*
3941
* @param projectBasedir The project basedir that will be used as last resort to search
4042
* the parent project hierarchy until a .git directory is found.
4143
*/
42-
public GitDirLocator(File projectBasedir) {
44+
public GitDirLocator(File projectBasedir, boolean shouldFailOnNoGitDirectory) {
4345
this.projectBasedir = projectBasedir;
46+
this.shouldFailOnNoGitDirectory = shouldFailOnNoGitDirectory;
4447
}
4548

4649
/**
@@ -53,7 +56,22 @@ public GitDirLocator(File projectBasedir) {
5356
* location or within the project or it's reactor projects.
5457
*/
5558
@Nullable
56-
public File lookupGitDirectory(@Nonnull File manuallyConfiguredDir) {
59+
public File lookupGitDirectory(@Nonnull File manuallyConfiguredDir) throws GitCommitIdExecutionException {
60+
File dotGitDirectory = runSearch(manuallyConfiguredDir);
61+
if (shouldFailOnNoGitDirectory && !directoryExists(dotGitDirectory)) {
62+
throw new GitCommitIdExecutionException(
63+
".git directory is not found! Please specify a valid [dotGitDirectory] in your"
64+
+ " project");
65+
}
66+
return dotGitDirectory;
67+
}
68+
69+
private static boolean directoryExists(@Nullable File fileLocation) {
70+
return fileLocation != null && fileLocation.exists() && fileLocation.isDirectory();
71+
}
72+
73+
74+
private File runSearch(@Nonnull File manuallyConfiguredDir) {
5775
if (manuallyConfiguredDir.exists()) {
5876

5977
// If manuallyConfiguredDir is a directory then we can use it as the git path.

src/test/java/pl/project13/core/util/GitDirLocatorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void shouldUseTheManuallySpecifiedDirectory() throws Exception {
3838
File dotGitDir = folder.newFolder("temp");
3939
try {
4040
// when
41-
GitDirLocator locator = new GitDirLocator(dotGitDir);
41+
GitDirLocator locator = new GitDirLocator(dotGitDir, true);
4242
File foundDirectory = locator.lookupGitDirectory(dotGitDir);
4343

4444
// then
@@ -71,7 +71,7 @@ public void shouldResolveRelativeSubmodule() throws Exception {
7171

7272
try {
7373
// when
74-
GitDirLocator locator = new GitDirLocator(dotGitDir);
74+
GitDirLocator locator = new GitDirLocator(dotGitDir, true);
7575
File foundDirectory = locator.lookupGitDirectory(dotGitDir);
7676

7777
// then

0 commit comments

Comments
 (0)