osmith has submitted this change. ( https://gerrit.osmocom.org/c/osmo-ci/+/39795?usp=email )
Change subject: lint/lint_diff: make use of shell functions ......................................................................
lint/lint_diff: make use of shell functions
Prepare to add more logic to this file by first moving existing code blocks into functions.
Change-Id: If884f51f20e794397ee0fecff0cf1f958957da05 --- M lint/lint_diff.sh 1 file changed, 77 insertions(+), 60 deletions(-)
Approvals: Jenkins Builder: Verified osmith: Looks good to me, approved laforge: Looks good to me, but someone else must approve pespin: Looks good to me, but someone else must approve
diff --git a/lint/lint_diff.sh b/lint/lint_diff.sh index 5ec26a5..9458ca7 100755 --- a/lint/lint_diff.sh +++ b/lint/lint_diff.sh @@ -3,77 +3,94 @@ COMMIT="$1" GIT_DIR="$(git rev-parse --show-toplevel 2>/dev/null || true)" SCRIPT_DIR="$(dirname "$(realpath "$0")")" - -if [ -z "$GIT_DIR" ]; then - echo "ERROR: path is not a git repository: $PWD" - exit 1 -fi - -if [ -z "$COMMIT" ]; then - # Clean worktree: diff last commit against the one before - COMMIT="HEAD~1" - - if [ -n "$(git status --porcelain)" ]; then - # Dirty worktree: diff uncommitted changes against last commit - COMMIT="HEAD" - fi -fi - ERROR=0
-echo "Running docker_run_rm.sh on the whole tree..." -echo -if ! "$SCRIPT_DIR"/docker_run_rm.sh; then - ERROR=1 -fi +check_git_dir() { + if [ -z "$GIT_DIR" ]; then + echo "ERROR: path is not a git repository: $PWD" + exit 1 + fi +}
-echo "Running checkpatch on 'git diff $COMMIT'..." -echo -if ! git diff -U0 "$COMMIT" | "$SCRIPT_DIR/checkpatch/checkpatch_osmo.sh" - \ - --color=always \ - --mailback \ - --show-types \ - --showfile \ - --terse -then - ERROR=1 -fi +set_commit() { + if [ -z "$COMMIT" ]; then + # Clean worktree: diff last commit against the one before + COMMIT="HEAD~1"
+ if [ -n "$(git status --porcelain)" ]; then + # Dirty worktree: diff uncommitted changes against last commit + COMMIT="HEAD" + fi + fi +}
-if [ "$ERROR" = 1 ]; then +test_docker_run_rm() { + echo "Running docker_run_rm.sh on the whole tree..." + echo + if ! "$SCRIPT_DIR"/docker_run_rm.sh; then + ERROR=1 + fi +} + +test_checkpatch() { + echo "Running checkpatch on 'git diff $COMMIT'..." + echo + if ! git diff -U0 "$COMMIT" | "$SCRIPT_DIR/checkpatch/checkpatch_osmo.sh" - \ + --color=always \ + --mailback \ + --show-types \ + --showfile \ + --terse + then + ERROR=1 + fi +} + +show_error() { echo echo "Please fix the linting errors above. More information:" echo "https://osmocom.org/projects/cellular-infrastructure/wiki/Linting" echo +}
+send_review_comments() { + echo "Leaving review comments in gerrit..." + set -x + + # Run again, but in the proper format for checkpatch_json.py + # and store the output in a file + git diff -U0 "$COMMIT" | "$SCRIPT_DIR/checkpatch/checkpatch_osmo.sh" \ + > ../checkpatch_output || true + cd .. + # Convert to gerrit review format + "$SCRIPT_DIR/checkpatch/checkpatch_json.py" \ + checkpatch_output \ + gerrit_report.json \ + "$BUILD_TAG" \ + "$BUILD_URL" + # Apply as review in gerrit + ssh \ + -o UserKnownHostsFile=$SCRIPT_DIR/../contrib/known_hosts \ + -p "$GERRIT_PORT" \ + -l jenkins \ + "$GERRIT_HOST" \ + gerrit \ + review \ + --project "$GERRIT_PROJECT" \ + "$GERRIT_CHANGE_NUMBER,$GERRIT_PATCHSET_NUMBER" \ + --json \ + < gerrit_report.json +} + +check_git_dir +set_commit +test_docker_run_rm +test_checkpatch + +if [ "$ERROR" = 1 ]; then + show_error if [ -n "$JENKINS_HOME" ]; then - echo "Leaving review comments in gerrit..." - set -x - - # Run again, but in the proper format for checkpatch_json.py - # and store the output in a file - git diff -U0 "$COMMIT" | "$SCRIPT_DIR/checkpatch/checkpatch_osmo.sh" \ - > ../checkpatch_output || true - cd .. - # Convert to gerrit review format - "$SCRIPT_DIR/checkpatch/checkpatch_json.py" \ - checkpatch_output \ - gerrit_report.json \ - "$BUILD_TAG" \ - "$BUILD_URL" - # Apply as review in gerrit - ssh \ - -o UserKnownHostsFile=$SCRIPT_DIR/../contrib/known_hosts \ - -p "$GERRIT_PORT" \ - -l jenkins \ - "$GERRIT_HOST" \ - gerrit \ - review \ - --project "$GERRIT_PROJECT" \ - "$GERRIT_CHANGE_NUMBER,$GERRIT_PATCHSET_NUMBER" \ - --json \ - < gerrit_report.json + send_review_comments fi - exit 1 fi