Skip to content

Commit

Permalink
feat: use short git revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
azz committed Jan 9, 2018
1 parent f0b2e3a commit 15b3d7e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
49 changes: 32 additions & 17 deletions src/__tests__/scm-git.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ afterEach(() => {

const mockGitFs = () => {
mock({
'root/.git': {},
'root/foo.js': 'foo()',
'root/bar.md': '# foo',
'/.git': {},
'/foo.js': 'foo()',
'/bar.md': '# foo',
});
execa.sync.mockImplementation((command, args) => {
if (command !== 'git') {
Expand All @@ -37,21 +37,36 @@ const mockGitFs = () => {
describe('with git', () => {
test('calls `git merge-base`', () => {
mock({
'root/.git': {},
'/.git': {},
});

prettyQuick('root');

expect(execa.sync).toHaveBeenCalledWith(
'git',
['merge-base', 'HEAD', 'master'],
{ cwd: 'root' }
{ cwd: '/' }
);
});

test('calls `git merge-base` with root git directory', () => {
mock({
'/.git': {},
'/other-dir': {},
});

prettyQuick('/other-dir');

expect(execa.sync).toHaveBeenCalledWith(
'git',
['merge-base', 'HEAD', 'master'],
{ cwd: '/' }
);
});

test('with --staged does NOT call `git merge-base`', () => {
mock({
'root/.git': {},
'/.git': {},
});

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

test('calls `git diff --name-only` with revision', () => {
mock({
'root/.git': {},
'/.git': {},
});

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

expect(execa.sync).toHaveBeenCalledWith(
'git',
['diff', '--name-only', '--diff-filter=ACMRTUB', 'banana'],
{ cwd: 'root' }
{ cwd: '/' }
);
});

test('calls `git ls-files`', () => {
mock({
'root/.git': {},
'/.git': {},
});

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

expect(execa.sync).toHaveBeenCalledWith(
'git',
['ls-files', '--others', '--exclude-standard'],
{ cwd: 'root' }
{ cwd: '/' }
);
});

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

mock({
'root/.git': {},
'/.git': {},
});
execa.sync.mockReturnValue({ stdout: 'banana' });

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

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

expect(fs.readFileSync('root/foo.js', 'utf8')).toEqual('formatted:foo()');
expect(fs.readFileSync('root/bar.md', 'utf8')).toEqual('formatted:# foo');
expect(fs.readFileSync('/foo.js', 'utf8')).toEqual('formatted:foo()');
expect(fs.readFileSync('/bar.md', 'utf8')).toEqual('formatted:# foo');
});

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

expect(execa.sync).toHaveBeenCalledWith('git', ['add', './foo.js'], {
cwd: 'root',
cwd: '/',
});
expect(execa.sync).toHaveBeenCalledWith('git', ['add', './bar.md'], {
cwd: 'root',
cwd: '/',
});
});

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

expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './foo.js'], {
cwd: 'root',
cwd: '/',
});
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './bar.md'], {
cwd: 'root',
cwd: '/',
});
});
});
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import createIgnorer from './createIgnorer';
import isSupportedExtension from './isSupportedExtension';

export default (
directory,
currentDirectory,
{
config,
since,
Expand All @@ -14,10 +14,11 @@ export default (
onWriteFile,
} = {}
) => {
const scm = scms(directory);
const scm = scms(currentDirectory);
if (!scm) {
throw new Error('Unable to detect a source control manager.');
}
const directory = scm.rootDirectory;

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

Expand Down
18 changes: 8 additions & 10 deletions src/scms/git.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { statSync } from 'fs';
import { join } from 'path';
import findUp from 'find-up';
import execa from 'execa';
import { dirname } from 'path';

export const name = 'git';

export const detect = directory => {
try {
return !!findUp.sync('.git', { cwd: directory });
} catch (error) {
return false;
const gitDirectory = findUp.sync('.git', { cwd: directory });
if (gitDirectory) {
return dirname(gitDirectory);
}
};

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

export const getSinceRevision = (directory, { staged }) => {
if (staged) {
return 'HEAD';
}
return runGit(directory, ['merge-base', 'HEAD', 'master']).stdout.trim();
const revision = staged
? 'HEAD'
: runGit(directory, ['merge-base', 'HEAD', 'master']).stdout.trim();
return runGit(directory, ['rev-parse', '--short', revision]).stdout.trim();
};

export const getChangedFiles = (directory, revision) => {
Expand Down
5 changes: 3 additions & 2 deletions src/scms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const scms = [gitScm];

export default directory => {
for (const scm of scms) {
if (scm.detect(directory)) {
return scm;
const rootDirectory = scm.detect(directory);
if (rootDirectory) {
return Object.assign({ rootDirectory }, scm);
}
}
};

0 comments on commit 15b3d7e

Please sign in to comment.