Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/main/java/io/github/pepperkit/githooks/GitHooksManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,20 @@ void checkProvidedHookNamesCorrectness(Map<String, String> hooks) {
}
}

/**
* Determines if current working directory is the root directory of a git repository.
* @return true if it is a git repository, else false
*/
public boolean isGitRepository() {
return Files.exists(GIT_PATH);
}

/**
* Checks that git hooks directory exists, and creates it if it doesn't.
* @throws IllegalStateException if git repository was not initialized
* or there's an error on creating git hooks directory
*/
void checkGitHooksDirAndCreateIfMissing() {
if (!Files.exists(GIT_PATH)) {
throw new IllegalStateException("It seems that it's not a git repository. " +
"Plugin goals should be executed from the root of the project.");
}

if (!Files.exists(GIT_HOOKS_PATH)) {
try {
Files.createDirectories(GIT_HOOKS_PATH);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/github/pepperkit/githooks/InitHooksMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public void setHooks(String hooks) {

@Override
public void execute() throws MojoExecutionException {
if (!gitHooksManager.isGitRepository()) {
getLog().info("No git repository found. " +
"Plugin goals will only be executed from the root of the project.");
return;
}

List<File> existingHookFiles = gitHooksManager.getExistingHookFiles();
if (hooks == null) {
existingHookFiles.forEach(File::delete);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
*/
package io.github.pepperkit.githooks;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -32,6 +30,14 @@ void beforeEach() {
gitHooksManagerMock = mock(GitHooksManager.class);
initHooksMojo = new InitHooksMojo();
initHooksMojo.gitHooksManager = gitHooksManagerMock;
when(gitHooksManagerMock.isGitRepository()).thenReturn(true);
}

@Test
void executesNothingIfNotAGitRepository() throws MojoExecutionException {
when(gitHooksManagerMock.isGitRepository()).thenReturn(false);
initHooksMojo.execute();
verify(gitHooksManagerMock, times(0)).getExistingHookFiles();
}

@Test
Expand Down