-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathgit-utils.js
More file actions
43 lines (38 loc) · 1.13 KB
/
Copy pathgit-utils.js
File metadata and controls
43 lines (38 loc) · 1.13 KB
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
const execa = require('execa');
const { relative } = require('path');
const git = async (args, options = {}) => {
const { stdout } = await execa('git', args, options);
return stdout;
};
/**
* // https://stackoverflow.com/questions/424071/how-to-list-all-the-files-in-a-commit
* @async
* @param {string} hash Git commit hash.
* @param {string} cwd Working directory.
* @return {Promise<Array>} List of modified files in a commit.
*/
const getCommitFiles = async (hash, cwd) =>
(
await git(['diff-tree', '--no-commit-id', '--name-only', '-r', hash], {
cwd,
})
).split('\n');
/**
* https://stackoverflow.com/a/957978/89594
* @async
* @param {string} cwd Working directory.
* @return {Promise<String>} System path of the git repository.
*/
const getRoot = cwd => git(['rev-parse', '--show-toplevel'], { cwd });
/**
* @async
* @param {string} path Absolute path.
* @param {string} cwd Working directory.
* @return {Promise<String>} Path relative to the git root.
*/
const getRelativePath = async (path, cwd) => relative(await getRoot(cwd), path);
module.exports = {
getCommitFiles,
getRoot,
getRelativePath,
};