Skip to content

Commit fba077c

Browse files
committed
Merge pull request #131 from ffriedrich/encoding
Added possibility to specify encoding for generated git.properties file
2 parents 2043d30 + ca10920 commit fba077c

32 files changed

+513
-14
lines changed

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
import pl.project13.maven.git.log.MavenLoggerBridge;
3535
import pl.project13.maven.git.util.PropertyManager;
3636

37-
import java.io.File;
38-
import java.io.FileWriter;
39-
import java.io.IOException;
37+
import java.io.*;
38+
import java.nio.charset.Charset;
4039
import java.text.SimpleDateFormat;
4140
import java.util.Collections;
4241
import java.util.Date;
@@ -272,7 +271,6 @@ public class GitCommitIdMojo extends AbstractMojo {
272271
@SuppressWarnings("UnusedDeclaration")
273272
private List<String> excludeProperties = Collections.emptyList();
274273

275-
276274
/**
277275
* The properties we store our data in and then expose them
278276
*/
@@ -487,25 +485,25 @@ void loadGitDataWithJGit(@NotNull Properties properties) throws IOException, Moj
487485
}
488486

489487
void generatePropertiesFile(@NotNull Properties properties, File base, String propertiesFilename) throws IOException {
490-
FileWriter fileWriter = null;
488+
Writer outputWriter = null;
491489
File gitPropsFile = craftPropertiesOutputFile(base, propertiesFilename);
492490
try {
493491
Files.createParentDirs(gitPropsFile);
494492

495-
fileWriter = new FileWriter(gitPropsFile);
493+
outputWriter = new OutputStreamWriter(new FileOutputStream(gitPropsFile), Charset.forName("UTF-8"));
496494
if ("json".equalsIgnoreCase(format)) {
497495
log("Writing json file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
498496
ObjectMapper mapper = new ObjectMapper();
499-
mapper.writeValue(fileWriter, properties);
497+
mapper.writeValue(outputWriter, properties);
500498
} else {
501499
log("Writing properties file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
502-
properties.store(fileWriter, "Generated by Git-Commit-Id-Plugin");
500+
properties.store(outputWriter, "Generated by Git-Commit-Id-Plugin");
503501
}
504502

505503
} catch (IOException ex) {
506504
throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex);
507505
} finally {
508-
Closeables.closeQuietly(fileWriter);
506+
Closeables.closeQuietly(outputWriter);
509507
}
510508
}
511509

src/test/java/pl/project13/maven/git/AvailableGitTestRepo.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public enum AvailableGitTestRepo {
5757
*/
5858
WITH_LIGHTWEIGHT_TAG_BEFORE_ANNOTATED_TAG("src/test/resources/_git_lightweight_tag_before_annotated_tag"),
5959
WITH_TAG_ON_DIFFERENT_BRANCH("src/test/resources/_git_with_tag_on_different_branch"),
60-
60+
WITH_ONE_COMMIT_WITH_SPECIAL_CHARACTERS("src/test/resources/_git_one_commit_with_umlaut"),
6161
MAVEN_GIT_COMMIT_ID_PLUGIN(".git");
6262

6363
private String dir;

src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public void shouldGenerateCustomPropertiesFileJson(boolean useNativeGit) throws
211211
// given
212212
mavenSandbox.withParentProject("my-pom-project", "pom")
213213
.withChildProject("my-jar-module", "jar")
214-
.withGitRepoInChild(AvailableGitTestRepo.GIT_COMMIT_ID)
214+
.withGitRepoInChild(AvailableGitTestRepo.WITH_ONE_COMMIT_WITH_SPECIAL_CHARACTERS)
215215
.create(CleanUp.CLEANUP_FIRST);
216216

217217
MavenProject targetProject = mavenSandbox.getChildProject();
@@ -224,17 +224,16 @@ public void shouldGenerateCustomPropertiesFileJson(boolean useNativeGit) throws
224224
alterMojoSettings("generateGitPropertiesFilename", targetFilePath);
225225
alterMojoSettings("format", "json");
226226
alterMojoSettings("useNativeGit", useNativeGit);
227-
228227
// when
229228
try {
230229
mojo.execute();
231230

232231
// then
233232
assertThat(expectedFile).exists();
234-
String json = Files.toString(expectedFile, Charset.defaultCharset());
233+
String json = Files.toString(expectedFile, Charset.forName("UTF-8"));
235234
ObjectMapper om = new ObjectMapper();
236235
Map<String, String> map = new HashMap<String, String>();
237-
map = om.readValue(expectedFile, map.getClass());
236+
map = om.readValue(json, map.getClass());
238237
assertThat(map.size() > 10);
239238
} finally {
240239
FileUtils.forceDelete(expectedFile);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
initial commit on test project
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0b0181b49468f45e573eaf3562f163107c5c121d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = false
4+
bare = false
5+
logallrefupdates = true
6+
symlinks = false
7+
ignorecase = true
8+
hideDotFiles = dotGitOnly
9+
[user]
10+
name = John Doe
11+
12+
[receive]
13+
denynonfastforwards = false
14+
denyCurrentBranch = ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message taken by
4+
# applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit. The hook is
8+
# allowed to edit the commit message file.
9+
#
10+
# To enable this hook, rename this file to "applypatch-msg".
11+
12+
. git-sh-setup
13+
test -x "$GIT_DIR/hooks/commit-msg" &&
14+
exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"}
15+
:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message.
4+
# Called by "git commit" with one argument, the name of the file
5+
# that has the commit message. The hook should exit with non-zero
6+
# status after issuing an appropriate message if it wants to stop the
7+
# commit. The hook is allowed to edit the commit message file.
8+
#
9+
# To enable this hook, rename this file to "commit-msg".
10+
11+
# Uncomment the below to add a Signed-off-by line to the message.
12+
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
13+
# hook is more suited to it.
14+
#
15+
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
16+
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
17+
18+
# This example catches duplicate Signed-off-by lines.
19+
20+
test "" = "$(grep '^Signed-off-by: ' "$1" |
21+
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
22+
echo >&2 Duplicate Signed-off-by lines.
23+
exit 1
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script that is called after a successful
4+
# commit is made.
5+
#
6+
# To enable this hook, rename this file to "post-commit".
7+
8+
: Nothing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script for the "post-receive" event.
4+
#
5+
# The "post-receive" script is run after receive-pack has accepted a pack
6+
# and the repository has been updated. It is passed arguments in through
7+
# stdin in the form
8+
# <oldrev> <newrev> <refname>
9+
# For example:
10+
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
11+
#
12+
# see contrib/hooks/ for a sample, or uncomment the next line and
13+
# rename the file to "post-receive".
14+
15+
#. /usr/share/doc/git-core/contrib/hooks/post-receive-email
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to prepare a packed repository for use over
4+
# dumb transports.
5+
#
6+
# To enable this hook, rename this file to "post-update".
7+
8+
exec git update-server-info
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to verify what is about to be committed
4+
# by applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit.
8+
#
9+
# To enable this hook, rename this file to "pre-applypatch".
10+
11+
. git-sh-setup
12+
test -x "$GIT_DIR/hooks/pre-commit" &&
13+
exec "$GIT_DIR/hooks/pre-commit" ${1+"$@"}
14+
:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to verify what is about to be committed.
4+
# Called by "git commit" with no arguments. The hook should
5+
# exit with non-zero status after issuing an appropriate message if
6+
# it wants to stop the commit.
7+
#
8+
# To enable this hook, rename this file to "pre-commit".
9+
10+
if git rev-parse --verify HEAD >/dev/null 2>&1
11+
then
12+
against=HEAD
13+
else
14+
# Initial commit: diff against an empty tree object
15+
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
16+
fi
17+
18+
# If you want to allow non-ascii filenames set this variable to true.
19+
allownonascii=$(git config hooks.allownonascii)
20+
21+
# Redirect output to stderr.
22+
exec 1>&2
23+
24+
# Cross platform projects tend to avoid non-ascii filenames; prevent
25+
# them from being added to the repository. We exploit the fact that the
26+
# printable range starts at the space character and ends with tilde.
27+
if [ "$allownonascii" != "true" ] &&
28+
# Note that the use of brackets around a tr range is ok here, (it's
29+
# even required, for portability to Solaris 10's /usr/bin/tr), since
30+
# the square bracket bytes happen to fall in the designated range.
31+
test $(git diff --cached --name-only --diff-filter=A -z $against |
32+
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
33+
then
34+
echo "Error: Attempt to add a non-ascii file name."
35+
echo
36+
echo "This can cause problems if you want to work"
37+
echo "with people on other platforms."
38+
echo
39+
echo "To be portable it is advisable to rename the file ..."
40+
echo
41+
echo "If you know what you are doing you can disable this"
42+
echo "check using:"
43+
echo
44+
echo " git config hooks.allownonascii true"
45+
echo
46+
exit 1
47+
fi
48+
49+
# If there are whitespace errors, print the offending file names and fail.
50+
exec git diff-index --check --cached $against --

0 commit comments

Comments
 (0)