Skip to content

Commit

Permalink
feat: Implement prepare-commit-msg-context feature
Browse files Browse the repository at this point in the history
  • Loading branch information
hankei6km committed Dec 16, 2023
1 parent 55e3086 commit b16a1a7
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 4 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ jobs:
strategy:
matrix:
features:
- color
- hello
- prepare-commit-msg-context
baseImage:
- debian:latest
- ubuntu:latest
Expand All @@ -34,8 +33,7 @@ jobs:
strategy:
matrix:
features:
- color
- hello
- prepare-commit-msg-context
steps:
- uses: actions/checkout@v3

Expand Down
8 changes: 8 additions & 0 deletions src/prepare-commit-msg-context/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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": []
}
9 changes: 9 additions & 0 deletions src/prepare-commit-msg-context/install.sh
Original file line number Diff line number Diff line change
@@ -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"
18 changes: 18 additions & 0 deletions src/prepare-commit-msg-context/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/_global/scenarios.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
31 changes: 31 additions & 0 deletions test/prepare-commit-msg-context/basic.sh
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions test/prepare-commit-msg-context/expected.txt
Original file line number Diff line number Diff line change
@@ -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
---
15 changes: 15 additions & 0 deletions test/prepare-commit-msg-context/scenarios.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
15 changes: 15 additions & 0 deletions test/prepare-commit-msg-context/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/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

# Feature-specific tests
# The 'check' command comes from the dev-container-features-test-lib.
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
2 changes: 2 additions & 0 deletions test/prepare-commit-msg-context/test_basic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
check "Check prepare-commit-msg is installed" test -f /usr/local/share/prepare-commit-msg-context/prepare-commit-msg
check "Check prepare-commit-msg is executable" test -x /usr/local/share/prepare-commit-msg-context/prepare-commit-msg

0 comments on commit b16a1a7

Please sign in to comment.