2525import javax .annotation .Nonnull ;
2626import javax .annotation .Nullable ;
2727import 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 */
3334public 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.
0 commit comments