Skip to content

Commit 012132b

Browse files
committed
fix(git): use dash-prefixed short opts in CHECKOUT_SHORT_OPTS_WITH_VALUE and replace hasCheckoutForceFlag with extractShortOpts
1 parent 04d1f6c commit 012132b

4 files changed

Lines changed: 23 additions & 82 deletions

File tree

dist/bin/cc-safety-net.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,7 +2563,7 @@ var CHECKOUT_OPTS_WITH_VALUE = new Set([
25632563
"--unified"
25642564
]);
25652565
var CHECKOUT_OPTS_WITH_OPTIONAL_VALUE = new Set(["--recurse-submodules", "--track", "-t"]);
2566-
var CHECKOUT_SHORT_OPTS_WITH_VALUE = new Set(["b", "B", "U"]);
2566+
var CHECKOUT_SHORT_OPTS_WITH_VALUE = new Set(["-b", "-B", "-U"]);
25672567
var SWITCH_SHORT_OPTS_WITH_VALUE = new Set(["-c", "-C"]);
25682568
var CHECKOUT_KNOWN_OPTS_NO_VALUE = new Set([
25692569
"-q",
@@ -2678,7 +2678,10 @@ function extractGitSubcommandAndRest(tokens) {
26782678
}
26792679
function analyzeGitCheckout(tokens) {
26802680
const { index: doubleDashIdx, before: beforeDash } = splitAtDoubleDash(tokens);
2681-
if (hasCheckoutForceFlag(beforeDash)) {
2681+
const shortOpts = extractShortOpts(beforeDash, {
2682+
shortOptsWithValue: CHECKOUT_SHORT_OPTS_WITH_VALUE
2683+
});
2684+
if (beforeDash.includes("--force") || shortOpts.has("-f")) {
26822685
return REASON_CHECKOUT_FORCE;
26832686
}
26842687
for (const token of tokens) {
@@ -2705,30 +2708,6 @@ function analyzeGitCheckout(tokens) {
27052708
}
27062709
return null;
27072710
}
2708-
function hasCheckoutForceFlag(tokens) {
2709-
for (const token of tokens) {
2710-
if (token === "--force") {
2711-
return true;
2712-
}
2713-
if (!token.startsWith("-") || token.startsWith("--") || token === "-") {
2714-
continue;
2715-
}
2716-
const chars = token.slice(1);
2717-
for (let i = 0;i < chars.length; i++) {
2718-
const char = chars[i];
2719-
if (!char || !/[a-zA-Z]/.test(char)) {
2720-
break;
2721-
}
2722-
if (CHECKOUT_SHORT_OPTS_WITH_VALUE.has(char)) {
2723-
break;
2724-
}
2725-
if (char === "f") {
2726-
return true;
2727-
}
2728-
}
2729-
}
2730-
return false;
2731-
}
27322711
function analyzeGitSwitch(tokens) {
27332712
const { before } = splitAtDoubleDash(tokens);
27342713
if (before.includes("--discard-changes")) {

dist/index.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ var CHECKOUT_OPTS_WITH_VALUE = new Set([
14221422
"--unified"
14231423
]);
14241424
var CHECKOUT_OPTS_WITH_OPTIONAL_VALUE = new Set(["--recurse-submodules", "--track", "-t"]);
1425-
var CHECKOUT_SHORT_OPTS_WITH_VALUE = new Set(["b", "B", "U"]);
1425+
var CHECKOUT_SHORT_OPTS_WITH_VALUE = new Set(["-b", "-B", "-U"]);
14261426
var SWITCH_SHORT_OPTS_WITH_VALUE = new Set(["-c", "-C"]);
14271427
var CHECKOUT_KNOWN_OPTS_NO_VALUE = new Set([
14281428
"-q",
@@ -1537,7 +1537,10 @@ function extractGitSubcommandAndRest(tokens) {
15371537
}
15381538
function analyzeGitCheckout(tokens) {
15391539
const { index: doubleDashIdx, before: beforeDash } = splitAtDoubleDash(tokens);
1540-
if (hasCheckoutForceFlag(beforeDash)) {
1540+
const shortOpts = extractShortOpts(beforeDash, {
1541+
shortOptsWithValue: CHECKOUT_SHORT_OPTS_WITH_VALUE
1542+
});
1543+
if (beforeDash.includes("--force") || shortOpts.has("-f")) {
15411544
return REASON_CHECKOUT_FORCE;
15421545
}
15431546
for (const token of tokens) {
@@ -1564,30 +1567,6 @@ function analyzeGitCheckout(tokens) {
15641567
}
15651568
return null;
15661569
}
1567-
function hasCheckoutForceFlag(tokens) {
1568-
for (const token of tokens) {
1569-
if (token === "--force") {
1570-
return true;
1571-
}
1572-
if (!token.startsWith("-") || token.startsWith("--") || token === "-") {
1573-
continue;
1574-
}
1575-
const chars = token.slice(1);
1576-
for (let i = 0;i < chars.length; i++) {
1577-
const char = chars[i];
1578-
if (!char || !/[a-zA-Z]/.test(char)) {
1579-
break;
1580-
}
1581-
if (CHECKOUT_SHORT_OPTS_WITH_VALUE.has(char)) {
1582-
break;
1583-
}
1584-
if (char === "f") {
1585-
return true;
1586-
}
1587-
}
1588-
}
1589-
return false;
1590-
}
15911570
function analyzeGitSwitch(tokens) {
15921571
const { before } = splitAtDoubleDash(tokens);
15931572
if (before.includes("--discard-changes")) {

src/core/rules-git.ts

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const CHECKOUT_OPTS_WITH_VALUE = new Set([
5454
]);
5555

5656
const CHECKOUT_OPTS_WITH_OPTIONAL_VALUE = new Set(['--recurse-submodules', '--track', '-t']);
57-
const CHECKOUT_SHORT_OPTS_WITH_VALUE = new Set(['b', 'B', 'U']);
57+
const CHECKOUT_SHORT_OPTS_WITH_VALUE = new Set(['-b', '-B', '-U']);
5858
const SWITCH_SHORT_OPTS_WITH_VALUE = new Set(['-c', '-C']);
5959

6060
const CHECKOUT_KNOWN_OPTS_NO_VALUE = new Set([
@@ -188,8 +188,11 @@ function extractGitSubcommandAndRest(tokens: readonly string[]): {
188188

189189
function analyzeGitCheckout(tokens: readonly string[]): string | null {
190190
const { index: doubleDashIdx, before: beforeDash } = splitAtDoubleDash(tokens);
191+
const shortOpts = extractShortOpts(beforeDash, {
192+
shortOptsWithValue: CHECKOUT_SHORT_OPTS_WITH_VALUE,
193+
});
191194

192-
if (hasCheckoutForceFlag(beforeDash)) {
195+
if (beforeDash.includes('--force') || shortOpts.has('-f')) {
193196
return REASON_CHECKOUT_FORCE;
194197
}
195198

@@ -222,34 +225,6 @@ function analyzeGitCheckout(tokens: readonly string[]): string | null {
222225
return null;
223226
}
224227

225-
function hasCheckoutForceFlag(tokens: readonly string[]): boolean {
226-
for (const token of tokens) {
227-
if (token === '--force') {
228-
return true;
229-
}
230-
231-
if (!token.startsWith('-') || token.startsWith('--') || token === '-') {
232-
continue;
233-
}
234-
235-
const chars = token.slice(1);
236-
for (let i = 0; i < chars.length; i++) {
237-
const char = chars[i];
238-
if (!char || !/[a-zA-Z]/.test(char)) {
239-
break;
240-
}
241-
if (CHECKOUT_SHORT_OPTS_WITH_VALUE.has(char)) {
242-
break;
243-
}
244-
if (char === 'f') {
245-
return true;
246-
}
247-
}
248-
}
249-
250-
return false;
251-
}
252-
253228
function analyzeGitSwitch(tokens: readonly string[]): string | null {
254229
const { before } = splitAtDoubleDash(tokens);
255230

tests/core/rules-git.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,18 @@ describe('git checkout', () => {
5757
assertAllowed('git checkout -bnew-branch');
5858
});
5959

60+
test('git checkout -bfeature allowed', () => {
61+
assertAllowed('git checkout -bfeature');
62+
});
63+
6064
test('git checkout -Bnew-branch allowed', () => {
6165
assertAllowed('git checkout -Bnew-branch');
6266
});
6367

68+
test('git checkout -qbfeature allowed', () => {
69+
assertAllowed('git checkout -qbfeature');
70+
});
71+
6472
test('git checkout ref pathspec blocked', () => {
6573
assertBlocked('git checkout HEAD file.txt', 'multiple positional args');
6674
});

0 commit comments

Comments
 (0)