Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally allow overwriting of the hooks file name #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ private boolean isEnabled() {
if ((!includedModules.isEmpty() || !excludedModules.isEmpty()) && isExecutionRoot()) {
getLog()
.info(
"Explicit included or excluded modules defined and the current module the execution root. Goal disabled.");
"Explicit included or excluded modules defined and the current module the execution"
+ " root. Goal disabled.");
return false;
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/cosium/code/format/FormatCodeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import com.cosium.code.format.formatter.CodeFormatter;
import com.cosium.code.format.formatter.LineRanges;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

/**
* Created on 07/11/17.
Expand Down
50 changes: 31 additions & 19 deletions src/main/java/com/cosium/code/format/InstallHooksMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -74,13 +75,17 @@ public class InstallHooksMojo extends AbstractMavenGitCodeFormatMojo {
@Parameter(property = "gcf.preCommitHookPipeline", defaultValue = "")
private String preCommitHookPipeline;

@Parameter(property = "gcf.hookScriptName")
private String hookScriptName;

@Override
public void execute() throws MojoExecutionException {
if (!isExecutionRoot()) {
getLog().debug("Not in execution root. Do not execute.");
return;
}
if (skip || skipInstallHooks) {
Log log = getLog();
if (this.skip || this.skipInstallHooks) {
final Log log = getLog();
if (log.isInfoEnabled()) {
log.info("skipped");
}
Expand All @@ -91,42 +96,42 @@ public void execute() throws MojoExecutionException {
getLog().info("Installing git hooks");
doExecute();
getLog().info("Installed git hooks");
} catch (Exception e) {
} catch (final Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}

private void doExecute() throws IOException {
Path hooksDirectory = prepareHooksDirectory();
final Path hooksDirectory = prepareHooksDirectory();

writePluginHooks(hooksDirectory);

configureHookBaseScripts(hooksDirectory);
}

private void writePluginHooks(Path hooksDirectory) throws IOException {
private void writePluginHooks(final Path hooksDirectory) throws IOException {
getLog().debug("Removing legacy pre commit hook file");
Files.deleteIfExists(hooksDirectory.resolve(legacyPluginPreCommitHookFileName()));
getLog().debug("Rmeoved legacy pre commit hook file");

getLog().debug("Writing plugin pre commit hook file");
executableManager
this.executableManager
.getOrCreateExecutableScript(hooksDirectory.resolve(pluginPreCommitHookFileName()))
.truncateWithTemplate(
() -> getClass().getResourceAsStream(BASE_PLUGIN_PRE_COMMIT_HOOK),
StandardCharsets.UTF_8.toString(),
mavenEnvironment.getMavenExecutable(debug).toAbsolutePath(),
this.mavenEnvironment.getMavenExecutable(this.debug).toAbsolutePath(),
pomFile().toAbsolutePath(),
mavenCliArguments());
getLog().debug("Written plugin pre commit hook file");
}

private void configureHookBaseScripts(Path hooksDirectory) throws IOException {
Executable basePreCommitHook =
executableManager.getOrCreateExecutableScript(
private void configureHookBaseScripts(final Path hooksDirectory) throws IOException {
final Executable basePreCommitHook =
this.executableManager.getOrCreateExecutableScript(
hooksDirectory.resolve(PRE_COMMIT_HOOK_BASE_SCRIPT));
getLog().debug("Configuring '" + basePreCommitHook + "'");
if (truncateHooksBaseScripts) {
if (this.truncateHooksBaseScripts) {
basePreCommitHook.truncate();
} else {
legacyPreCommitHookBaseScriptCalls().forEach(basePreCommitHook::removeCommandCall);
Expand All @@ -135,15 +140,18 @@ private void configureHookBaseScripts(Path hooksDirectory) throws IOException {
}

private String mavenCliArguments() {
Stream<String> propagatedProperties =
ofNullable(propertiesToPropagate).map(Arrays::asList).orElse(Collections.emptyList())
final Stream<String> propagatedProperties =
ofNullable(this.propertiesToPropagate)
.map(Arrays::asList)
.orElse(Collections.emptyList())
.stream()
.filter(prop -> System.getProperty(prop) != null)
.map(prop -> "-D" + prop + "=" + System.getProperty(prop));

Stream<String> properties = Stream.concat(propagatedProperties, Stream.of(propertiesToAdd));
if (preCommitHookPipeline != null && !preCommitHookPipeline.isEmpty()) {
properties = Stream.concat(properties, Stream.of(preCommitHookPipeline));
Stream<String> properties =
Stream.concat(propagatedProperties, Stream.of(this.propertiesToAdd));
if (this.preCommitHookPipeline != null && !this.preCommitHookPipeline.isEmpty()) {
properties = Stream.concat(properties, Stream.of(this.preCommitHookPipeline));
}
return properties.collect(Collectors.joining(" "));
}
Expand All @@ -161,7 +169,7 @@ private String preCommitHookBaseScriptCall() {
}

private List<String> legacyPreCommitHookBaseScriptCalls() {
List<String> calls = new ArrayList<>();
final List<String> calls = new ArrayList<>();
calls.add(
"./"
+ gitBaseDir().relativize(getOrCreateHooksDirectory())
Expand All @@ -175,11 +183,15 @@ private List<String> legacyPreCommitHookBaseScriptCalls() {
return calls;
}

private String getHookScriptName() {
return Optional.ofNullable(this.hookScriptName).orElseGet(this::artifactId);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the actual change introduced.

}

private String pluginPreCommitHookFileName() {
return artifactId() + "." + BASE_PLUGIN_PRE_COMMIT_HOOK;
return getHookScriptName() + "." + BASE_PLUGIN_PRE_COMMIT_HOOK;
}

private String legacyPluginPreCommitHookFileName() {
return artifactId() + "." + LEGACY_BASE_PLUGIN_PRE_COMMIT_HOOK;
return getHookScriptName() + "." + LEGACY_BASE_PLUGIN_PRE_COMMIT_HOOK;
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/cosium/code/format/TemporaryFile.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.cosium.code.format;

import org.apache.maven.plugin.logging.Log;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.maven.plugin.logging.Log;

/** @author Réda Housni Alaoui */
public class TemporaryFile implements Closeable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.cosium.code.format.executable;

import com.cosium.code.format.MavenGitCodeFormatException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.logging.Log;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.function.Supplier;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.logging.Log;

/** @author Réda Housni Alaoui */
public class DefaultCommandRunner implements CommandRunner {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.cosium.code.format.git;

import static java.util.Objects.requireNonNull;

import java.io.IOException;
import java.util.Collection;
import java.util.Set;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.AbbreviatedObjectId;
Expand All @@ -9,12 +14,6 @@
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectReader;

import java.io.IOException;
import java.util.Collection;
import java.util.Set;

import static java.util.Objects.requireNonNull;

/** @author Réda Housni Alaoui */
public class AutoCRLFObjectReader extends ObjectReader {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.cosium.code.format.git;

import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
import org.eclipse.jgit.lib.ObjectStream;
import org.eclipse.jgit.util.io.EolStreamTypeUtil;
import static java.util.Objects.requireNonNull;

import java.io.IOException;
import java.io.InputStream;

import static java.util.Objects.requireNonNull;
import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
import org.eclipse.jgit.lib.ObjectStream;
import org.eclipse.jgit.util.io.EolStreamTypeUtil;

/** @author Réda Housni Alaoui */
public class AutoCRLFObjectStream extends ObjectStream {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.cosium.code.format.git;

import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
import org.eclipse.jgit.lib.ObjectReader;
import static java.util.Objects.requireNonNull;

import java.io.File;
import java.io.IOException;

import static java.util.Objects.requireNonNull;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
import org.eclipse.jgit.lib.ObjectReader;

/** @author Réda Housni Alaoui */
public class AutoCRLFRepository extends FileRepository {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/cosium/code/format/git/GitIndexEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,12 @@ private LineRanges computeLineRanges(DirCacheEntry dirCacheEntry) {
}

try {
return git.diff().setPathFilter(PathFilter.create(dirCacheEntry.getPathString()))
.setCached(true).call().stream()
return git
.diff()
.setPathFilter(PathFilter.create(dirCacheEntry.getPathString()))
.setCached(true)
.call()
.stream()
.map(this::computeLineRanges)
.reduce(LineRanges::concat)
.orElse(LineRanges.all());
Expand Down
23 changes: 11 additions & 12 deletions src/main/java/com/cosium/code/format/git/GitStagedFiles.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package com.cosium.code.format.git;

import static java.util.Objects.requireNonNull;

import com.cosium.code.format.MavenGitCodeFormatException;
import com.cosium.code.format.TemporaryFile;
import com.cosium.code.format.formatter.CodeFormatters;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.maven.plugin.logging.Log;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status;
Expand All @@ -16,18 +27,6 @@
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.WorkingTreeOptions;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Objects.requireNonNull;

/** @author Réda Housni Alaoui */
public class GitStagedFiles {

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/cosium/code/format/git/Index.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.cosium.code.format.git;

import java.io.IOException;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheEditor;
import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;

import java.io.IOException;

/** @author Réda Housni Alaoui */
public class Index implements AutoCloseable {

Expand Down