Skip to content

Commit

Permalink
Merge pull request #131 from ffriedrich/encoding
Browse files Browse the repository at this point in the history
Added possibility to specify encoding for generated git.properties file
  • Loading branch information
ktoso committed Sep 23, 2014
2 parents 2043d30 + ca10920 commit fba077c
Show file tree
Hide file tree
Showing 32 changed files with 513 additions and 14 deletions.
16 changes: 7 additions & 9 deletions src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@
import pl.project13.maven.git.log.MavenLoggerBridge;
import pl.project13.maven.git.util.PropertyManager;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
Expand Down Expand Up @@ -272,7 +271,6 @@ public class GitCommitIdMojo extends AbstractMojo {
@SuppressWarnings("UnusedDeclaration")
private List<String> excludeProperties = Collections.emptyList();


/**
* The properties we store our data in and then expose them
*/
Expand Down Expand Up @@ -487,25 +485,25 @@ void loadGitDataWithJGit(@NotNull Properties properties) throws IOException, Moj
}

void generatePropertiesFile(@NotNull Properties properties, File base, String propertiesFilename) throws IOException {
FileWriter fileWriter = null;
Writer outputWriter = null;
File gitPropsFile = craftPropertiesOutputFile(base, propertiesFilename);
try {
Files.createParentDirs(gitPropsFile);

fileWriter = new FileWriter(gitPropsFile);
outputWriter = new OutputStreamWriter(new FileOutputStream(gitPropsFile), Charset.forName("UTF-8"));
if ("json".equalsIgnoreCase(format)) {
log("Writing json file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(fileWriter, properties);
mapper.writeValue(outputWriter, properties);
} else {
log("Writing properties file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
properties.store(fileWriter, "Generated by Git-Commit-Id-Plugin");
properties.store(outputWriter, "Generated by Git-Commit-Id-Plugin");
}

} catch (IOException ex) {
throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex);
} finally {
Closeables.closeQuietly(fileWriter);
Closeables.closeQuietly(outputWriter);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public enum AvailableGitTestRepo {
*/
WITH_LIGHTWEIGHT_TAG_BEFORE_ANNOTATED_TAG("src/test/resources/_git_lightweight_tag_before_annotated_tag"),
WITH_TAG_ON_DIFFERENT_BRANCH("src/test/resources/_git_with_tag_on_different_branch"),

WITH_ONE_COMMIT_WITH_SPECIAL_CHARACTERS("src/test/resources/_git_one_commit_with_umlaut"),
MAVEN_GIT_COMMIT_ID_PLUGIN(".git");

private String dir;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void shouldGenerateCustomPropertiesFileJson(boolean useNativeGit) throws
// given
mavenSandbox.withParentProject("my-pom-project", "pom")
.withChildProject("my-jar-module", "jar")
.withGitRepoInChild(AvailableGitTestRepo.GIT_COMMIT_ID)
.withGitRepoInChild(AvailableGitTestRepo.WITH_ONE_COMMIT_WITH_SPECIAL_CHARACTERS)
.create(CleanUp.CLEANUP_FIRST);

MavenProject targetProject = mavenSandbox.getChildProject();
Expand All @@ -224,17 +224,16 @@ public void shouldGenerateCustomPropertiesFileJson(boolean useNativeGit) throws
alterMojoSettings("generateGitPropertiesFilename", targetFilePath);
alterMojoSettings("format", "json");
alterMojoSettings("useNativeGit", useNativeGit);

// when
try {
mojo.execute();

// then
assertThat(expectedFile).exists();
String json = Files.toString(expectedFile, Charset.defaultCharset());
String json = Files.toString(expectedFile, Charset.forName("UTF-8"));
ObjectMapper om = new ObjectMapper();
Map<String, String> map = new HashMap<String, String>();
map = om.readValue(expectedFile, map.getClass());
map = om.readValue(json, map.getClass());
assertThat(map.size() > 10);
} finally {
FileUtils.forceDelete(expectedFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
initial commit on test project
1 change: 1 addition & 0 deletions src/test/resources/_git_one_commit_with_umlaut/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
1 change: 1 addition & 0 deletions src/test/resources/_git_one_commit_with_umlaut/ORIG_HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0b0181b49468f45e573eaf3562f163107c5c121d
14 changes: 14 additions & 0 deletions src/test/resources/_git_one_commit_with_umlaut/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
hideDotFiles = dotGitOnly
[user]
name = John Doe
email = [email protected]
[receive]
denynonfastforwards = false
denyCurrentBranch = ignore
1 change: 1 addition & 0 deletions src/test/resources/_git_one_commit_with_umlaut/description
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# An example hook script to check the commit log message taken by
# applypatch from an e-mail message.
#
# The hook should exit with non-zero status after issuing an
# appropriate message if it wants to stop the commit. The hook is
# allowed to edit the commit message file.
#
# To enable this hook, rename this file to "applypatch-msg".

. git-sh-setup
test -x "$GIT_DIR/hooks/commit-msg" &&
exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"}
:
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.
#
# To enable this hook, rename this file to "commit-msg".

# Uncomment the below to add a Signed-off-by line to the message.
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
# hook is more suited to it.
#
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"

# This example catches duplicate Signed-off-by lines.

test "" = "$(grep '^Signed-off-by: ' "$1" |
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
echo >&2 Duplicate Signed-off-by lines.
exit 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
#
# An example hook script that is called after a successful
# commit is made.
#
# To enable this hook, rename this file to "post-commit".

: Nothing
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# An example hook script for the "post-receive" event.
#
# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated. It is passed arguments in through
# stdin in the form
# <oldrev> <newrev> <refname>
# For example:
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
#
# see contrib/hooks/ for a sample, or uncomment the next line and
# rename the file to "post-receive".

#. /usr/share/doc/git-core/contrib/hooks/post-receive-email
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".

exec git update-server-info
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
#
# An example hook script to verify what is about to be committed
# by applypatch from an e-mail message.
#
# The hook should exit with non-zero status after issuing an
# appropriate message if it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-applypatch".

. git-sh-setup
test -x "$GIT_DIR/hooks/pre-commit" &&
exec "$GIT_DIR/hooks/pre-commit" ${1+"$@"}
:
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# If you want to allow non-ascii filenames set this variable to true.
allownonascii=$(git config hooks.allownonascii)

# Redirect output to stderr.
exec 1>&2

# Cross platform projects tend to avoid non-ascii filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
# Note that the use of brackets around a tr range is ok here, (it's
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
test $(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
echo "Error: Attempt to add a non-ascii file name."
echo
echo "This can cause problems if you want to work"
echo "with people on other platforms."
echo
echo "To be portable it is advisable to rename the file ..."
echo
echo "If you know what you are doing you can disable this"
echo "check using:"
echo
echo " git config hooks.allownonascii true"
echo
exit 1
fi

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --
Loading

0 comments on commit fba077c

Please sign in to comment.