|
| 1 | +# Cross-Platform Git Commit Helper |
| 2 | + |
| 3 | +This directory contains tools to resolve heredoc issues when committing on Windows and ensure consistent cross-platform git commit behavior. |
| 4 | + |
| 5 | +## Problem Solved |
| 6 | + |
| 7 | +The original issue was that heredoc syntax (`<<'EOF'`) used in commit messages only works in bash/Unix shells and fails on Windows Command Prompt. This caused commit failures on Windows systems. |
| 8 | + |
| 9 | +## Files |
| 10 | + |
| 11 | +### Core Scripts |
| 12 | +- **`commit-helper.js`** - Main Node.js script that handles cross-platform commits |
| 13 | +- **`commit.bat`** - Windows batch wrapper |
| 14 | +- **`commit.sh`** - Unix/Linux/macOS shell wrapper |
| 15 | + |
| 16 | +### Utility Libraries |
| 17 | +- **`../common/src/util/git-cross-platform.ts`** - TypeScript utility functions for cross-platform git operations |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Command Line |
| 22 | + |
| 23 | +#### Windows |
| 24 | +```batch |
| 25 | +scripts\commit.bat "Your commit message" |
| 26 | +scripts\commit.bat "Title" "Body line 1" "Body line 2" |
| 27 | +``` |
| 28 | + |
| 29 | +#### Unix/Linux/macOS |
| 30 | +```bash |
| 31 | +scripts/commit.sh "Your commit message" |
| 32 | +scripts/commit.sh "Title" "Body line 1" "Body line 2" |
| 33 | +``` |
| 34 | + |
| 35 | +#### Direct Node.js (Cross-platform) |
| 36 | +```bash |
| 37 | +node scripts/commit-helper.js "Your commit message" |
| 38 | +node scripts/commit-helper.js "Title" "Body line 1" "Body line 2" |
| 39 | +``` |
| 40 | + |
| 41 | +### Programmatic Usage |
| 42 | + |
| 43 | +#### TypeScript/JavaScript |
| 44 | +```typescript |
| 45 | +import { commitChanges, commitChangesMultiline } from '../common/src/util/git-cross-platform' |
| 46 | + |
| 47 | +// Simple commit |
| 48 | +commitChanges("Fix bug in authentication") |
| 49 | + |
| 50 | +// Multiline commit |
| 51 | +commitChangesMultiline("Add new feature", [ |
| 52 | + "- Implemented user authentication", |
| 53 | + "- Added comprehensive tests", |
| 54 | + "- Updated documentation" |
| 55 | +]) |
| 56 | +``` |
| 57 | + |
| 58 | +#### ES Modules |
| 59 | +```javascript |
| 60 | +import { createCommitMessage, commitWithTempFile } from './scripts/commit-helper.js' |
| 61 | + |
| 62 | +const message = createCommitMessage(["Fix critical bug"]) |
| 63 | +commitWithTempFile(message) |
| 64 | +``` |
| 65 | + |
| 66 | +## Features |
| 67 | + |
| 68 | +### ✅ Cross-Platform Compatibility |
| 69 | +- Works on Windows (cmd.exe, PowerShell) |
| 70 | +- Works on Unix/Linux/macOS (bash, zsh, fish) |
| 71 | +- Handles different line ending conventions |
| 72 | + |
| 73 | +### ✅ Automatic Co-Author Attribution |
| 74 | +All commits automatically include: |
| 75 | +``` |
| 76 | +Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> |
| 77 | +``` |
| 78 | + |
| 79 | +### ✅ Multiline Message Support |
| 80 | +- Supports complex commit messages with titles and body |
| 81 | +- Handles special characters and quotes safely |
| 82 | +- No heredoc syntax issues |
| 83 | + |
| 84 | +### ✅ Fallback Mechanisms |
| 85 | +- Primary: Temporary file method (most reliable) |
| 86 | +- Fallback: Direct git commit (if temp file fails) |
| 87 | +- Graceful error handling |
| 88 | + |
| 89 | +## Technical Details |
| 90 | + |
| 91 | +### How It Works |
| 92 | + |
| 93 | +1. **Input Processing**: Accepts single or multiple arguments for commit messages |
| 94 | +2. **Message Formatting**: Combines title and body lines with proper spacing |
| 95 | +3. **Co-Author Addition**: Automatically appends factory-droid attribution |
| 96 | +4. **Temporary File Method**: Writes message to temp file and uses `git commit -F` |
| 97 | +5. **Cleanup**: Removes temporary files after commit |
| 98 | + |
| 99 | +### Why Temporary Files? |
| 100 | + |
| 101 | +The temporary file approach (`git commit -F file`) is used because: |
| 102 | +- Avoids shell escaping issues across different platforms |
| 103 | +- Handles multiline messages reliably |
| 104 | +- Works with any special characters or quotes |
| 105 | +- No heredoc syntax required |
| 106 | + |
| 107 | +### Platform-Specific Considerations |
| 108 | + |
| 109 | +#### Windows |
| 110 | +- Escapes double quotes in messages |
| 111 | +- Uses Windows-style paths for temp files |
| 112 | +- Compatible with both cmd.exe and PowerShell |
| 113 | + |
| 114 | +#### Unix/Linux/macOS |
| 115 | +- Escapes single quotes and backslashes |
| 116 | +- Uses POSIX-style paths |
| 117 | +- Compatible with bash, zsh, fish shells |
| 118 | + |
| 119 | +## Error Handling |
| 120 | + |
| 121 | +The helper includes multiple fallback mechanisms: |
| 122 | +1. Try temp file approach with co-author |
| 123 | +2. Fall back to simple git commit if temp file fails |
| 124 | +3. Provide clear error messages for debugging |
| 125 | + |
| 126 | +## Examples |
| 127 | + |
| 128 | +### Simple Commit |
| 129 | +```bash |
| 130 | +node scripts/commit-helper.js "Fix typo in README" |
| 131 | +``` |
| 132 | +Results in: |
| 133 | +``` |
| 134 | +Fix typo in README |
| 135 | +
|
| 136 | +Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> |
| 137 | +``` |
| 138 | + |
| 139 | +### Complex Multiline Commit |
| 140 | +```bash |
| 141 | +node scripts/commit-helper.js "Add cross-platform commit helper" "Resolves heredoc issues on Windows" "- Created commit-helper.js script" "- Added Windows batch wrapper" "- Added Unix shell wrapper" |
| 142 | +``` |
| 143 | +Results in: |
| 144 | +``` |
| 145 | +Add cross-platform commit helper |
| 146 | +
|
| 147 | +Resolves heredoc issues on Windows |
| 148 | +- Created commit-helper.js script |
| 149 | +- Added Windows batch wrapper |
| 150 | +- Added Unix shell wrapper |
| 151 | +
|
| 152 | +Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> |
| 153 | +``` |
| 154 | + |
| 155 | +## Integration |
| 156 | + |
| 157 | +This helper can be integrated into: |
| 158 | +- Build scripts and CI/CD pipelines |
| 159 | +- Development workflows |
| 160 | +- Automated commit processes |
| 161 | +- IDE extensions and tools |
| 162 | + |
| 163 | +The TypeScript utilities in `git-cross-platform.ts` provide a clean API for programmatic use within the Codebuff codebase. |
0 commit comments