git-mob-tool 1.5.4

A CLI app which can help users automatically add co-author(s) to git commits for pair/mob programming
Documentation
#!/bin/sh

set -e

main() {
  # Do nothing during rebase
  [ -z "$(git branch --show-current)" ] && exit 0

  # Do nothing during amend (without message) or reuse of a commit message
  [ "$2" = 'commit' ] && exit 0

  add_jira_issue_id_prefix "$@"
  add_co_authored_by_trailers "$@"
}

add_co_authored_by_trailers() {
  # Uses https://github.com/Mubashwer/git-mob
  trailers=$(git mob --list | sed "s/^/Co-authored-by: /")
  [ -n "$trailers" ] || return 0

  printf "%s\n" "$trailers" |
    sed "s/^/--trailer\n/" |
    tr '\n' '\0' |
    xargs -0 git interpret-trailers --if-exists addIfDifferent --in-place "$1"

  printf "%s\n\n" "$trailers"
}

add_jira_issue_id_prefix() {
  # If the branch name starts with string resembling a Jira Issue ID, fetch it
  jira_issue_id=$(git branch --show-current | grep -o -E "^[a-zA-Z]+-[0-9]+" | tr '[:lower:]' '[:upper:]')
  commit_msg_file_text=$(cat "$1")
  commit_msg=$(echo "$commit_msg_file_text" | grep -v "^[[:space:]]*#" || true)

  # If the Jira Issue ID is identified and the commit message does not already start with it
  # then prepend the commit message with it
  if [ -n "$jira_issue_id" ] && echo "$commit_msg" | grep -q -i -v "^\[\?$jira_issue_id\]\?"; then
    printf "[%s] %s\n" "$jira_issue_id" "$commit_msg_file_text" > "$1"
  fi
}

main "$@"