commit-msg 808 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/bin/sh
  2. MSG="$1"
  3. # Check for, and add if missing, a unique Change-Id
  4. #
  5. add_ChangeId() {
  6. if grep '^Change-Id: ' "$MSG" >/dev/null
  7. then
  8. return
  9. fi
  10. id=$(_gen_ChangeId)
  11. out="$MSG.new"
  12. ftt="$MSG.footers"
  13. sed -e '/^[A-Za-z][A-Za-z0-9-]*: /,$d' <"$MSG" >"$out"
  14. sed -ne '/^[A-Za-z][A-Za-z0-9-]*: /,$p' <"$MSG" >"$ftt"
  15. if ! [ -s "$ftt" ]
  16. then
  17. echo >>"$out"
  18. fi
  19. echo "Change-Id: I$id" >>"$out"
  20. cat "$ftt" >>"$out"
  21. mv -f "$out" "$MSG"
  22. rm -f "$out" "$ftt"
  23. }
  24. _gen_ChangeIdInput() {
  25. echo "tree $(git write-tree)"
  26. if parent=$(git rev-parse HEAD^0 2>/dev/null)
  27. then
  28. echo "parent $parent"
  29. fi
  30. echo "author $(git var GIT_AUTHOR_IDENT)"
  31. echo "committer $(git var GIT_COMMITTER_IDENT)"
  32. echo
  33. cat "$MSG"
  34. }
  35. _gen_ChangeId() {
  36. _gen_ChangeIdInput |
  37. git hash-object -t commit --stdin
  38. }
  39. add_ChangeId