cz-cli icon indicating copy to clipboard operation
cz-cli copied to clipboard

Git-generated trailers are not preserved

Open CJKay opened this issue 4 years ago • 2 comments

Commitizen's --hook option truncates the COMMIT_EDITMSG file. This wipes the Signed-off-by trailer generated by Git's -s flag, as well as any other trailers that might have been there before running Commitizen, and forces us to amend our commits after the fact.

We have a temporary workaround in our hook for this, which might offer some insight:

#!/bin/bash

file="$1"
type="$2"

if [ -z "$type" ]; then # only run on new commits
    #
    # Save any commit message trailers generated by Git.
    #

    trailers=$(git interpret-trailers --parse "$file")

    #
    # Execute the Commitizen hook.
    #

    (exec < "/dev/tty" && npx --no-install git-cz --hook) || true

    #
    # Restore any trailers that Commitizen might have overwritten.
    #

    printf "\n" >> "$file"

    while IFS= read -r trailer; do
        git interpret-trailers --in-place --trailer "$trailer" "$file"
    done <<< "$trailers"
fi

Version: [email protected], [email protected]

CJKay avatar Apr 12 '21 18:04 CJKay

can you tell me where I have to put this config file? Thanks

manang avatar Sep 29 '21 14:09 manang

can you tell me where I have to put this config file? Thanks

This needs to be in your prepare-commit-msg Git hook (.git/hooks/prepare-commit-message, or .husky/prepare-commit-message if you use Husky [we do]).

CJKay avatar Sep 30 '21 10:09 CJKay