github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/githooks/prepare-commit-msg (about)

     1  #!/usr/bin/env bash
     2  #
     3  # Prepare the commit message by adding a release note.
     4  
     5  require_justification=0
     6  set -euo pipefail
     7  
     8  if [[ "${2-}" = "message" ]]; then
     9    # This git command is non-interactive so it will not filter out any comments
    10    # we add. There is nothing more for us to do.
    11    exit 0
    12  fi
    13  
    14  give_up() {
    15    echo "prepare-commit-msg: $@" >&2
    16    exit 0  # exit with successful status to allow the commit to proceed
    17  }
    18  
    19  if ! test -e "$1" -o -z "$1"; then
    20  	give_up "$1: commit message file does not exist or is empty"
    21  fi
    22  
    23  # Git can be configured to use any character as the comment indicator. See the
    24  # core.commentChar Git option. We can deduce what comment character is in effect
    25  # by looking for text that we know will be preceded by the comment character.
    26  if ! cchar=$(grep "^. Please enter the commit message for your changes." "$1" | head -c1); then
    27    give_up "unable to determine comment char"
    28  fi
    29  
    30  if ! tempfile=$(mktemp); then
    31    give_up "failed to create temporary file"
    32  fi
    33  trap "rm -f $tempfile" EXIT
    34  
    35  sed_script=''
    36  
    37  # If the setting 'cockroachdb.disable-commit-template' is set and
    38  # true, all the template recommendations will be commented out as
    39  # opposed to be filled in.
    40  tchar=''
    41  tdisable=$(git config --bool --get cockroachdb.disable-commit-template || echo false)
    42  if test x"$tdisable" != xfalse; then
    43  	tchar="$cchar "
    44  fi
    45  
    46  if [ "$require_justification" = 1 ]; then
    47    # Add an explicit "Release justification: None" if no release justification was specified.
    48    if ! grep -q '^Release justification' "$1"; then
    49    	sed_script+="/$cchar Please enter the commit message for your changes./i\\
    50  \\
    51  ${tchar}Release justification:
    52  ;
    53  "
    54    fi
    55  fi
    56  
    57  # Add a commit message template if the commit message is empty.
    58  if ! grep -q -E -v "^$cchar|^$" "$1"; then
    59  	sed_script+="/^$/i\\
    60  ${tchar}<pkg>: <short description - lowercase, no final period>\\
    61  \\
    62  ${tchar}<what was there before: Previously, ...>\\
    63  ${tchar}<why it needed to change: This was inadequate because ...>\\
    64  ${tchar}<what you did about it: To address this, this patch ...>
    65  ;
    66  "
    67  	if test x"$tchar" = x; then
    68  		sed_script+="/^$/i\\
    69  $cchar Note: to disable this commit template, run: git config --global --add cockroachdb.disable-commit-template true
    70  ;
    71  "
    72  	fi
    73  fi
    74  
    75  # Add an explicit "Release note: None" if no release note was specified.
    76  if ! grep -q '^Release note' "$1"; then
    77  	sed_script+="/$cchar Please enter the commit message for your changes./i\\
    78  $cchar Use 'Release note: None' if there is no user-visible change.\\
    79  ${tchar}Release note (<category, see below>): <what> <show> <why>\\
    80  
    81  ;
    82  "
    83  fi
    84  
    85  # Inject commit message recommendations into the commit message help text.
    86  sed_script+="/$cchar.*an empty message aborts the commit./a\\
    87  $cchar\\
    88  $cchar Commit message recommendation:\\
    89  $cchar\\
    90  $cchar     ---\\
    91  $cchar     <pkg>: <short description>\\
    92  "
    93  
    94  if [ "$require_justification" = 1 ]; then
    95    sed_script+="$cchar\\
    96  $cchar     Release justification: <release justification>\\
    97  "
    98  fi
    99  sed_script+="$cchar\\
   100  $cchar     <what was there before: Previously, ...>\\
   101  $cchar     <why it needed to change: This was inadequate because ...>\\
   102  $cchar     <what you did about it: To address this, this patch ...>\\
   103  $cchar\\
   104  $cchar     Release note (<category>): <what> <show> <why>\\
   105  $cchar     ---\\
   106  $cchar\\
   107  $cchar Wrap long lines! 72 columns is best.\\
   108  $cchar See also: https://wiki.crdb.io/wiki/spaces/CRDB/pages/73072807/Git+Commit+Messages\\
   109  $cchar\\
   110  "
   111  
   112  if [ "$require_justification" = 1 ]; then
   113    sed_script+="$cchar\\
   114  $cchar Categories for release justification:\\
   115  $cchar     - non-production code changes\\
   116  $cchar     - bug fixes and low-risk updates to new functionality\\
   117  $cchar     - fixes for high-priority or high-severity bugs in existing functionality\\
   118  $cchar     - low risk, high benefit changes to existing functionality\\
   119  $cchar\\
   120  "
   121  fi
   122  
   123  sed_script+="$cchar The release note must be present if your commit has user-facing\\
   124  $cchar or backward-incompatible changes. Use 'Release note: None' otherwise.\\
   125  $cchar\\
   126  $cchar Things to keep in mind for release notes:\\
   127  $cchar   - past tense (this was changed...) or present tense (now possible to...)\\
   128  $cchar   - what has changed: narrow down the product area / feature\\
   129  $cchar     Note: for bug fixes, indicate since when the bug was present\\
   130  $cchar   - show what changed: how a user can see the change for themselves\\
   131  $cchar     Note: for bug fixes, show the symptom(s) to recognize the bug\\
   132  $cchar   - why the change: who does this impact, how and why should they care\\
   133  $cchar\\
   134  $cchar See also: https://wiki.crdb.io/wiki/spaces/CRDB/pages/73072807/Release+notes\\
   135  $cchar\\
   136  $cchar Example release notes:\\
   137  $cchar\\
   138  $cchar   Release note (sql change): The IMPLEMENT statement was extended\\
   139  $cchar   to support the new STEP clause. This can be used to\\
   140  $cchar   implement more gradually, as often required by teams of two\\
   141  $cchar   or more.\\
   142  $cchar\\
   143  $cchar   Release note (bug fix): The system.replication_stats report no longer\\
   144  $cchar   erroneously considers some ranges belonging to table partitions to be\\
   145  $cchar   over-replicated. This bug was present since version 19.2.\\
   146  $cchar\\
   147  $cchar Categories for release notes:\\
   148  $cchar   - cli change\\
   149  $cchar   - sql change\\
   150  $cchar   - admin ui change\\
   151  $cchar   - security update\\
   152  $cchar   - general change (e.g., change of required Go version)\\
   153  $cchar   - build change (e.g., compatibility with older CPUs)\\
   154  $cchar   - enterprise change (e.g., change to backup/restore)\\
   155  $cchar   - backwards-incompatible change\\
   156  $cchar   - performance improvement\\
   157  $cchar   - bug fix
   158  ;
   159  "
   160  
   161  if ! sed "$sed_script" "$1" > "$tempfile"; then
   162    give_up "unable to inject commit message recommendations"
   163  fi
   164  
   165  if ! mv "$tempfile" "$1"; then
   166    give_up "failed overwriting commit message file"
   167  fi