Harden git commit extra flags
- Move extra commit args after message flags to avoid swallowing `-m` - Drop non-flag tokens when tokenizing user-supplied commit flags - Add tests and clarify the settings helper text
This commit is contained in:
1 parent
c3af56e177
commit
77a64493bb
4 files changed
+105
-5
No files matched your search
@@ -12,6 +12,7 @@ import { GitCoreLive } from "./GitCore.ts";
|
||||
import { GitCore, type GitCoreShape } from "../Services/GitCore.ts";
|
||||
import { GitCommandError } from "../Errors.ts";
|
||||
import { type ProcessRunResult, runProcess } from "../../processRunner.ts";
|
||||
import { tokenizeCommitFlags } from "./GitManager.ts";
|
||||
|
||||
// ── Helpers ──
|
||||
|
||||
@@ -1903,4 +1904,90 @@ it.layer(TestLayer)("git integration", (it) => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("tokenizeCommitFlags", () => {
|
||||
it("returns empty array for undefined", () => {
|
||||
expect(tokenizeCommitFlags(undefined)).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns empty array for empty string", () => {
|
||||
expect(tokenizeCommitFlags("")).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns empty array for whitespace-only string", () => {
|
||||
expect(tokenizeCommitFlags(" ")).toEqual([]);
|
||||
});
|
||||
|
||||
it("tokenizes a single flag", () => {
|
||||
expect(tokenizeCommitFlags("--no-gpg-sign")).toEqual(["--no-gpg-sign"]);
|
||||
});
|
||||
|
||||
it("tokenizes multiple flags separated by spaces", () => {
|
||||
expect(tokenizeCommitFlags("--no-gpg-sign --signoff")).toEqual([
|
||||
"--no-gpg-sign",
|
||||
"--signoff",
|
||||
]);
|
||||
});
|
||||
|
||||
it("handles extra whitespace between flags", () => {
|
||||
expect(tokenizeCommitFlags(" --no-gpg-sign --signoff ")).toEqual([
|
||||
"--no-gpg-sign",
|
||||
"--signoff",
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps flags with = values intact", () => {
|
||||
expect(tokenizeCommitFlags("--cleanup=verbatim")).toEqual(["--cleanup=verbatim"]);
|
||||
});
|
||||
|
||||
it("drops tokens that do not start with a dash", () => {
|
||||
expect(tokenizeCommitFlags("--author Foo")).toEqual(["--author"]);
|
||||
});
|
||||
|
||||
it("drops all non-flag tokens from a mixed input", () => {
|
||||
expect(tokenizeCommitFlags("HEAD --no-verify somefile.txt -S")).toEqual([
|
||||
"--no-verify",
|
||||
"-S",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("commit with extraArgs", () => {
|
||||
it.effect("passes extra flags through to git commit", () =>
|
||||
Effect.gen(function* () {
|
||||
const tmp = yield* makeTmpDir();
|
||||
yield* initRepoWithCommit(tmp);
|
||||
const core = yield* GitCore;
|
||||
|
||||
yield* writeTextFile(path.join(tmp, "extra.txt"), "extra args test\n");
|
||||
yield* git(tmp, ["add", "extra.txt"]);
|
||||
|
||||
const { commitSha } = yield* core.commit(tmp, "test extra args", "", [
|
||||
"--no-gpg-sign",
|
||||
]);
|
||||
expect(commitSha).toBeTruthy();
|
||||
|
||||
// Verify the commit was created with the right message
|
||||
const logOutput = yield* git(tmp, ["log", "-1", "--pretty=%s"]);
|
||||
expect(logOutput).toBe("test extra args");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("works without extra args (backward compat)", () =>
|
||||
Effect.gen(function* () {
|
||||
const tmp = yield* makeTmpDir();
|
||||
yield* initRepoWithCommit(tmp);
|
||||
const core = yield* GitCore;
|
||||
|
||||
yield* writeTextFile(path.join(tmp, "compat.txt"), "compat test\n");
|
||||
yield* git(tmp, ["add", "compat.txt"]);
|
||||
|
||||
const { commitSha } = yield* core.commit(tmp, "no extra args", "");
|
||||
expect(commitSha).toBeTruthy();
|
||||
|
||||
const logOutput = yield* git(tmp, ["log", "-1", "--pretty=%s"]);
|
||||
expect(logOutput).toBe("no extra args");
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -806,11 +806,14 @@ const makeGitCore = Effect.gen(function* () {
|
||||
|
||||
const commit: GitCoreShape["commit"] = (cwd, subject, body, extraArgs = []) =>
|
||||
Effect.gen(function* () {
|
||||
const args = ["commit", ...extraArgs, "-m", subject];
|
||||
const args = ["commit", "-m", subject];
|
||||
const trimmedBody = body.trim();
|
||||
if (trimmedBody.length > 0) {
|
||||
args.push("-m", trimmedBody);
|
||||
}
|
||||
// Append extra flags after the message args so an argument-consuming
|
||||
// flag (e.g. `-C`) cannot accidentally swallow `-m` or the subject.
|
||||
args.push(...extraArgs);
|
||||
yield* runGit("GitCore.commit.commit", cwd, args);
|
||||
const commitSha = yield* runGitStdout("GitCore.commit.revParseHead", cwd, [
|
||||
"rev-parse",
|
||||
|
||||
@@ -207,11 +207,21 @@ interface CommitAndBranchSuggestion {
|
||||
commitMessage: string;
|
||||
}
|
||||
|
||||
function tokenizeCommitFlags(rawFlags?: string): string[] {
|
||||
return (rawFlags ?? "")
|
||||
/**
|
||||
* Tokenize and sanitise user-provided extra flags for `git commit`.
|
||||
*
|
||||
* Each token must look like a CLI flag (start with `-`). Tokens that don't
|
||||
* start with a dash are silently dropped so that a malformed value like
|
||||
* `--author "Foo"` (which the naive split turns into `["--author", "Foo"]`)
|
||||
* cannot inject a positional argument into the git invocation.
|
||||
*/
|
||||
export function tokenizeCommitFlags(rawFlags?: string): string[] {
|
||||
const tokens = (rawFlags ?? "")
|
||||
.trim()
|
||||
.split(/\s+/g)
|
||||
.filter((value) => value.length > 0);
|
||||
|
||||
return tokens.filter((token) => token.startsWith("-"));
|
||||
}
|
||||
|
||||
function formatCommitMessage(subject: string, body: string): string {
|
||||
|
||||
@@ -567,8 +567,8 @@ function SettingsRouteView() {
|
||||
spellCheck={false}
|
||||
/>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
Applied to app-run git commit commands only. Example: <code>--no-gpg-sign</code>
|
||||
. Quoted arguments are not supported yet.
|
||||
Applied to app-run git commit commands only. Example:{" "}
|
||||
<code>--no-gpg-sign</code>. Quoted arguments are not supported yet.
|
||||
</span>
|
||||
</label>
|
||||
|
||||
|
||||
Reference in new issue
Block a user