This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getChangedFiles.js
52 lines (45 loc) · 1.93 KB
/
getChangedFiles.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
// @flow
import os from 'os';
import isCI from 'is-ci';
import Git from './Git';
/**
* This function tries to determine what changes are relevant at the moment.
* Relevant changes are unstaged/uncommited files, modified files on the branch
* from master or changes in the last commit as a fallback. It expects default
* branch to be master. This is how it works internally:
*
* First it executes these commands:
*
* git ls-files --others --exclude-standard # untracked files
* +
* git --no-pager diff --name-only HEAD # uncommited but staged files
* +
* git --no-pager diff --name-only origin/master...HEAD # modified files on the feature branch (3 dots are important)
*
* Fallback for master or new branches of no result was returned:
*
* git --no-pager diff --name-only HEAD^ HEAD # latest commit (doesn't work with only one commit in Git history)
*
* For more details: https://git-scm.com/docs/git-diff
*/
export default function getChangedFiles(): $ReadOnlyArray<string> {
const uncommittedChanges = Git.getUntrackedFiles().concat(
Git.getWorktreeChangedFiles(),
);
// It's OK to run tests on uncommitted changes locally but it's unexpected
// in CI because it indicates that CI generated something which is not
// expected (tests runner would be confused and it would try to test
// the newly generated files).
if (isCI === true && uncommittedChanges.length > 0) {
// eslint-disable-next-line no-console
console.error(
`ERROR: There are some uncommitted changes in the working tree:
${uncommittedChanges.join(os.EOL)}
This usually means that CI generated some changes (for example during installation of dependencies) which is unexpected. Please try to fix it locally and commit the newly generated files.`,
);
process.exit(1);
}
return uncommittedChanges.length > 0
? uncommittedChanges
: Git.getLastCommitChanges();
}