fix(tooling): commit hook-restaged file contents

This commit is contained in:
Vincent Koc
2026-04-12 05:19:13 +01:00
parent a45c4bebc5
commit 0acfa47e08
2 changed files with 28 additions and 2 deletions

View File

@@ -216,11 +216,11 @@ fi
committed=false
if [ "$fast_commit" = true ]; then
declare -a commit_env=(FAST_COMMIT=1)
if run_git_with_lock_retry "commit" env "${commit_env[@]}" git commit -m "$commit_message" -- "${files[@]}"; then
if run_git_with_lock_retry "commit" env "${commit_env[@]}" git commit -m "$commit_message"; then
committed=true
fi
else
if run_git_with_lock_retry "commit" git commit -m "$commit_message" -- "${files[@]}"; then
if run_git_with_lock_retry "commit" git commit -m "$commit_message"; then
committed=true
fi
fi

View File

@@ -60,6 +60,10 @@ function committedPaths(repo: string) {
return output.split("\n").filter(Boolean).toSorted();
}
function committedFileContents(repo: string, relativePath: string) {
return git(repo, "show", `HEAD:${relativePath}`);
}
describe("scripts/committer", () => {
it("accepts supported path argument shapes", () => {
const cases = [
@@ -161,6 +165,28 @@ describe("scripts/committer", () => {
expect(committedPaths(repo)).toEqual(["note.txt"]);
});
it("commits the hook-restaged file contents and leaves the tree clean", () => {
const repo = createRepo();
installHook(
repo,
".githooks/pre-commit",
[
"#!/usr/bin/env bash",
"set -euo pipefail",
"printf 'formatted\\n' > note.txt",
"git add note.txt",
].join("\n") + "\n",
);
writeRepoFile(repo, "note.txt", "raw\n");
const output = commitWithHelperArgs(repo, "test: hook rewrite", "note.txt");
expect(output).toContain('Committed "test: hook rewrite" with 1 files');
expect(committedPaths(repo)).toEqual(["note.txt"]);
expect(committedFileContents(repo, "note.txt")).toBe("formatted");
expect(git(repo, "status", "--short", "--untracked-files=no")).toBe("");
});
it("prints usage for --help", () => {
const repo = createRepo();