Skip to content

Commit 3287e8f

Browse files
authored
fix: revert "feat: safer handling of partially staged files" (#32)
This reverts commit cc69fb3.
1 parent cc69fb3 commit 3287e8f

File tree

6 files changed

+7
-62
lines changed

6 files changed

+7
-62
lines changed

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ In `package.json`'s `"scripts"` section, add:
7474

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

77-
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.
78-
7977
### `--branch`
8078

8179
When not in `staged` pre-commit mode, use this flag to compare changes with the specified branch. Defaults to `master` (git) / `default` (hg) branch.

bin/pretty-quick.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const prettyQuick = require('..').default;
99

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

12-
let success = true;
1312
prettyQuick(
1413
process.cwd(),
1514
Object.assign({}, args, {
@@ -29,23 +28,10 @@ prettyQuick(
2928
);
3029
},
3130

32-
onPartiallyStagedFile: file => {
33-
console.log(`✗ Found ${chalk.bold('partially')} staged file ${file}.`);
34-
success = false;
35-
},
36-
3731
onWriteFile: file => {
3832
console.log(`✍️ Fixing up ${chalk.bold(file)}.`);
3933
},
4034
})
4135
);
4236

43-
if (success) {
44-
console.log('✅ Everything is awesome!');
45-
} else {
46-
console.log(
47-
'✗ Partially staged files were fixed up.' +
48-
` ${chalk.bold('Please update stage before committing')}.`
49-
);
50-
process.exit(1); // ensure git hooks abort
51-
}
37+
console.log('✅ Everything is awesome!');

src/__tests__/scm-git.test.js

+5-20
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ afterEach(() => {
1111
jest.clearAllMocks();
1212
});
1313

14-
const mockGitFs = (additionalUnstaged = '') => {
14+
const mockGitFs = () => {
1515
mock({
1616
'/.git': {},
17-
'/raz.js': 'raz()',
1817
'/foo.js': 'foo()',
1918
'/bar.md': '# foo',
2019
});
@@ -27,8 +26,8 @@ const mockGitFs = (additionalUnstaged = '') => {
2726
return { stdout: '' };
2827
case 'diff':
2928
return args[2] === '--cached'
30-
? { stdout: './raz.js\n' }
31-
: { stdout: './foo.js\n' + './bar.md\n' + additionalUnstaged };
29+
? { stdout: './foo.js\n' }
30+
: { stdout: './foo.js\n' + './bar.md\n' };
3231
case 'add':
3332
return { stdout: '' };
3433
default:
@@ -152,33 +151,19 @@ describe('with git', () => {
152151
expect(fs.readFileSync('/bar.md', 'utf8')).toEqual('formatted:# foo');
153152
});
154153

155-
test('with --staged stages fully-staged files', () => {
154+
test('with --staged stages staged files', () => {
156155
mockGitFs();
157156

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

160-
expect(execa.sync).toHaveBeenCalledWith('git', ['add', './raz.js'], {
161-
cwd: '/',
162-
});
163-
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './foo.md'], {
159+
expect(execa.sync).toHaveBeenCalledWith('git', ['add', './foo.js'], {
164160
cwd: '/',
165161
});
166162
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './bar.md'], {
167163
cwd: '/',
168164
});
169165
});
170166

171-
test('with --staged does not stage previously partially staged files AND aborts commit', () => {
172-
const additionalUnstaged = './raz.js\n'; // raz.js is partly staged and partly not staged
173-
mockGitFs(additionalUnstaged);
174-
175-
prettyQuick('root', { since: 'banana', staged: true });
176-
177-
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './raz.js'], {
178-
cwd: '/',
179-
});
180-
});
181-
182167
test('without --staged does NOT stage changed files', () => {
183168
mockGitFs();
184169

src/index.js

+1-17
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export default (
1212
branch,
1313
onFoundSinceRevision,
1414
onFoundChangedFiles,
15-
onPartiallyStagedFile,
1615
onWriteFile,
1716
} = {}
1817
) => {
@@ -31,28 +30,13 @@ export default (
3130
.filter(isSupportedExtension)
3231
.filter(createIgnorer(directory));
3332

34-
const unstagedFiles = staged
35-
? scm
36-
.getUnstagedChangedFiles(directory, revision)
37-
.filter(isSupportedExtension)
38-
.filter(createIgnorer(directory))
39-
: [];
40-
41-
const wasFullyStaged = f => unstagedFiles.indexOf(f) < 0;
42-
4333
onFoundChangedFiles && onFoundChangedFiles(changedFiles);
4434

4535
formatFiles(directory, changedFiles, {
4636
config,
4737
onWriteFile: file => {
4838
onWriteFile && onWriteFile(file);
49-
if (staged) {
50-
if (wasFullyStaged(file)) {
51-
scm.stageFile(directory, file);
52-
} else {
53-
onPartiallyStagedFile && onPartiallyStagedFile(file);
54-
}
55-
}
39+
staged && scm.stageFile(directory, file);
5640
},
5741
});
5842
};

src/scms/git.js

-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ export const getChangedFiles = (directory, revision, staged) => {
6161
].filter(Boolean);
6262
};
6363

64-
export const getUnstagedChangedFiles = (directory, revision) => {
65-
return getChangedFiles(directory, revision, false);
66-
};
67-
6864
export const stageFile = (directory, file) => {
6965
runGit(directory, ['add', file]);
7066
};

src/scms/hg.js

-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ export const getChangedFiles = (directory, revision) => {
3535
].filter(Boolean);
3636
};
3737

38-
export const getUnstagedChangedFiles = () => {
39-
return [];
40-
};
41-
4238
export const stageFile = (directory, file) => {
4339
runHg(directory, ['add', file]);
4440
};

0 commit comments

Comments
 (0)