-
Notifications
You must be signed in to change notification settings - Fork 0
feat: handle edge cases in commit-msg hook (GIT-85) #56
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
+195
−19
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -534,7 +534,7 @@ Because REST conventions make the API predictable.`; | |
| assert.ok(modifiedMsg.includes('AI-Decision:'), 'Should detect decision from "because"'); | ||
| }); | ||
|
|
||
| it('should handle merge commit message', () => { | ||
| it('should skip merge commit message', () => { | ||
| process.env.GIT_MEM_AGENT = 'TestAgent/2.0'; | ||
|
|
||
| const commitMsg = `Merge branch 'feature/auth' into main | ||
|
|
@@ -551,19 +551,17 @@ Because REST conventions make the API predictable.`; | |
|
|
||
| assert.equal(result.status, 0); | ||
|
|
||
| // Should add basic trailers even for merge commits | ||
| // Should NOT add trailers to merge commits | ||
| const modifiedMsg = readFileSync(msgPath, 'utf8'); | ||
| assert.ok(modifiedMsg.includes('AI-Agent:'), 'Should add trailers to merge commit'); | ||
| assert.ok(!modifiedMsg.includes('AI-Agent:'), 'Should skip merge commits'); | ||
| }); | ||
|
|
||
| it('should handle amend commit message', () => { | ||
| it('should skip merge pull request commit', () => { | ||
| process.env.GIT_MEM_AGENT = 'TestAgent/2.0'; | ||
|
|
||
| // Simulating an amend - the message file may have existing content from previous commit | ||
| const commitMsg = `feat: original feature | ||
| const commitMsg = `Merge pull request #123 from feature/auth | ||
|
|
||
| # This is an amended commit | ||
| Previously: fix typo`; | ||
| Add authentication feature`; | ||
|
|
||
| const msgPath = createCommitMsgFile(repoDir, commitMsg); | ||
|
|
||
|
|
@@ -575,7 +573,144 @@ Previously: fix typo`; | |
| assert.equal(result.status, 0); | ||
|
|
||
| const modifiedMsg = readFileSync(msgPath, 'utf8'); | ||
| assert.ok(modifiedMsg.includes('AI-Agent:'), 'Should add trailers to amended commit'); | ||
| assert.ok(!modifiedMsg.includes('AI-Agent:'), 'Should skip merge PR commits'); | ||
| }); | ||
|
|
||
| it('should skip fixup! commits', () => { | ||
| process.env.GIT_MEM_AGENT = 'TestAgent/2.0'; | ||
|
|
||
| const commitMsg = 'fixup! feat: original commit'; | ||
| const msgPath = createCommitMsgFile(repoDir, commitMsg); | ||
|
|
||
| const result = runHook('commit-msg', { | ||
| commit_msg_path: msgPath, | ||
| cwd: repoDir, | ||
| }); | ||
|
|
||
| assert.equal(result.status, 0); | ||
|
|
||
| const modifiedMsg = readFileSync(msgPath, 'utf8'); | ||
| assert.ok(!modifiedMsg.includes('AI-Agent:'), 'Should skip fixup commits'); | ||
| }); | ||
|
|
||
| it('should skip squash! commits', () => { | ||
| process.env.GIT_MEM_AGENT = 'TestAgent/2.0'; | ||
|
|
||
| const commitMsg = 'squash! feat: original commit'; | ||
| const msgPath = createCommitMsgFile(repoDir, commitMsg); | ||
|
|
||
| const result = runHook('commit-msg', { | ||
| commit_msg_path: msgPath, | ||
| cwd: repoDir, | ||
| }); | ||
|
|
||
| assert.equal(result.status, 0); | ||
|
|
||
| const modifiedMsg = readFileSync(msgPath, 'utf8'); | ||
| assert.ok(!modifiedMsg.includes('AI-Agent:'), 'Should skip squash commits'); | ||
| }); | ||
|
|
||
| it('should skip amend! commits', () => { | ||
| process.env.GIT_MEM_AGENT = 'TestAgent/2.0'; | ||
|
|
||
| const commitMsg = 'amend! feat: original commit'; | ||
| const msgPath = createCommitMsgFile(repoDir, commitMsg); | ||
|
|
||
| const result = runHook('commit-msg', { | ||
| commit_msg_path: msgPath, | ||
| cwd: repoDir, | ||
| }); | ||
|
|
||
| assert.equal(result.status, 0); | ||
|
|
||
| const modifiedMsg = readFileSync(msgPath, 'utf8'); | ||
| assert.ok(!modifiedMsg.includes('AI-Agent:'), 'Should skip amend! commits'); | ||
| }); | ||
|
|
||
| it('should skip revert commits', () => { | ||
| process.env.GIT_MEM_AGENT = 'TestAgent/2.0'; | ||
|
|
||
| const commitMsg = `Revert "feat: add problematic feature" | ||
|
|
||
| This reverts commit abc1234.`; | ||
|
|
||
| const msgPath = createCommitMsgFile(repoDir, commitMsg); | ||
|
|
||
| const result = runHook('commit-msg', { | ||
| commit_msg_path: msgPath, | ||
| cwd: repoDir, | ||
| }); | ||
|
|
||
| assert.equal(result.status, 0); | ||
|
|
||
| const modifiedMsg = readFileSync(msgPath, 'utf8'); | ||
| assert.ok(!modifiedMsg.includes('AI-Agent:'), 'Should skip revert commits'); | ||
| }); | ||
|
|
||
| it('should handle regular amend (git commit --amend)', () => { | ||
| process.env.GIT_MEM_AGENT = 'TestAgent/2.0'; | ||
|
|
||
| // Regular amend - message doesn't start with "amend!" | ||
| // Just modifying a previous commit message | ||
| const commitMsg = `feat: original feature (amended) | ||
|
|
||
| Updated the implementation details.`; | ||
|
|
||
| const msgPath = createCommitMsgFile(repoDir, commitMsg); | ||
|
|
||
| const result = runHook('commit-msg', { | ||
| commit_msg_path: msgPath, | ||
| cwd: repoDir, | ||
| }); | ||
|
|
||
| assert.equal(result.status, 0); | ||
|
|
||
| // Regular amends should still get trailers | ||
| const modifiedMsg = readFileSync(msgPath, 'utf8'); | ||
| assert.ok(modifiedMsg.includes('AI-Agent:'), 'Should add trailers to regular amended commit'); | ||
| }); | ||
|
|
||
| it('should handle unicode and emoji in commit message', () => { | ||
| process.env.GIT_MEM_AGENT = 'TestAgent/2.0'; | ||
|
|
||
| const commitMsg = 'feat: add 日本語 support 🎉\n\nThis adds internationalization because users need 多言語 support.'; | ||
| const msgPath = createCommitMsgFile(repoDir, commitMsg); | ||
|
|
||
| const result = runHook('commit-msg', { | ||
| commit_msg_path: msgPath, | ||
| cwd: repoDir, | ||
| }); | ||
|
|
||
| assert.equal(result.status, 0); | ||
|
|
||
| const modifiedMsg = readFileSync(msgPath, 'utf8'); | ||
| assert.ok(modifiedMsg.includes('🎉'), 'Should preserve emoji'); | ||
| assert.ok(modifiedMsg.includes('日本語'), 'Should preserve unicode'); | ||
| assert.ok(modifiedMsg.includes('AI-Agent:'), 'Should add trailers'); | ||
| }); | ||
|
|
||
| it('should handle very long commit message', () => { | ||
| process.env.GIT_MEM_AGENT = 'TestAgent/2.0'; | ||
|
|
||
| // Create a long commit message (> 1000 chars) | ||
| const longBody = 'This is a detailed explanation. '.repeat(50); | ||
| const commitMsg = `feat: add comprehensive feature\n\n${longBody}\n\nBecause this decision will help with scaling.`; | ||
| const msgPath = createCommitMsgFile(repoDir, commitMsg); | ||
|
|
||
| const result = runHook('commit-msg', { | ||
| commit_msg_path: msgPath, | ||
| cwd: repoDir, | ||
| }); | ||
|
|
||
| assert.equal(result.status, 0); | ||
|
|
||
| const modifiedMsg = readFileSync(msgPath, 'utf8'); | ||
| assert.ok(modifiedMsg.includes('AI-Agent:'), 'Should handle long messages'); | ||
| // Content should be truncated in trailers (200 char limit in buildTrailers) | ||
| const decisionMatch = modifiedMsg.match(/AI-Decision: (.+)/); | ||
| if (decisionMatch) { | ||
| assert.ok(decisionMatch[1].length <= 200, 'Trailer content should be truncated to 200 chars'); | ||
| } | ||
|
Comment on lines
+692
to
+713
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make the truncation test assert the decision trailer exists. Right now the test passes even if ✅ Tighten the assertion- const decisionMatch = modifiedMsg.match(/AI-Decision: (.+)/);
- if (decisionMatch) {
- assert.ok(decisionMatch[1].length <= 200, 'Trailer content should be truncated to 200 chars');
- }
+ const decisionMatch = modifiedMsg.match(/AI-Decision: (.+)/);
+ assert.ok(decisionMatch, 'Should include AI-Decision trailer');
+ assert.ok(decisionMatch[1].length <= 200, 'Trailer content should be truncated to 200 chars');🤖 Prompt for AI Agents |
||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Optional: avoid reading the first line three times.
You can capture the first line once and reuse it for the three skip checks to reduce subprocesses and keep the script a bit cleaner.
♻️ Optional refactor
📝 Committable suggestion
🤖 Prompt for AI Agents