Skip to content

Commit

Permalink
fix: Accept "template" source
Browse files Browse the repository at this point in the history
  • Loading branch information
hankei6km committed Dec 26, 2023
1 parent a675a25 commit f8f6a56
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
11 changes: 9 additions & 2 deletions src/prepare-commit-msg-context/prepare-commit-msg-diff
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 13 additions & 6 deletions src/prepare-commit-msg-context/prepare-commit-msg-diff-history
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion test/prepare-commit-msg-context/basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion test/prepare-commit-msg-context/format-diff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion test/prepare-commit-msg-context/mock-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

0 comments on commit f8f6a56

Please sign in to comment.