Skip to content

Commit

Permalink
fix: revert "feat: safer handling of partially staged files" (#32)
Browse files Browse the repository at this point in the history
This reverts commit cc69fb3.
  • Loading branch information
azz committed May 21, 2018
1 parent cc69fb3 commit 3287e8f
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 62 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ In `package.json`'s `"scripts"` section, add:

Pre-commit mode. Under this flag only staged files will be formatted, and they will be re-staged after formatting.

Partially staged files will not be re-staged after formatting and pretty-quick will exit with a non-zero exit code. The intent is to abort the git commit and allow the user to amend their selective staging to include formatting fixes.

### `--branch`

When not in `staged` pre-commit mode, use this flag to compare changes with the specified branch. Defaults to `master` (git) / `default` (hg) branch.
Expand Down
16 changes: 1 addition & 15 deletions bin/pretty-quick.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const prettyQuick = require('..').default;

const args = mri(process.argv.slice(2));

let success = true;
prettyQuick(
process.cwd(),
Object.assign({}, args, {
Expand All @@ -29,23 +28,10 @@ prettyQuick(
);
},

onPartiallyStagedFile: file => {
console.log(`✗ Found ${chalk.bold('partially')} staged file ${file}.`);
success = false;
},

onWriteFile: file => {
console.log(`✍️ Fixing up ${chalk.bold(file)}.`);
},
})
);

if (success) {
console.log('✅ Everything is awesome!');
} else {
console.log(
'✗ Partially staged files were fixed up.' +
` ${chalk.bold('Please update stage before committing')}.`
);
process.exit(1); // ensure git hooks abort
}
console.log('✅ Everything is awesome!');
25 changes: 5 additions & 20 deletions src/__tests__/scm-git.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ afterEach(() => {
jest.clearAllMocks();
});

const mockGitFs = (additionalUnstaged = '') => {
const mockGitFs = () => {
mock({
'/.git': {},
'/raz.js': 'raz()',
'/foo.js': 'foo()',
'/bar.md': '# foo',
});
Expand All @@ -27,8 +26,8 @@ const mockGitFs = (additionalUnstaged = '') => {
return { stdout: '' };
case 'diff':
return args[2] === '--cached'
? { stdout: './raz.js\n' }
: { stdout: './foo.js\n' + './bar.md\n' + additionalUnstaged };
? { stdout: './foo.js\n' }
: { stdout: './foo.js\n' + './bar.md\n' };
case 'add':
return { stdout: '' };
default:
Expand Down Expand Up @@ -152,33 +151,19 @@ describe('with git', () => {
expect(fs.readFileSync('/bar.md', 'utf8')).toEqual('formatted:# foo');
});

test('with --staged stages fully-staged files', () => {
test('with --staged stages staged files', () => {
mockGitFs();

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

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

test('with --staged does not stage previously partially staged files AND aborts commit', () => {
const additionalUnstaged = './raz.js\n'; // raz.js is partly staged and partly not staged
mockGitFs(additionalUnstaged);

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

expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './raz.js'], {
cwd: '/',
});
});

test('without --staged does NOT stage changed files', () => {
mockGitFs();

Expand Down
18 changes: 1 addition & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default (
branch,
onFoundSinceRevision,
onFoundChangedFiles,
onPartiallyStagedFile,
onWriteFile,
} = {}
) => {
Expand All @@ -31,28 +30,13 @@ export default (
.filter(isSupportedExtension)
.filter(createIgnorer(directory));

const unstagedFiles = staged
? scm
.getUnstagedChangedFiles(directory, revision)
.filter(isSupportedExtension)
.filter(createIgnorer(directory))
: [];

const wasFullyStaged = f => unstagedFiles.indexOf(f) < 0;

onFoundChangedFiles && onFoundChangedFiles(changedFiles);

formatFiles(directory, changedFiles, {
config,
onWriteFile: file => {
onWriteFile && onWriteFile(file);
if (staged) {
if (wasFullyStaged(file)) {
scm.stageFile(directory, file);
} else {
onPartiallyStagedFile && onPartiallyStagedFile(file);
}
}
staged && scm.stageFile(directory, file);
},
});
};
4 changes: 0 additions & 4 deletions src/scms/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ export const getChangedFiles = (directory, revision, staged) => {
].filter(Boolean);
};

export const getUnstagedChangedFiles = (directory, revision) => {
return getChangedFiles(directory, revision, false);
};

export const stageFile = (directory, file) => {
runGit(directory, ['add', file]);
};
4 changes: 0 additions & 4 deletions src/scms/hg.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ export const getChangedFiles = (directory, revision) => {
].filter(Boolean);
};

export const getUnstagedChangedFiles = () => {
return [];
};

export const stageFile = (directory, file) => {
runHg(directory, ['add', file]);
};

0 comments on commit 3287e8f

Please sign in to comment.