UNPKG

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