Skip to content

Commit 7cd18c3

Browse files
committed
Make ESLint report fatal error for files with dirty index
1 parent 189f8ae commit 7cd18c3

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/git.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ const getGitFileList = (): string[] => {
8181
return gitFileListCache;
8282
};
8383

84+
const hasCleanIndex = (filePath: string): boolean => {
85+
const command = [
86+
"git",
87+
"diff",
88+
"--quiet",
89+
"--relative",
90+
"--unified=0",
91+
"--",
92+
sanitizeFilePath(filePath),
93+
].join(" ");
94+
95+
let result = true;
96+
try {
97+
child_process.execSync(command).toString();
98+
} catch (err: unknown) {
99+
result = false;
100+
}
101+
102+
return result;
103+
};
104+
84105
const isHunkHeader = (input: string) => {
85106
const hunkHeaderRE = new RegExp(/^@@ [^@]* @@/);
86107
return hunkHeaderRE.exec(input);
@@ -131,5 +152,11 @@ const getRangesForDiff = (diff: string): Range[] =>
131152
.map(getRangeForChangedLines)
132153
.filter(removeNullRanges);
133154

134-
export { getDiffForFile, getRangesForDiff, getDiffFileList, getGitFileList };
155+
export {
156+
getDiffForFile,
157+
getRangesForDiff,
158+
getDiffFileList,
159+
getGitFileList,
160+
hasCleanIndex,
161+
};
135162
export type { Range };

src/processors.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type { Linter } from "eslint";
22
import type { Range } from "./git";
3-
import { getDiffFileList, getDiffForFile, getRangesForDiff } from "./git";
3+
import {
4+
getDiffFileList,
5+
getDiffForFile,
6+
getRangesForDiff,
7+
hasCleanIndex,
8+
} from "./git";
49

510
const STAGED = true;
611

@@ -27,6 +32,21 @@ const getPostProcessor = (staged = false) => (
2732
messages: Linter.LintMessage[][],
2833
filename: string
2934
): Linter.LintMessage[] => {
35+
if (staged && !hasCleanIndex(filename)) {
36+
const fatal = true;
37+
const message = `${filename} has unstaged changes. Please stage or remove the changes.`;
38+
const severity: Linter.Severity = 2;
39+
const fatalError: Linter.LintMessage = {
40+
fatal,
41+
message,
42+
severity,
43+
column: 0,
44+
line: 0,
45+
ruleId: null,
46+
};
47+
return [fatalError];
48+
}
49+
3050
return messages
3151
.map((message) => {
3252
const filteredMessage = message.filter(({ fatal, line }) => {

0 commit comments

Comments
 (0)