diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 9a32de6..6b682d4 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -13,8 +13,7 @@ jobs: strategy: matrix: features: - - color - - hello + - prepare-commit-msg-context baseImage: - debian:latest - ubuntu:latest @@ -34,8 +33,7 @@ jobs: strategy: matrix: features: - - color - - hello + - prepare-commit-msg-context steps: - uses: actions/checkout@v3 diff --git a/src/prepare-commit-msg-context/devcontainer-feature.json b/src/prepare-commit-msg-context/devcontainer-feature.json new file mode 100644 index 0000000..8e462af --- /dev/null +++ b/src/prepare-commit-msg-context/devcontainer-feature.json @@ -0,0 +1,8 @@ +{ + "name": "prepare-commit-msg-context", + "id": "prepare-commit-msg-context", + "version": "1.0.0", + "description": "Install a prepare-commit-msg hook script to /usr/local/share(the script writes the commit content as a comment).", + "options": {}, + "installsAfter": [] +} diff --git a/src/prepare-commit-msg-context/install.sh b/src/prepare-commit-msg-context/install.sh new file mode 100644 index 0000000..877445a --- /dev/null +++ b/src/prepare-commit-msg-context/install.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env sh + +INSTALL_DIR="/usr/local/share/prepare-commit-msg-context" + +set -e + +test -d "${INSTALL_DIR}" || mkdir -p "${INSTALL_DIR}" +cp "$(dirname "${0}")/prepare-commit-msg" "${INSTALL_DIR}/prepare-commit-msg" +chmod a+x "${INSTALL_DIR}/prepare-commit-msg" diff --git a/src/prepare-commit-msg-context/prepare-commit-msg b/src/prepare-commit-msg-context/prepare-commit-msg new file mode 100644 index 0000000..b8c9c37 --- /dev/null +++ b/src/prepare-commit-msg-context/prepare-commit-msg @@ -0,0 +1,18 @@ +#!/bin/sh + +set -e + +COMMIT_MSG_FILE="${1}" +COMMIT_SOURCE=$2 +SHA1=$3 + +echo "" >> "${COMMIT_MSG_FILE}" +git diff --cached | sed 's/^/# /' >> "${COMMIT_MSG_FILE}" + +case "$COMMIT_SOURCE,$SHA1" in + ,|template,) + echo "" >> "${COMMIT_MSG_FILE}" + echo "# Recent History" >> "${COMMIT_MSG_FILE}" + git log --oneline --graph --decorate -n 10 | sed 's/^/# /' >> "${COMMIT_MSG_FILE}" ;; + *) ;; +esac \ No newline at end of file diff --git a/test/_global/scenarios.json b/test/_global/scenarios.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/_global/scenarios.json @@ -0,0 +1 @@ +{} diff --git a/test/prepare-commit-msg-context/basic.sh b/test/prepare-commit-msg-context/basic.sh new file mode 100644 index 0000000..ac56975 --- /dev/null +++ b/test/prepare-commit-msg-context/basic.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +set -e + +# Optional: Import test library bundled with the devcontainer CLI +# Provides the 'check' and 'reportResults' commands. +source dev-container-features-test-lib + +# Check for installed features +check "Check prepare-commit-msg is installed" test -x /usr/local/share/prepare-commit-msg-context/prepare-commit-msg + +# Check for working features +git config --global user.email "you@example.com" +git config --global user.name "Your Name" +git config --global init.defaultBranch main +# Make temp repo +TEMP_FILE=$(mktemp) +TEMP_DIR=$(mktemp -d) +pushd "${TEMP_DIR}" +git init +git commit --allow-empty -m "Initial commit" +echo "test" > test.txt +git add test.txt +# Run prepare-commit-msg +/usr/local/share/prepare-commit-msg-context/prepare-commit-msg "${TEMP_FILE}" +popd +check "Check prepare-commit-msg-context is working" 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/expected.txt b/test/prepare-commit-msg-context/expected.txt new file mode 100644 index 0000000..032867d --- /dev/null +++ b/test/prepare-commit-msg-context/expected.txt @@ -0,0 +1,11 @@ + +# diff --git a/test.txt b/test.txt +# new file mode 100644 +# index 0000000..9daeafb +# --- /dev/null +# +++ b/test.txt +# @@ -0,0 +1 @@ +# +test + +# Recent History +--- diff --git a/test/prepare-commit-msg-context/scenarios.json b/test/prepare-commit-msg-context/scenarios.json new file mode 100644 index 0000000..7cebad1 --- /dev/null +++ b/test/prepare-commit-msg-context/scenarios.json @@ -0,0 +1,15 @@ +{ + "basic": { + "image": "mcr.microsoft.com/devcontainers/base:ubuntu", + "features": { + "ghcr.io/devcontainers/features/common-utils:1": { + "installZsh": false, + "installOhMyZsh": false, + "upgradePackages": false, + "username": "node" + }, + "prepare-commit-msg-context": {} + }, + "remoteUser": "node" + } +} diff --git a/test/prepare-commit-msg-context/test.sh b/test/prepare-commit-msg-context/test.sh new file mode 100644 index 0000000..5d3a1dc --- /dev/null +++ b/test/prepare-commit-msg-context/test.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +set -e + +# Optional: Import test library bundled with the devcontainer CLI +# Provides the 'check' and 'reportResults' commands. +source dev-container-features-test-lib + +# Check for installed features +check "Check prepare-commit-msg is installed" test -x /usr/local/share/prepare-commit-msg-context/prepare-commit-msg + +# Report result +# If any of the checks above exited with a non-zero exit code, the test will fail. +reportResults