Skip to content

Remove any use of process.cwd(), and clean up directory options across functions #40

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 10, 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
21 changes: 21 additions & 0 deletions .changeset/blue-carpets-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
"@changesets/ghcommit": major
---

Refactor & clean up options for multiple functions

- For `commitFilesFromDirectory`:
- Rename `workingDirectory` to `cwd` for consistency across repos,
and utils like `exec`
- Make `cwd` a required argument
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be OK to make this optional and just default to process.cwd(). In changesets/action we'd still provide it explicitly though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, decided being explicit was probably better anyway, given how easy it is to use process.cwd() if that's what people wanted. 😄

- For `commitChangesFromRepo`:
- Merge `repoDirectory` and `addFromDirectory` into a single required argument
`cwd`. This folder will now both be used to filter which files are added,
and to find the root of the repository.
- Introduce `recursivelyFindRoot` option (default: `true`),
to optionally search for the root of the repository,
by checking for existence of `.git` directory in parent directories,
starting from `cwd`.

This effectively removes all usage of process.cwd() within the package,
instead requiring all usage to be very explicit with specifying paths.
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ In addition to `CommitFilesBasedArgs`, this function has the following arguments

```ts
{
/**
* The directory used to find the repository root,
* and search for changed files to commit.
*
* Any files that have been changed outside of this directory will be ignored.
*/
cwd: string;
/**
* The base commit to build your changes on-top of
*
Expand All @@ -91,21 +98,13 @@ In addition to `CommitFilesBasedArgs`, this function has the following arguments
commit: string;
};
/**
* The root of the repository.
*
* When unspecified, the root of the repository will be found by recursively
* searching for the `.git` directory from the current working directory.
*/
repoDirectory?: string;
/**
* The starting directory to recurse from when detecting changed files.
*
* Useful for monorepos where you want to add files from a specific directory only.
* Don't require {@link cwd} to be the root of the repository,
* and use it as a starting point to recursively search for the `.git`
* directory in parent directories.
*
* Defaults to resolved value of {@link repoDirectory},
* which will add all changed files in the repository.
* @default true
*/
addFromDirectory?: string;
recursivelyFindRoot?: boolean;
/**
* An optional function that can be used to filter which files are included
* in the commit. True should be returned for files that should be included.
Expand All @@ -131,6 +130,7 @@ await commitChangesFromRepo({
...context.repo,
branch: "new-branch-to-create",
message: "[chore] do something",
cwd: process.cwd(),
});

// Commit & push the files from a specific directory
Expand All @@ -141,7 +141,7 @@ await commitChangesFromRepo({
repository: "my-repo",
branch: "another-new-branch-to-create",
message: "[chore] do something else\n\nsome more details",
repoDirectory: "/tmp/some-repo",
cwd: "/tmp/some-repo",
});

// Commit & push the files from the current directory,
Expand All @@ -154,6 +154,7 @@ await commitChangesFromRepo({
headline: "[chore] do something else",
body: "some more details",
},
cwd: process.cwd(),
base: {
// This will be the original sha from the workflow run,
// even if we've made commits locally
Expand All @@ -178,7 +179,7 @@ In addition to `CommitFilesBasedArgs`, this function has the following arguments
* The directory to consider the root of the repository when calculating
* file paths
*/
workingDirectory?: string;
cwd: string;
/**
* The file paths, relative to {@link workingDirectory},
* to add or delete from the branch on GitHub.
Expand Down Expand Up @@ -210,7 +211,7 @@ await commitFilesFromDirectory({
base: {
branch: "main",
},
workingDirectory: "foo/bar",
cwd: "foo/bar",
fileChanges: {
additions: ["package-lock.json", "package.json"],
},
Expand All @@ -226,7 +227,7 @@ await commitFilesFromDirectory({
base: {
tag: "v1.0.0",
},
workingDirectory: "some-dir",
cwd: "some-dir",
fileChanges: {
additions: ["index.html"],
},
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-only-warn": "^1.1.0",
"jest": "^29.7.0",
"mock-cwd": "^1.0.0",
"pino": "^9.3.2",
"pino-pretty": "^11.2.2",
"prettier": "^3.3.3",
Expand Down
32 changes: 0 additions & 32 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import {
} from "./interface.js";

export const commitFilesFromDirectory = async ({
workingDirectory = process.cwd(),
cwd,
fileChanges,
...otherArgs
}: CommitFilesFromDirectoryArgs): Promise<CommitFilesResult> => {
const additions: FileAddition[] = await Promise.all(
(fileChanges.additions || []).map(async (p) => {
return {
path: p,
contents: await fs.readFile(path.join(workingDirectory, p)),
contents: await fs.readFile(path.join(cwd, p)),
};
}),
);
Expand Down
26 changes: 11 additions & 15 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
CommitFilesFromBuffersArgs,
CommitFilesResult,
} from "./interface";
import { isAbsolute, relative } from "path";
import { relative, resolve } from "path";

/**
* @see https://isomorphic-git.org/docs/en/walk#walkerentry-mode
Expand All @@ -20,18 +20,20 @@ const FILE_MODES = {

export const commitChangesFromRepo = async ({
base,
repoDirectory,
addFromDirectory,
cwd: workingDirectory,
recursivelyFindRoot = true,
filterFiles,
log,
...otherArgs
}: CommitChangesFromRepoArgs): Promise<CommitFilesResult> => {
const ref = base?.commit ?? "HEAD";
const resolvedRepoDirectory =
repoDirectory ?? (await git.findRoot({ fs, filepath: process.cwd() }));
const cwd = resolve(workingDirectory);
const repoRoot = recursivelyFindRoot
? await git.findRoot({ fs, filepath: cwd })
: cwd;
const gitLog = await git.log({
fs,
dir: resolvedRepoDirectory,
dir: repoRoot,
ref,
depth: 1,
});
Expand All @@ -42,18 +44,12 @@ export const commitChangesFromRepo = async ({
throw new Error(`Could not determine oid for ${ref}`);
}

if (addFromDirectory && !isAbsolute(addFromDirectory)) {
throw new Error(
`addFromDirectory must be an absolute path, got ${addFromDirectory}`,
);
}

/**
* The directory to add files from. This is relative to the repository
* root, and is used to filter files.
*/
const relativeStartDirectory =
addFromDirectory && relative(resolvedRepoDirectory, addFromDirectory) + "/";
cwd === repoRoot ? null : relative(repoRoot, cwd) + "/";

// Determine changed files
const trees = [git.TREE({ ref: oid }), git.WORKDIR()];
Expand All @@ -65,14 +61,14 @@ export const commitChangesFromRepo = async ({
};
await git.walk({
fs,
dir: resolvedRepoDirectory,
dir: repoRoot,
trees,
map: async (filepath, [commit, workdir]) => {
// Don't include ignored files
if (
await git.isIgnored({
fs,
dir: resolvedRepoDirectory,
dir: repoRoot,
filepath,
})
) {
Expand Down
31 changes: 15 additions & 16 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,27 @@ export interface CommitFilesFromDirectoryArgs
* The directory to consider the root of the repository when calculating
* file paths
*/
workingDirectory?: string;
cwd: string;
/**
* The file paths, relative to {@link workingDirectory},
* The file paths, relative to {@link cwd},
* to add or delete from the branch on GitHub.
*/
fileChanges: {
/** File paths, relative to {@link workingDirectory}, to remove from the repo. */
/** File paths, relative to {@link cwd}, to remove from the repo. */
additions?: string[];
/** File paths, relative to the repository root, to remove from the repo. */
deletions?: string[];
};
}

export interface CommitChangesFromRepoArgs extends CommitFilesBasedArgs {
/**
* The directory used to find the repository root,
* and search for changed files to commit.
*
* Any files that have been changed outside of this directory will be ignored.
*/
cwd: string;
/**
* The base commit to build your changes on-top of.
*
Expand All @@ -105,21 +112,13 @@ export interface CommitChangesFromRepoArgs extends CommitFilesBasedArgs {
commit: string;
};
/**
* The root of the repository.
*
* When unspecified, the root of the repository will be found by recursively
* searching for the `.git` directory from the current working directory.
*/
repoDirectory?: string;
/**
* The starting directory to recurse from when detecting changed files.
*
* Useful for monorepos where you want to add files from a specific directory only.
* Don't require {@link cwd} to be the root of the repository,
* and use it as a starting point to recursively search for the `.git`
* directory in parent directories.
*
* Defaults to resolved value of {@link repoDirectory},
* which will add all changed files in the repository.
* @default true
*/
addFromDirectory?: string;
recursivelyFindRoot?: boolean;
/**
* An optional function that can be used to filter which files are included
* in the commit. True should be returned for files that should be included.
Expand Down
2 changes: 1 addition & 1 deletion src/test/integration/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("fs", () => {
headline: "Test commit",
body: "This is a test commit",
},
workingDirectory: tmpDir,
cwd: tmpDir,
fileChanges: {
additions: ["foo.txt"],
},
Expand Down
Loading