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

fix(devcontainer): improve pre-commit setup and git diff handling #10

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
126 changes: 64 additions & 62 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,66 +1,68 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye",
"containerEnv": {
"PRE_COMMIT_HOME": ".pre-commit",
"OPENROUTER_API_KEY": "${localEnv:OPENROUTER_API_KEY}",
"OPENROUTER_MODEL": "${localEnv:OPENROUTER_MODEL}"
},
"features": {
"ghcr.io/devcontainers/features/python:1": {
"installTools": true,
"toolsToInstall": "pre-commit",
"version": "3.12"
},
"ghcr.io/deepspacecartel/devcontainers-features/starship:latest": {
"version": "latest"
},
"ghcr.io/devcontainers/features/github-cli:1": {
"installDirectlyFromGitHubRelease": true,
"version": "latest"
}
},
"customizations": {
"vscode": {
"settings": {
"dotfiles.repository":"deepspacecartel/dotfiles",
"remote.extensionKind": {
"ms-azuretools.vscode-docker": "ui"
}
},
// <!-- prettier-ignore-start -->
"extensions": [
// Code Formatting
"esbenp.prettier-vscode",
"streetsidesoftware.code-spell-checker",
// GitHub Integration
"GitHub.copilot",
"GitHub.vscode-github-actions",
"GitHub.vscode-pull-request-github",
// File Headers
"psioniq.psi-header",
// YAML Support
"redhat.vscode-yaml",
// Markdown Support
"bierner.github-markdown-preview",
"bierner.markdown-checkbox",
"bierner.markdown-footnotes",
"bierner.markdown-mermaid",
"bierner.markdown-preview-github-styles",
"bierner.markdown-yaml-preamble",
"chintans98.markdown-jira",
"yzhang.markdown-all-in-one",
// Task Management
"Gruntfuggly.todo-tree",
"wayou.vscode-todo-highlight",
// Development Tools
"Codeium.codeium",
"ryanluker.vscode-coverage-gutters"
]
// <!-- prettier-ignore-end -->
}
}
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye",
"containerEnv": {
"PRE_COMMIT_HOME": ".pre-commit",
"OPENROUTER_API_KEY": "${localEnv:OPENROUTER_API_KEY}"
},
"postCreateCommand": {
"pre-commit": "pre-commit install"
},
"features": {
"ghcr.io/devcontainers/features/python:1": {
"installTools": true,
"toolsToInstall": "pre-commit",
"version": "3.12"
},
"ghcr.io/deepspacecartel/devcontainers-features/starship:latest": {
"version": "latest"
},
"ghcr.io/devcontainers/features/github-cli:1": {
"installDirectlyFromGitHubRelease": true,
"version": "latest"
}
},
"customizations": {
"vscode": {
"settings": {
"dotfiles.repository": "deepspacecartel/dotfiles",
"remote.extensionKind": {
"ms-azuretools.vscode-docker": "ui"
}
},
// <!-- prettier-ignore-start -->
"extensions": [
// Code Formatting
"esbenp.prettier-vscode",
"streetsidesoftware.code-spell-checker",
// GitHub Integration
"GitHub.copilot",
"GitHub.vscode-github-actions",
"GitHub.vscode-pull-request-github",
// File Headers
"psioniq.psi-header",
// YAML Support
"redhat.vscode-yaml",
// Markdown Support
"bierner.github-markdown-preview",
"bierner.markdown-checkbox",
"bierner.markdown-footnotes",
"bierner.markdown-mermaid",
"bierner.markdown-preview-github-styles",
"bierner.markdown-yaml-preamble",
"chintans98.markdown-jira",
"yzhang.markdown-all-in-one",
// Task Management
"Gruntfuggly.todo-tree",
"wayou.vscode-todo-highlight",
// Development Tools
"Codeium.codeium",
"ryanluker.vscode-coverage-gutters"
]
// <!-- prettier-ignore-end -->
}
}
}
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ repos:
- repo: local
hooks:
- id: prepare-commit-msg
name: Git Commit AI
name: "PartCAD: Prepare Commit Message"
entry: prepare-commit-msg.sh
args: ["--open-source", "--commit-msg-filename"]
language: script
stages: [prepare-commit-msg]
- id: keep-a-changelog
name: Changelog AI
name: "PartCAD: Update CHANGELOG.md"
description: A hook to assist with updating CHANGELOG.md using AI.
entry: keep-a-changelog.sh
language: script
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# https://pre-commit.com/#creating-new-hooks
- id: prepare-commit-msg
name: Git Commit AI
name: "PartCAD: Prepare Commit Message"
description: A hook to assist with git commit messages using AI.
entry: prepare-commit-msg.sh
language: script
args: ["--commit-msg-filename"]
stages: [prepare-commit-msg]
pass_filenames: true
- id: keep-a-changelog
name: Changelog AI
name: "PartCAD: Update CHANGELOG.md"
description: A hook to assist with updating CHANGELOG.md using AI.
entry: keep-a-changelog.sh
language: script
Expand Down
9 changes: 2 additions & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
"editor.formatOnSave": true,
"editor.formatOnType": true,
"editor.insertSpaces": true,
"editor.rulers": [
120
],
"editor.rulers": [120],
"editor.tabSize": 2,
"editor.useTabStops": false,
"files.encoding": "utf8",
Expand All @@ -46,8 +44,5 @@
"terminal.integrated.fontFamily": "'Hack Nerd Font Mono', Consolas, 'Courier New', monospace",
"terminal.integrated.scrollback": 10000000,
"workbench.colorTheme": "GitHub Dark",
"cSpell.words": [
"behaviour",
"EDITMSG"
],
"cSpell.words": ["behaviour", "EDITMSG", "OPENROUTER"]
}
19 changes: 18 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,27 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixed

- jq: Argument list too long #3
- Cleaned up temporary files after API call.
- Cleaned up temporary files after API call.

## [1.7.1] - 2024-12-02

### Fixed

- prepare-commit-msg.sh: line 237: /usr/bin/curl: Argument list too long

## [1.8.0] - 2024-12-12

### Fixed

- Fixed `git diff` command to ignore whitespace.

### Changed

- Added `postCreateCommand` to `devcontainer.json` for `pre-commit` installation.
- Modified `keep-a-changelog.sh` to use `git diff --cached --ignore-all-space` to reduce API call size.
- Modified `prepare-commit-msg.sh` to handle whitespace discrepancies with `git diff --cached --ignore-all-space`.
- Use temporary files to store user and system prompts.
- Added cleanup for temp files on script exit in keep-a-changelog.sh and prepare-commit-msg.sh.
- Renamed "Changelog AI" hooks to "PartCAD: Update CHANGELOG.md"
- Renamed "Git Commit AI" hooks to "PartCAD: Prepare Commit Message"
- Improved cleanup mechanism for temporary files
23 changes: 20 additions & 3 deletions keep-a-changelog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ if [ -n "$(git diff --cached --name-only -- "${CHANGELOG_FILENAME}")" ]; then
fi

# Get git changes
CHANGES=$(git diff --cached)
CHANGES=$(git diff --cached --ignore-all-space)
CURRENT_CHANGELOG=""
if [ -f "${CHANGELOG_FILENAME}" ]; then
CURRENT_CHANGELOG=$(cat "${CHANGELOG_FILENAME}")
Expand Down Expand Up @@ -194,11 +194,28 @@ if [ -z "$OPENROUTER_API_KEY" ]; then
exit 1
fi

PROMPT_FILE=$(mktemp)
echo "$USER_PROMPT" > "$PROMPT_FILE"
debug_log "Prompt saved to $PROMPT_FILE"

SYSTEM_PROMPT_FILE=$(mktemp)
echo "$SYSTEM_PROMPT" > "$SYSTEM_PROMPT_FILE"
debug_log "System prompt saved to $SYSTEM_PROMPT_FILE"

# Ensure cleanup on script exit
cleanup() {
local exit_code=$?
rm -f "$PROMPT_FILE" "$SYSTEM_PROMPT_FILE" 2>/dev/null
exit $exit_code
}
trap cleanup EXIT INT TERM


# Prepare the request body
REQUEST_BODY=$(jq -n \
--arg model "$MODEL" \
--arg prompt "$USER_PROMPT" \
--arg system "$SYSTEM_PROMPT" \
--rawfile prompt "$PROMPT_FILE" \
--rawfile system "$SYSTEM_PROMPT_FILE" \
'{
stream: false,
model: $model,
Expand Down
14 changes: 12 additions & 2 deletions prepare-commit-msg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ IMPORTANT
- Do not include any explanation in your response
- Only return a commit message content
- Do not wrap it in backticks
- One change per line.
- All lines should be a concise summary (max 80 characters)
EOF
)

Expand Down Expand Up @@ -175,9 +177,9 @@ fi

# Get git changes
if [ "$OPEN_SOURCE" = true ]; then
CHANGES=$(git diff --cached)
CHANGES=$(git diff --cached --ignore-all-space)
else
CHANGES=$(git diff --cached --name-status)
CHANGES=$(git diff --cached --ignore-all-space --name-status)
fi


Expand Down Expand Up @@ -213,6 +215,14 @@ SYSTEM_PROMPT_FILE=$(mktemp)
echo "$SYSTEM_PROMPT" > "$SYSTEM_PROMPT_FILE"
debug_log "System prompt saved to $SYSTEM_PROMPT_FILE"

# Ensure cleanup on script exit
cleanup() {
local exit_code=$?
rm -f "$PROMPT_FILE" "$SYSTEM_PROMPT_FILE" "$REQUEST_BODY_FILE" 2>/dev/null
exit $exit_code
}
trap cleanup EXIT INT TERM

# Prepare the request body
REQUEST_BODY=$(jq -n \
--arg model "$MODEL" \
Expand Down