diff --git a/src/prepare-commit-msg-context/prepare-commit-msg-diff b/src/prepare-commit-msg-context/prepare-commit-msg-diff index f69128f..11aa8cf 100644 --- a/src/prepare-commit-msg-context/prepare-commit-msg-diff +++ b/src/prepare-commit-msg-context/prepare-commit-msg-diff @@ -6,9 +6,16 @@ COMMIT_MSG_FILE="${1}" COMMIT_SOURCE="${2}" SHA1="${3}" +print_context() { + echo "" >> "${COMMIT_MSG_FILE}" + git diff --cached | sed 's/^/# /' >> "${COMMIT_MSG_FILE}" +} + + case "${COMMIT_SOURCE},${SHA1}" in ",") - echo "" >> "${COMMIT_MSG_FILE}" - git diff --cached | sed 's/^/# /' >> "${COMMIT_MSG_FILE}" ;; + print_context;; + "template,") + print_context;; *) ;; esac diff --git a/src/prepare-commit-msg-context/prepare-commit-msg-diff-history b/src/prepare-commit-msg-context/prepare-commit-msg-diff-history index 1e03526..0ce4a37 100644 --- a/src/prepare-commit-msg-context/prepare-commit-msg-diff-history +++ b/src/prepare-commit-msg-context/prepare-commit-msg-diff-history @@ -6,13 +6,20 @@ COMMIT_MSG_FILE="${1}" COMMIT_SOURCE="${2}" SHA1="${3}" +print_context() { + echo "" >> "${COMMIT_MSG_FILE}" + git diff --cached | sed 's/^/# /' >> "${COMMIT_MSG_FILE}" + + echo "" >> "${COMMIT_MSG_FILE}" + echo "# Recent History" >> "${COMMIT_MSG_FILE}" + git log --oneline --graph --decorate -n 10 | sed 's/^/# /' >> "${COMMIT_MSG_FILE}" +} + + case "${COMMIT_SOURCE},${SHA1}" in ",") - echo "" >> "${COMMIT_MSG_FILE}" - git diff --cached | sed 's/^/# /' >> "${COMMIT_MSG_FILE}" - - echo "" >> "${COMMIT_MSG_FILE}" - echo "# Recent History" >> "${COMMIT_MSG_FILE}" - git log --oneline --graph --decorate -n 10 | sed 's/^/# /' >> "${COMMIT_MSG_FILE}" ;; + print_context;; + "template,") + print_context;; *) ;; esac diff --git a/test/prepare-commit-msg-context/basic.sh b/test/prepare-commit-msg-context/basic.sh index 71588fa..51f246b 100644 --- a/test/prepare-commit-msg-context/basic.sh +++ b/test/prepare-commit-msg-context/basic.sh @@ -12,9 +12,13 @@ check "Check prepare-commit-msg is installed" test -x /usr/local/share/prepare-c # Check for working features TEMP_FILE=$(mktemp) -"$(dirname "${0}")/mock-repo.sh" /usr/local/share/prepare-commit-msg-context/prepare-commit-msg "${TEMP_FILE}" +"$(dirname "${0}")/mock-repo.sh" /usr/local/share/prepare-commit-msg-context/prepare-commit-msg "${TEMP_FILE}" "" check "Check prepare-commit-msg-context is working" diff <(sed -e 's/.\+ Initial commit/---/' < "${TEMP_FILE}") expected.txt +TEMP_FILE=$(mktemp) +"$(dirname "${0}")/mock-repo.sh" /usr/local/share/prepare-commit-msg-context/prepare-commit-msg "${TEMP_FILE}" template +check "Check prepare-commit-msg-context is working(template)" diff <(sed -e 's/.\+ Initial commit/---/' < "${TEMP_FILE}") expected.txt + # Report result # If any of the checks above exited with a non-zero exit code, the test will fail. reportResults diff --git a/test/prepare-commit-msg-context/format-diff.sh b/test/prepare-commit-msg-context/format-diff.sh index 2116879..ba9ec59 100644 --- a/test/prepare-commit-msg-context/format-diff.sh +++ b/test/prepare-commit-msg-context/format-diff.sh @@ -12,9 +12,13 @@ check "Check prepare-commit-msg is installed" test -x /usr/local/share/prepare-c # Check for working features TEMP_FILE=$(mktemp) -"$(dirname "${0}")/mock-repo.sh" /usr/local/share/prepare-commit-msg-context/prepare-commit-msg "${TEMP_FILE}" +"$(dirname "${0}")/mock-repo.sh" /usr/local/share/prepare-commit-msg-context/prepare-commit-msg "${TEMP_FILE}" "" check "Check prepare-commit-msg-context is working" diff "${TEMP_FILE}" expected-diff.txt +TEMP_FILE=$(mktemp) +"$(dirname "${0}")/mock-repo.sh" /usr/local/share/prepare-commit-msg-context/prepare-commit-msg "${TEMP_FILE}" template +check "Check prepare-commit-msg-context is working(template)" diff "${TEMP_FILE}" expected-diff.txt + # Report result # If any of the checks above exited with a non-zero exit code, the test will fail. reportResults diff --git a/test/prepare-commit-msg-context/mock-repo.sh b/test/prepare-commit-msg-context/mock-repo.sh index a52ba1b..49e74de 100644 --- a/test/prepare-commit-msg-context/mock-repo.sh +++ b/test/prepare-commit-msg-context/mock-repo.sh @@ -4,6 +4,7 @@ set -e PREPAIRE_COMMIT_MSG="${1}" TEMP_FILE="${2}" +COMMIT_SOURCE="${3}" # Setup git config git config --global user.email "you@example.com" @@ -18,5 +19,5 @@ git commit --allow-empty -m "Initial commit" echo "test" > test.txt git add test.txt # Run prepare-commit-msg -${PREPAIRE_COMMIT_MSG} "${TEMP_FILE}" +${PREPAIRE_COMMIT_MSG} "${TEMP_FILE}" "${COMMIT_SOURCE}" popd \ No newline at end of file