-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Implement --hook option for git hooks integration (#615)
fixes #448 (re #462) This pr allows project maintainers to enforce Commitizen generated commit messages as part of the workflow triggered by the `git commit` command. * implements the `--hook` flag, which directs Commitizen to edit the `.git/COMMIT_EDITMSG` file directly. * documents the use of the `--hook` flag in the `README`, both through traditional `git hooks` and `husky`.
- Loading branch information
Showing
5 changed files
with
162 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import { spawn } from 'child_process'; | ||
|
||
import path from 'path'; | ||
|
||
import { writeFileSync, openSync, closeSync } from 'fs'; | ||
|
||
import dedent from 'dedent'; | ||
|
||
export { commit }; | ||
|
@@ -9,35 +13,70 @@ export { commit }; | |
*/ | ||
function commit (sh, repoPath, message, options, done) { | ||
let called = false; | ||
let args = ['commit', '-m', dedent(message), ...(options.args || [])]; | ||
let child = spawn('git', args, { | ||
cwd: repoPath, | ||
stdio: options.quiet ? 'ignore' : 'inherit' | ||
}); | ||
|
||
child.on('error', function (err) { | ||
if (called) return; | ||
called = true; | ||
|
||
done(err); | ||
}); | ||
|
||
child.on('exit', function (code, signal) { | ||
if (called) return; | ||
called = true; | ||
|
||
if (code) { | ||
if (code === 128) { | ||
console.warn(` | ||
Git exited with code 128. Did you forget to run: | ||
git config --global user.email "[email protected]" | ||
git config --global user.name "Your Name" | ||
`) | ||
|
||
// commit the file by spawning a git process, unless the --hook | ||
// option was provided. in that case, write the commit message into | ||
// the .git/COMMIT_EDITMSG file | ||
if (!options.hookMode) { | ||
let args = ['commit', '-m', dedent(message), ...(options.args || [])]; | ||
let child = spawn('git', args, { | ||
cwd: repoPath, | ||
stdio: options.quiet ? 'ignore' : 'inherit' | ||
}); | ||
|
||
child.on('error', function (err) { | ||
if (called) return; | ||
called = true; | ||
|
||
done(err); | ||
}); | ||
|
||
child.on('exit', function (code, signal) { | ||
if (called) return; | ||
called = true; | ||
|
||
if (code) { | ||
if (code === 128) { | ||
console.warn(` | ||
Git exited with code 128. Did you forget to run: | ||
git config --global user.email "[email protected]" | ||
git config --global user.name "Your Name" | ||
`) | ||
} | ||
done(Object.assign(new Error(`git exited with error code ${code}`), { code, signal })); | ||
} else { | ||
done(null); | ||
} | ||
}); | ||
} else { | ||
const commitFilePath = path.join(repoPath, '/.git/COMMIT_EDITMSG'); | ||
try { | ||
const fd = openSync(commitFilePath, 'w'); | ||
try { | ||
writeFileSync(fd, dedent(message)); | ||
done(null); | ||
} catch (e) { | ||
done(e); | ||
} finally { | ||
closeSync(fd); | ||
} | ||
} catch (e) { | ||
// windows doesn't allow opening existing hidden files | ||
// in 'w' mode... but it does let you do 'r+'! | ||
try { | ||
const fd = openSync(commitFilePath, 'r+'); | ||
try { | ||
writeFileSync(fd, dedent(message)); | ||
done(null); | ||
} catch (e) { | ||
done(e); | ||
} finally { | ||
closeSync(fd); | ||
} | ||
} catch (e) { | ||
done(e); | ||
} | ||
done(Object.assign(new Error(`git exited with error code ${code}`), { code, signal })); | ||
} else { | ||
done(null); | ||
} | ||
}); | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,36 +1,45 @@ | ||
/* eslint-env mocha */ | ||
|
||
import { expect } from 'chai'; | ||
import { parse } from '../../src/cli/parsers/git-cz'; | ||
import { gitCz as gitCzParser, commitizen as commitizenParser } from '../../src/cli/parsers'; | ||
|
||
describe('parsers', () => { | ||
describe('git-cz', () => { | ||
it('should parse --message "Hello, World!"', () => { | ||
expect(parse(['--amend', '--message', 'Hello, World!'])).to.deep.equal(['--amend']); | ||
expect(gitCzParser.parse(['--amend', '--message', 'Hello, World!'])).to.deep.equal(['--amend']); | ||
}); | ||
|
||
it('should parse --message="Hello, World!"', () => { | ||
expect(parse(['--amend', '--message=Hello, World!'])).to.deep.equal(['--amend']); | ||
expect(gitCzParser.parse(['--amend', '--message=Hello, World!'])).to.deep.equal(['--amend']); | ||
}); | ||
|
||
it('should parse -amwip', () => { | ||
expect(parse(['-amwip'])).to.deep.equal(['-a']); | ||
expect(gitCzParser.parse(['-amwip'])).to.deep.equal(['-a']); | ||
}); | ||
|
||
it('should parse -am=wip', () => { | ||
expect(parse(['-am=wip'])).to.deep.equal(['-a']); | ||
expect(gitCzParser.parse(['-am=wip'])).to.deep.equal(['-a']); | ||
}); | ||
|
||
it('should parse -am wip', () => { | ||
expect(parse(['-am', 'wip'])).to.deep.equal(['-a']); | ||
expect(gitCzParser.parse(['-am', 'wip'])).to.deep.equal(['-a']); | ||
}); | ||
|
||
it('should parse -a -m wip -n', () => { | ||
expect(parse(['-a', '-m', 'wip', '-n'])).to.deep.equal(['-a', '-n']); | ||
expect(gitCzParser.parse(['-a', '-m', 'wip', '-n'])).to.deep.equal(['-a', '-n']); | ||
}); | ||
|
||
it('should parse -a -m=wip -n', () => { | ||
expect(parse(['-a', '-m=wip', '-n'])).to.deep.equal(['-a', '-n']); | ||
expect(gitCzParser.parse(['-a', '-m=wip', '-n'])).to.deep.equal(['-a', '-n']); | ||
}); | ||
}); | ||
|
||
describe('commitizen', () => { | ||
it('should parse out the --amend option', () => { | ||
expect(commitizenParser.parse(['--amend'])).to.deep.equal({ _: [], amend: true }) | ||
}); | ||
it('should parse out the --hook option', () => { | ||
expect(commitizenParser.parse(['--hook'])).to.deep.equal({ _: [], hook: true }) | ||
}); | ||
}); | ||
}); |