Skip to content

Explicitly throw errors when executable files are found #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lazy-icons-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/ghcommit": minor
---

Throw an error when executable files are encountered
5 changes: 5 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export const commitChangesFromRepo = async ({
`Unexpected symlink at ${filepath}, GitHub API only supports files and directories. You may need to add this file to .gitignore`,
);
}
if ((await workdir?.mode()) === FILE_MODES.executableFile) {
throw new Error(
`Unexpected executable file at ${filepath}, GitHub API only supports non-executable files and directories. You may need to add this file to .gitignore`,
);
}
const prevOid = await commit?.oid();
const currentOid = await workdir?.oid();
// Don't include files that haven't changed, and exist in both trees
Expand Down
57 changes: 57 additions & 0 deletions src/test/integration/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const makeFileChanges = async (
repoDirectory: string,
changegroup:
| "standard"
| "with-executable-file"
| "with-ignored-symlink"
| "with-included-valid-symlink"
| "with-included-invalid-symlink",
Expand Down Expand Up @@ -120,6 +121,17 @@ const makeFileChanges = async (
path.join(repoDirectory, "coverage", "foo", "bar"),
"This file should be ignored",
);
if (changegroup === "with-executable-file") {
// Add an executable file
await fs.promises.writeFile(
path.join(repoDirectory, "executable-file.sh"),
"#!/bin/bash\necho hello",
);
await fs.promises.chmod(
path.join(repoDirectory, "executable-file.sh"),
0o755,
);
}
if (changegroup === "with-ignored-symlink") {
// node_modules is ignored in this repo
await fs.promises.mkdir(path.join(repoDirectory, "node_modules"), {
Expand Down Expand Up @@ -341,6 +353,51 @@ describe("git", () => {
});
});

it(`should throw appropriate error when executable file is present`, async () => {
const branch = `${TEST_BRANCH_PREFIX}-executable-file`;
branches.push(branch);

await fs.promises.mkdir(testDir, { recursive: true });
const repoDirectory = path.join(testDir, `repo-executable-file`);

// Clone the git repo locally using the git cli and child-process
await new Promise<void>((resolve, reject) => {
const p = execFile(
"git",
["clone", process.cwd(), `repo-executable-file`],
{ cwd: testDir },
(error) => {
if (error) {
reject(error);
} else {
resolve();
}
},
);
p.stdout?.pipe(process.stdout);
p.stderr?.pipe(process.stderr);
});

await makeFileChanges(repoDirectory, "with-executable-file");

// Push the changes
await expect(() =>
commitChangesFromRepo({
octokit,
...REPO,
branch,
message: {
headline: "Test commit",
body: "This is a test commit",
},
repoDirectory,
log,
}),
).rejects.toThrow(
"Unexpected executable file at executable-file.sh, GitHub API only supports non-executable files and directories. You may need to add this file to .gitignore",
);
});

it("should correctly be able to base changes off specific commit", async () => {
const branch = `${TEST_BRANCH_PREFIX}-specific-base`;
branches.push(branch);
Expand Down