From fa15ffa5433098af689496ddae73a496ab644356 Mon Sep 17 00:00:00 2001 From: Andreas Kohn Date: Tue, 21 Jul 2015 01:32:00 +0200 Subject: [PATCH 1/4] Add support for MacOS X (10.10) * xargs -r: GNU extension, reimplemented using a shell function * sed -z: GNU extension, reimplemented by piping through echo (removing the newlines) and tr (adding the ',') * xargs -I{}: reimplemented using a for-loop, to avoid hitting the 255-byte limitation for eacho argument of xargs(1) While there: replace the grep-parsing by a filtering for-loop, which as a side-effect fixes command line arguments with quotes due to the use of eval. --- git-mvn-wrapper.sh | 57 +++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/git-mvn-wrapper.sh b/git-mvn-wrapper.sh index 5e20532..6cb6c33 100755 --- a/git-mvn-wrapper.sh +++ b/git-mvn-wrapper.sh @@ -11,13 +11,44 @@ set -e -if ! echo "$*" | grep -qE '(^|\s)-O(\s|$)'; then +# Replacement for GNU xargs -r +maybe_xargs() { + _in= + while read _line; do + _in="${_line} ${_in}" + done + if [ -n "${_in}" ]; then + "$@" ${_in} + fi +} + +find_module() { + NDIR=$(dirname "$1") + DIR="." + while [ ! "$NDIR" = "$DIR" ] && [ ! -f "$NDIR/pom.xml" ]; do + DIR="$NDIR" + NDIR=$(dirname "$DIR") + done + echo "$NDIR" +} + +real_args= +optimize= +while [ $# -gt 0 ]; do + if [ "$1" = "-O" ]; then + optimize=true + else + real_args="${real_args} \"$1\"" + fi + shift +done + +if [ -z "${optimize}" ]; then # Regular Maven execution requested. Pass on all arguments as-is. - mvn "$@" + eval mvn ${real_args} else # Optimized Maven execution requested. Drop the custom flag. - ARGS=$(echo "$*" | sed -r 's,(^|\s)-O(\s|$),\1\2,') - set -- $ARGS + set -- ${real_args} if ! git rev-parse --is-inside-git-dir > /dev/null 2>&1; then # This is not a Git repository; can't optimize the build. @@ -29,18 +60,12 @@ else # module to which the file belongs. Remove duplicates and join the # remaining module directory names with commas. Lastly, instruct Maven to # only build modified modules and their dependents. - git ls-files --modified \ - | xargs -I{} sh -c ' - NDIR=$(dirname "{}") - DIR="." - while [ ! "$NDIR" = "$DIR" ] && [ ! -f "$NDIR/pom.xml" ]; do - DIR="$NDIR" - NDIR=$(dirname "$DIR") - done - echo "$NDIR" - ' \ + for _file in `git ls-files --modified`; do + find_module "${_file}" + done \ | sort -u \ - | sed -z 's/\n/,/g' \ - | xargs -r mvn $@ -amd -pl + | xargs echo \ + | tr ' ' ',' \ + | eval maybe_xargs mvn $@ -amd -pl fi fi From cb33345aecad8ef832e01c6e92e23ac6ffd0e457 Mon Sep 17 00:00:00 2001 From: Andreas Kohn Date: Tue, 21 Jul 2015 10:13:07 +0200 Subject: [PATCH 2/4] Skip empty lines --- git-mvn-wrapper.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/git-mvn-wrapper.sh b/git-mvn-wrapper.sh index 6cb6c33..f0c6999 100755 --- a/git-mvn-wrapper.sh +++ b/git-mvn-wrapper.sh @@ -15,6 +15,9 @@ set -e maybe_xargs() { _in= while read _line; do + if [ -z "${_line}" ]; then + continue + fi _in="${_line} ${_in}" done if [ -n "${_in}" ]; then From bcb4563492eebafdb60cc75d33c3b06fad6de983 Mon Sep 17 00:00:00 2001 From: Andreas Kohn Date: Tue, 21 Jul 2015 10:13:58 +0200 Subject: [PATCH 3/4] Avoid a sub-shell: we can just process the output with read This should improve the speed a bit, and also avoids issues with files-with-spaces --- git-mvn-wrapper.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-mvn-wrapper.sh b/git-mvn-wrapper.sh index f0c6999..dac50d0 100755 --- a/git-mvn-wrapper.sh +++ b/git-mvn-wrapper.sh @@ -63,7 +63,7 @@ else # module to which the file belongs. Remove duplicates and join the # remaining module directory names with commas. Lastly, instruct Maven to # only build modified modules and their dependents. - for _file in `git ls-files --modified`; do + git ls-files --modified | while read _file; do find_module "${_file}" done \ | sort -u \ From a6335d2f31158cb7e73b02cc10f9d209e84c1409 Mon Sep 17 00:00:00 2001 From: Andreas Kohn Date: Tue, 21 Jul 2015 10:15:10 +0200 Subject: [PATCH 4/4] Avoid one eval: just use "$@" at the right point --- git-mvn-wrapper.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-mvn-wrapper.sh b/git-mvn-wrapper.sh index dac50d0..8af7779 100755 --- a/git-mvn-wrapper.sh +++ b/git-mvn-wrapper.sh @@ -69,6 +69,6 @@ else | sort -u \ | xargs echo \ | tr ' ' ',' \ - | eval maybe_xargs mvn $@ -amd -pl + | maybe_xargs mvn "$@" -amd -pl fi fi