-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathgit.js
65 lines (59 loc) · 1.9 KB
/
git.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const execa = require('execa');
const debug = require('debug')('semantic-release:git');
/**
* Retrieve the list of files modified on the local repository.
*
* @param {Object} [execaOpts] Options to pass to `execa`.
*
* @return {Array<String>} Array of modified files path.
*/
async function getModifiedFiles(execaOptions) {
return (await execa('git', ['ls-files', '-m', '-o'], execaOptions)).stdout
.split('\n')
.map((file) => file.trim())
.filter((file) => Boolean(file));
}
/**
* Add a list of file to the Git index. `.gitignore` will be ignored.
*
* @param {Array<String>} files Array of files path to add to the index.
* @param {Object} [execaOpts] Options to pass to `execa`.
*/
async function add(files, execaOptions) {
const shell = await execa('git', ['add', '--force', '--ignore-errors', ...files], {...execaOptions, reject: false});
debug('add file to git index', shell);
}
/**
* Commit to the local repository.
*
* @param {String} message Commit message.
* @param {Object} [execaOpts] Options to pass to `execa`.
*
* @throws {Error} if the commit failed.
*/
async function commit(message, execaOptions) {
await execa('git', ['commit', '-m', message], execaOptions);
}
/**
* Push to the remote repository.
*
* @param {String} origin The remote repository URL.
* @param {String} branch The branch to push.
* @param {Object} [execaOpts] Options to pass to `execa`.
*
* @throws {Error} if the push failed.
*/
async function push(origin, branch, execaOptions) {
await execa('git', ['push', '--tags', origin, `HEAD:${branch}`], execaOptions);
}
/**
* Get the HEAD sha.
*
* @param {Object} [execaOpts] Options to pass to `execa`.
*
* @return {String} The sha of the head commit on the local repository
*/
async function gitHead(execaOptions) {
return (await execa('git', ['rev-parse', 'HEAD'], execaOptions)).stdout;
}
module.exports = {getModifiedFiles, add, gitHead, commit, push};