Skip to content

Commit ffbe8f7

Browse files
dwightmulcahyphillipuniverse
authored andcommitted
Added hook name validation (#5)
Changed debug log msg to info to output the hooks that are written. No need to add the SHEBANG if the script already starts with one. Made NEW_LINE none system dependent.
1 parent f040c89 commit ffbe8f7

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

src/main/java/org/sandbox/GitHookInstallMojo.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,38 @@
1313

1414
import java.io.File;
1515
import java.io.IOException;
16+
import java.nio.charset.Charset;
1617
import java.nio.file.Files;
1718
import java.nio.file.Path;
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.List;
1822
import java.util.Map;
1923

2024
@Mojo(name = "install", defaultPhase = LifecyclePhase.INITIALIZE, threadSafe = true)
2125
public final class GitHookInstallMojo extends AbstractMojo {
2226

23-
private static final String SHEBANG = "#!/bin/sh";
27+
private static final String NEW_LINE = System.lineSeparator();
28+
private static final String SHEBANG = "#!/bin/sh" + NEW_LINE;
29+
private static final List<String> validHooks = Arrays.asList(
30+
"applypatch-msg",
31+
"pre-applypatch",
32+
"post-applypatch",
33+
"pre-commit",
34+
"prepare-commit-msg",
35+
"commit-msg",
36+
"post-commit",
37+
"pre-rebase",
38+
"post-checkout",
39+
"post-merge",
40+
"pre-receive",
41+
"update",
42+
"post-receive",
43+
"post-update",
44+
"pre-auto-gc",
45+
"post-rewrite",
46+
"pre-push"
47+
);
2448

2549
@Parameter
2650
private Map<String, String> hooks;
@@ -41,18 +65,23 @@ public void execute() throws MojoExecutionException, MojoFailureException {
4165

4266
for (Map.Entry<String, String> hook : hooks.entrySet()) {
4367
String hookName = hook.getKey();
44-
String finalScript = SHEBANG + '\n' + hook.getValue();
68+
if (!validHooks.contains(hookName)) {
69+
getLog().error( String.format("`%s` hook is not a valid git-hook name", hookName) );
70+
continue;
71+
}
72+
73+
String hookScript = hook.getValue();
74+
String finalScript = (hookScript.startsWith("#!") ? "" : SHEBANG) + hookScript + NEW_LINE;
4575
try {
46-
getLog().debug(String.format("Installing %s hook into %s", hookName,
47-
hooksDir.toAbsolutePath().toString()));
48-
writeFile(hooksDir.resolve(hookName), finalScript.getBytes());
76+
getLog().info( String.format("Installing %s hook into %s", hookName, hooksDir.toAbsolutePath().toString()) );
77+
writeFile(hooksDir.resolve(hookName), finalScript.getBytes(Charset.forName("UTF-8")));
4978
} catch (IOException e) {
5079
throw new MojoExecutionException("Could not write hook with name: " + hookName, e);
5180
}
5281
}
5382
}
5483

55-
protected synchronized void writeFile(Path path, byte[] bytes) throws IOException {
84+
private synchronized void writeFile(Path path, byte[] bytes) throws IOException {
5685
File created = Files.write(path, bytes, CREATE, TRUNCATE_EXISTING).toFile();
5786
boolean success = created.setExecutable(true, true)
5887
&& created.setReadable(true, true)

0 commit comments

Comments
 (0)