|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +safeRunCommand() { |
| 4 | + typeset cmnd="$*" |
| 5 | + typeset ret_code |
| 6 | + |
| 7 | + eval $cmnd |
| 8 | + ret_code=$? |
| 9 | + if [ $ret_code != 0 ]; then |
| 10 | + printf "Error : [%d] when executing command: '$cmnd'" $ret_code |
| 11 | + exit $ret_code |
| 12 | + fi |
| 13 | +} |
| 14 | + |
| 15 | +function get_module() { |
| 16 | + local path=$1; |
| 17 | + while true; do |
| 18 | + path=$(dirname $path); |
| 19 | + if [ -f "$path/pom.xml" ]; then |
| 20 | + echo "$path"; |
| 21 | + return; |
| 22 | + elif [[ "./" =~ "$path" ]]; then |
| 23 | + return; |
| 24 | + fi |
| 25 | + done |
| 26 | +} |
| 27 | + |
| 28 | +commits=(); |
| 29 | +affectedFiles=(); |
| 30 | +moduleFolders=(); |
| 31 | + |
| 32 | +remote="$1" |
| 33 | +url="$2" |
| 34 | + |
| 35 | +z40=0000000000000000000000000000000000000000 |
| 36 | + |
| 37 | +while read local_ref local_sha remote_ref remote_sha |
| 38 | +do |
| 39 | + if [ "$remote_sha" = $z40 ] |
| 40 | + then |
| 41 | + # New branch, examine all commits |
| 42 | + range="$local_sha" |
| 43 | + else |
| 44 | + # Update to existing branch, examine new commits |
| 45 | + range="$remote_sha..$local_sha" |
| 46 | + fi |
| 47 | + |
| 48 | + commits=( $(git rev-list "$range") ) |
| 49 | + |
| 50 | + for commit in ${commits[@]} |
| 51 | + do |
| 52 | + affectedFiles+=( $(git diff-tree --no-commit-id --name-only -r $commit) ) |
| 53 | + done |
| 54 | + |
| 55 | + if [ ${#affectedFiles[@]} -eq 0 ]; then |
| 56 | + exit 0; |
| 57 | + fi |
| 58 | + |
| 59 | + #delete duplicates |
| 60 | + affectedFiles=($(echo "${affectedFiles[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) |
| 61 | + |
| 62 | + for file in ${affectedFiles[@]} |
| 63 | + do |
| 64 | + moduleFolders+=($(get_module "$file")); |
| 65 | +# moduleFolders+=($(get_module "$file" | cut -d "/" -f2-)); // or this, if java project doesn't equal git-root folder |
| 66 | + done |
| 67 | + |
| 68 | + if [ ${#moduleFolders[@]} -eq 0 ]; then |
| 69 | + exit 0; |
| 70 | + fi |
| 71 | + |
| 72 | + modules_arg=$(printf ",%s" "${moduleFolders[@]}"); |
| 73 | + modules_arg=${modules_arg:1}; |
| 74 | + |
| 75 | + command="mvn clean -pl $modules_arg" |
| 76 | + safeRunCommand "$command" |
| 77 | + |
| 78 | + command="mvn test -pl $modules_arg -amd" |
| 79 | + safeRunCommand "$command" |
| 80 | + |
| 81 | + exit 0 |
| 82 | +done |
0 commit comments