Search code examples
pythonshellgerrit

Gerrit query option to print just the first line


This script works:

!#/bin/bash
gerrit_query_output=$(ssh -p 29418 my_review-server.com gerrit query --format=JSON --commit-message change:66664 | head -n 1)
commit_message=$(echo $gerrit_query_output | python -c "import sys, json; print json.load(sys.stdin)['commitMessage']" | grep -v ^Change-Id:)

echo "commit msg= $commit_message"
  1. Is there an option in gerrit query command to print only first line so I can avoid | head -n 1 in line 2? json parsing in python fails if gerrit_query_output gets {"type":"stats","rowCount":1,"runTimeMilliseconds":9,"moreChanges":false} as second line.

  2. Is there a better way to get commitMessage without Change-Id: line, so I can avoid | grep -v ^Change-Id:


Solution

  • 1) Use "jq" instead of phthon to get the commit message:

    ssh -p 29418 GERRIT-SERVER gerrit query --format=JSON --commit-message change:CHANGE-NUMBER | jq -r --raw-output '.commitMessage // empty'
    

    2) There's no way to do that.