Skip to content

Commit 15b3d7e

Browse files
committed
feat: use short git revisions
1 parent f0b2e3a commit 15b3d7e

File tree

4 files changed

+46
-31
lines changed

4 files changed

+46
-31
lines changed

src/__tests__/scm-git.test.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ afterEach(() => {
1313

1414
const mockGitFs = () => {
1515
mock({
16-
'root/.git': {},
17-
'root/foo.js': 'foo()',
18-
'root/bar.md': '# foo',
16+
'/.git': {},
17+
'/foo.js': 'foo()',
18+
'/bar.md': '# foo',
1919
});
2020
execa.sync.mockImplementation((command, args) => {
2121
if (command !== 'git') {
@@ -37,21 +37,36 @@ const mockGitFs = () => {
3737
describe('with git', () => {
3838
test('calls `git merge-base`', () => {
3939
mock({
40-
'root/.git': {},
40+
'/.git': {},
4141
});
4242

4343
prettyQuick('root');
4444

4545
expect(execa.sync).toHaveBeenCalledWith(
4646
'git',
4747
['merge-base', 'HEAD', 'master'],
48-
{ cwd: 'root' }
48+
{ cwd: '/' }
49+
);
50+
});
51+
52+
test('calls `git merge-base` with root git directory', () => {
53+
mock({
54+
'/.git': {},
55+
'/other-dir': {},
56+
});
57+
58+
prettyQuick('/other-dir');
59+
60+
expect(execa.sync).toHaveBeenCalledWith(
61+
'git',
62+
['merge-base', 'HEAD', 'master'],
63+
{ cwd: '/' }
4964
);
5065
});
5166

5267
test('with --staged does NOT call `git merge-base`', () => {
5368
mock({
54-
'root/.git': {},
69+
'/.git': {},
5570
});
5671

5772
prettyQuick('root');
@@ -65,37 +80,37 @@ describe('with git', () => {
6580

6681
test('calls `git diff --name-only` with revision', () => {
6782
mock({
68-
'root/.git': {},
83+
'/.git': {},
6984
});
7085

7186
prettyQuick('root', { since: 'banana' });
7287

7388
expect(execa.sync).toHaveBeenCalledWith(
7489
'git',
7590
['diff', '--name-only', '--diff-filter=ACMRTUB', 'banana'],
76-
{ cwd: 'root' }
91+
{ cwd: '/' }
7792
);
7893
});
7994

8095
test('calls `git ls-files`', () => {
8196
mock({
82-
'root/.git': {},
97+
'/.git': {},
8398
});
8499

85100
prettyQuick('root', { since: 'banana' });
86101

87102
expect(execa.sync).toHaveBeenCalledWith(
88103
'git',
89104
['ls-files', '--others', '--exclude-standard'],
90-
{ cwd: 'root' }
105+
{ cwd: '/' }
91106
);
92107
});
93108

94109
test('calls onFoundSinceRevision with return value from `git merge-base`', () => {
95110
const onFoundSinceRevision = jest.fn();
96111

97112
mock({
98-
'root/.git': {},
113+
'/.git': {},
99114
});
100115
execa.sync.mockReturnValue({ stdout: 'banana' });
101116

@@ -130,8 +145,8 @@ describe('with git', () => {
130145

131146
prettyQuick('root', { since: 'banana', onWriteFile });
132147

133-
expect(fs.readFileSync('root/foo.js', 'utf8')).toEqual('formatted:foo()');
134-
expect(fs.readFileSync('root/bar.md', 'utf8')).toEqual('formatted:# foo');
148+
expect(fs.readFileSync('/foo.js', 'utf8')).toEqual('formatted:foo()');
149+
expect(fs.readFileSync('/bar.md', 'utf8')).toEqual('formatted:# foo');
135150
});
136151

137152
test('with --staged stages changed files', () => {
@@ -140,10 +155,10 @@ describe('with git', () => {
140155
prettyQuick('root', { since: 'banana', staged: true });
141156

142157
expect(execa.sync).toHaveBeenCalledWith('git', ['add', './foo.js'], {
143-
cwd: 'root',
158+
cwd: '/',
144159
});
145160
expect(execa.sync).toHaveBeenCalledWith('git', ['add', './bar.md'], {
146-
cwd: 'root',
161+
cwd: '/',
147162
});
148163
});
149164

@@ -153,10 +168,10 @@ describe('with git', () => {
153168
prettyQuick('root', { since: 'banana' });
154169

155170
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './foo.js'], {
156-
cwd: 'root',
171+
cwd: '/',
157172
});
158173
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './bar.md'], {
159-
cwd: 'root',
174+
cwd: '/',
160175
});
161176
});
162177
});

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import createIgnorer from './createIgnorer';
44
import isSupportedExtension from './isSupportedExtension';
55

66
export default (
7-
directory,
7+
currentDirectory,
88
{
99
config,
1010
since,
@@ -14,10 +14,11 @@ export default (
1414
onWriteFile,
1515
} = {}
1616
) => {
17-
const scm = scms(directory);
17+
const scm = scms(currentDirectory);
1818
if (!scm) {
1919
throw new Error('Unable to detect a source control manager.');
2020
}
21+
const directory = scm.rootDirectory;
2122

2223
const revision = since || scm.getSinceRevision(directory, { staged });
2324

src/scms/git.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { statSync } from 'fs';
2-
import { join } from 'path';
31
import findUp from 'find-up';
42
import execa from 'execa';
3+
import { dirname } from 'path';
54

65
export const name = 'git';
76

87
export const detect = directory => {
9-
try {
10-
return !!findUp.sync('.git', { cwd: directory });
11-
} catch (error) {
12-
return false;
8+
const gitDirectory = findUp.sync('.git', { cwd: directory });
9+
if (gitDirectory) {
10+
return dirname(gitDirectory);
1311
}
1412
};
1513

@@ -21,10 +19,10 @@ const runGit = (directory, args) =>
2119
const getLines = execaResult => execaResult.stdout.split('\n');
2220

2321
export const getSinceRevision = (directory, { staged }) => {
24-
if (staged) {
25-
return 'HEAD';
26-
}
27-
return runGit(directory, ['merge-base', 'HEAD', 'master']).stdout.trim();
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();
2826
};
2927

3028
export const getChangedFiles = (directory, revision) => {

src/scms/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ const scms = [gitScm];
44

55
export default directory => {
66
for (const scm of scms) {
7-
if (scm.detect(directory)) {
8-
return scm;
7+
const rootDirectory = scm.detect(directory);
8+
if (rootDirectory) {
9+
return Object.assign({ rootDirectory }, scm);
910
}
1011
}
1112
};

0 commit comments

Comments
 (0)