Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement prepare-commit-msg-context feature #4

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
14 changes: 14 additions & 0 deletions test/prepare-commit-msg-context/test.sh
Original file line number Diff line number Diff line change
@@ -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