diff --git a/git-mvn-wrapper.sh b/git-mvn-wrapper.sh index 5e20532..8af7779 100755 --- a/git-mvn-wrapper.sh +++ b/git-mvn-wrapper.sh @@ -11,13 +11,47 @@ set -e -if ! echo "$*" | grep -qE '(^|\s)-O(\s|$)'; then +# Replacement for GNU xargs -r +maybe_xargs() { + _in= + while read _line; do + if [ -z "${_line}" ]; then + continue + fi + _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 +63,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" - ' \ + git ls-files --modified | while read _file; do + find_module "${_file}" + done \ | sort -u \ - | sed -z 's/\n/,/g' \ - | xargs -r mvn $@ -amd -pl + | xargs echo \ + | tr ' ' ',' \ + | maybe_xargs mvn "$@" -amd -pl fi fi