Skip to content

Commit a9468f5

Browse files
authored
fix: handle git repo with no commits (#12)
1 parent 48073d8 commit a9468f5

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

src/scms/git.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,42 @@ const runGit = (directory, args) =>
1919
const getLines = execaResult => execaResult.stdout.split('\n');
2020

2121
export const getSinceRevision = (directory, { staged }) => {
22-
const revision = staged
23-
? 'HEAD'
24-
: runGit(directory, ['merge-base', 'HEAD', 'master']).stdout.trim();
25-
return runGit(directory, ['rev-parse', '--short', revision]).stdout.trim();
22+
try {
23+
const revision = staged
24+
? 'HEAD'
25+
: runGit(directory, ['merge-base', 'HEAD', 'master']).stdout.trim();
26+
return runGit(directory, ['rev-parse', '--short', revision]).stdout.trim();
27+
} catch (error) {
28+
if (
29+
/HEAD/.test(error.message) ||
30+
(staged && /Needed a single revision/.test(error.message))
31+
) {
32+
return null;
33+
}
34+
throw error;
35+
}
2636
};
2737

2838
export const getChangedFiles = (directory, revision, staged) => {
29-
return (staged
30-
? getLines(
31-
runGit(directory, [
39+
return [
40+
...getLines(
41+
runGit(
42+
directory,
43+
[
3244
'diff',
3345
'--name-only',
34-
'--cached',
46+
staged ? '--cached' : null,
3547
'--diff-filter=ACMRTUB',
3648
revision,
37-
])
49+
].filter(Boolean)
3850
)
39-
: [
40-
...getLines(
41-
runGit(directory, [
42-
'diff',
43-
'--name-only',
44-
'--diff-filter=ACMRTUB',
45-
revision,
46-
])
47-
),
48-
...getLines(
51+
),
52+
...(staged
53+
? []
54+
: getLines(
4955
runGit(directory, ['ls-files', '--others', '--exclude-standard'])
50-
),
51-
]
52-
).filter(Boolean);
56+
)),
57+
].filter(Boolean);
5358
};
5459

5560
export const stageFile = (directory, file) => {

0 commit comments

Comments
 (0)