Skip to content

Commit e020be1

Browse files
author
TheSnoozer
committed
make the GitDirLocator return the directory where both implementations can collect their results (e.g. the parent directory for the NativeGitImplementation)
1 parent 7517a2a commit e020be1

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,11 @@ protected static void loadGitData(@Nonnull Callback cb, @Nonnull Properties prop
344344
throw new GitCommitIdExecutionException("suspicious argument for evaluateOnCommit, aborting execution!");
345345
}
346346

347-
File dotGitDirectory = findDotGitDirectory(cb);
347+
File dotGitDirectory = new GitDirLocator(
348+
cb.getProjectBaseDir(),
349+
cb.useNativeGit(),
350+
cb.shouldFailOnNoGitDirectory()
351+
).lookupGitDirectory(cb.getDotGitDirectory());
348352
if (dotGitDirectory != null) {
349353
cb.getLogInterface().info("dotGitDirectory '" + dotGitDirectory.getAbsolutePath() + "'");
350354
} else {
@@ -359,25 +363,12 @@ protected static void loadGitData(@Nonnull Callback cb, @Nonnull Properties prop
359363
}
360364
}
361365

362-
/**
363-
* Find the git directory of the currently used project. If it's not already specified, this
364-
* method will try to find it.
365-
*
366-
* @return the File representation of the .git directory
367-
*/
368-
private static File findDotGitDirectory(@Nonnull Callback cb) throws GitCommitIdExecutionException {
369-
return new GitDirLocator(
370-
cb.getProjectBaseDir(),
371-
cb.shouldFailOnNoGitDirectory()
372-
).lookupGitDirectory(cb.getDotGitDirectory());
373-
}
374-
375366
private static void loadGitDataWithNativeGit(
376367
@Nonnull Callback cb,
377368
@Nonnull File dotGitDirectory,
378369
@Nonnull Properties properties) throws GitCommitIdExecutionException {
379370
GitDataProvider nativeGitProvider = NativeGitProvider
380-
.on(dotGitDirectory.getParentFile(), cb.getNativeGitTimeoutInMs(), cb.getLogInterface())
371+
.on(dotGitDirectory, cb.getNativeGitTimeoutInMs(), cb.getLogInterface())
381372
.setPrefixDot(cb.getPrefixDot())
382373
.setAbbrevLength(cb.getAbbrevLength())
383374
.setDateFormat(cb.getDateFormat())

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,27 @@
3333
*/
3434
public class GitDirLocator {
3535
final File projectBasedir;
36+
final boolean useNativeGit;
3637
final boolean shouldFailOnNoGitDirectory;
3738

3839
/**
3940
* Constructor to encapsulates all references required to locate a valid .git directory
4041
*
4142
* @param projectBasedir The project basedir that will be used as last resort to search
4243
* the parent project hierarchy until a .git directory is found.
44+
* @param useNativeGit Boolean that indicates if we use the native git implementation or the
45+
* jGit Implementation. For the native git we usually need to
46+
* use the parent "git"-Folder, as git can not run commands
47+
* in "your-project/.git".
48+
* @param shouldFailOnNoGitDirectory Boolean that indicates if the process should fail if no
49+
* git directory can be found.
4350
*/
44-
public GitDirLocator(File projectBasedir, boolean shouldFailOnNoGitDirectory) {
51+
public GitDirLocator(
52+
File projectBasedir,
53+
boolean useNativeGit,
54+
boolean shouldFailOnNoGitDirectory) {
4555
this.projectBasedir = projectBasedir;
56+
this.useNativeGit = useNativeGit;
4657
this.shouldFailOnNoGitDirectory = shouldFailOnNoGitDirectory;
4758
}
4859

@@ -63,6 +74,9 @@ public File lookupGitDirectory(@Nonnull File manuallyConfiguredDir) throws GitCo
6374
".git directory is not found! Please specify a valid [dotGitDirectory] in your"
6475
+ " project");
6576
}
77+
if (useNativeGit) {
78+
dotGitDirectory = dotGitDirectory.getParentFile();
79+
}
6680
return dotGitDirectory;
6781
}
6882

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, true);
41+
GitDirLocator locator = new GitDirLocator(dotGitDir, false, 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, true);
74+
GitDirLocator locator = new GitDirLocator(dotGitDir, false, true);
7575
File foundDirectory = locator.lookupGitDirectory(dotGitDir);
7676

7777
// then

0 commit comments

Comments
 (0)