Skip to content

Commit a8122de

Browse files
committed
Remove recursivelyFindRoot and make cwd optional
1 parent 165c6cb commit a8122de

4 files changed

Lines changed: 31 additions & 77 deletions

File tree

.changeset/kind-spoons-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/ghcommit": major
3+
---
4+
5+
Remove `recursivelyFindRoot` and make `cwd` optional for `commitChangesSinceBase`. Files will also not be filtered by `cwd` by default. To only commit files from a directory, use the `filterFiles` option instead.

src/git.ts

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,21 @@ import { resolveGitRef } from "./utils.ts";
2121
* Works in Node.js only.
2222
*/
2323
export async function commitChangesSinceBase({
24-
cwd: workingDirectory,
25-
recursivelyFindRoot = true,
24+
cwd,
2625
filterFiles,
2726
...otherArgs
2827
}: CommitChangesSinceBaseOptions): Promise<CommitChangesResult> {
29-
const ref = resolveGitRef(otherArgs.base ?? { commit: "HEAD" });
30-
const cwd = path.resolve(workingDirectory);
31-
const repoRoot = recursivelyFindRoot ? await findGitRoot(cwd) : cwd;
28+
cwd = path.resolve(cwd ?? process.cwd());
3229

33-
const refSha = await getShaForRef(repoRoot, ref);
30+
const ref = resolveGitRef(otherArgs.base ?? { commit: "HEAD" });
31+
const refSha = await getShaForRef(cwd, ref);
3432
if (!refSha) {
3533
throw new Error(`Could not determine sha for ref ${ref}`);
3634
}
3735

3836
return await commitChanges({
3937
...otherArgs,
40-
fileChanges: await getFileChanges(
41-
workingDirectory,
42-
repoRoot,
43-
refSha,
44-
filterFiles,
45-
),
38+
fileChanges: await getFileChanges(cwd, refSha, filterFiles),
4639
base: {
4740
commit: refSha,
4841
},
@@ -52,30 +45,17 @@ export async function commitChangesSinceBase({
5245
// Exported for testing only
5346
export async function getFileChanges(
5447
cwd: string,
55-
repoRoot: string,
5648
ref: string,
5749
filterFiles?: CommitChangesSinceBaseOptions["filterFiles"],
5850
): Promise<CommitChangesOptions["fileChanges"]> {
59-
/**
60-
* The directory to add files from. This is relative to the repository
61-
* root, and is used to filter files.
62-
*/
63-
const relativeStartDirectory =
64-
cwd === repoRoot ? null : path.relative(repoRoot, cwd) + "/";
51+
const repoRoot = await findGitRoot(cwd);
6552

6653
const additions: CommitChangesOptions["fileChanges"]["additions"] = [];
6754
const deletions: CommitChangesOptions["fileChanges"]["deletions"] = [];
6855

6956
const addPath = async (filePath: string) => {
70-
if (
71-
relativeStartDirectory &&
72-
!filePath.startsWith(relativeStartDirectory)
73-
) {
74-
return;
75-
}
76-
if (filterFiles && !filterFiles(filePath)) {
77-
return;
78-
}
57+
if (filterFiles && !filterFiles(filePath)) return;
58+
7959
const resolvedFilePath = path.join(repoRoot, filePath);
8060
const stat = await fs.lstat(resolvedFilePath);
8161
if (stat.isSymbolicLink()) {
@@ -97,15 +77,7 @@ export async function getFileChanges(
9777
};
9878

9979
const deletePath = (filePath: string) => {
100-
if (
101-
relativeStartDirectory &&
102-
!filePath.startsWith(relativeStartDirectory)
103-
) {
104-
return;
105-
}
106-
if (filterFiles && !filterFiles(filePath)) {
107-
return;
108-
}
80+
if (filterFiles && !filterFiles(filePath)) return;
10981

11082
deletions.push({ path: filePath });
11183
};

src/types.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,16 @@ export interface CommitChangesSinceBaseOptions extends Omit<
9393
*/
9494
base?: GitRef;
9595
/**
96-
* The directory to execute git commands in. And any changes outside of this
97-
* directory (but within the repository) is ignored.
98-
*/
99-
cwd: string;
100-
/**
101-
* Don't require {@link cwd} to be the root of the repository,
102-
* and use it as a starting point to recursively search for the `.git`
103-
* directory in parent directories.
96+
* The directory to execute git commands in. This can be set if the repository
97+
* is in a different directory.
10498
*
105-
* @default true
99+
* @default process.cwd()
106100
*/
107-
recursivelyFindRoot?: boolean;
101+
cwd?: string;
108102
/**
109-
* An optional function that can be used to filter which files are included
110-
* in the commit. True should be returned for files that should be included.
103+
* Filter the files to be included in the commit. The file paths are relative
104+
* to the repository root, e.g. `"src/index.ts"`. Return `true` to include
105+
* the file.
111106
*
112107
* By default, all files are included.
113108
*/

tests/git.test.ts

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe("getFileChanges", () => {
4444
await fixture.writeFile("ignored/file.txt", "This file should be ignored");
4545
await fixture.writeFile(".env", "This file should be ignored");
4646

47-
const result = await getFileChanges(fixture.path, fixture.path, "HEAD");
47+
const result = await getFileChanges(fixture.path, "HEAD");
4848
expect(result).toEqual({
4949
additions: [
5050
{
@@ -75,11 +75,7 @@ describe("getFileChanges", () => {
7575
});
7676
await fixture.writeFile("b.txt", "This is a new file!");
7777

78-
const result = await getFileChanges(
79-
fixture.path,
80-
fixture.path,
81-
"refs/heads/new-branch",
82-
);
78+
const result = await getFileChanges(fixture.path, "refs/heads/new-branch");
8379
expect(result).toEqual({
8480
additions: [
8581
{
@@ -102,11 +98,7 @@ describe("getFileChanges", () => {
10298
});
10399
await fixture.writeFile("b.txt", "This is a new file!");
104100

105-
const result = await getFileChanges(
106-
fixture.path,
107-
fixture.path,
108-
"refs/tags/v1.0.0",
109-
);
101+
const result = await getFileChanges(fixture.path, "refs/tags/v1.0.0");
110102
expect(result).toEqual({
111103
additions: [
112104
{
@@ -132,7 +124,7 @@ describe("getFileChanges", () => {
132124

133125
await fixture.writeFile("b.txt", "This is a new file!");
134126

135-
const result = await getFileChanges(fixture.path, fixture.path, commitSha);
127+
const result = await getFileChanges(fixture.path, commitSha);
136128
expect(result).toEqual({
137129
additions: [
138130
{
@@ -157,7 +149,6 @@ describe("getFileChanges", () => {
157149
await fixture.writeFile("nested/bar.txt", "This is a new file!");
158150

159151
const result = await getFileChanges(
160-
fixture.path,
161152
fixture.path,
162153
"HEAD",
163154
// Only include top-level files
@@ -188,7 +179,6 @@ describe("getFileChanges", () => {
188179

189180
const result = await getFileChanges(
190181
path.join(fixture.path, "nested"),
191-
fixture.path,
192182
"HEAD",
193183
);
194184
expect(result).toEqual({
@@ -220,7 +210,7 @@ describe("getFileChanges", () => {
220210
});
221211

222212
// Since we committed, HEAD points to the last commit and there's no change since then
223-
const result = await getFileChanges(fixture.path, fixture.path, "HEAD");
213+
const result = await getFileChanges(fixture.path, "HEAD");
224214
expect(result).toEqual({ additions: [], deletions: [] });
225215

226216
await fixture.rm("some-dir/nested");
@@ -230,9 +220,7 @@ describe("getFileChanges", () => {
230220
);
231221

232222
// We made symlink changes since the last commit, so this should error now
233-
await expect(
234-
getFileChanges(fixture.path, fixture.path, "HEAD"),
235-
).rejects.toThrow(
223+
await expect(getFileChanges(fixture.path, "HEAD")).rejects.toThrow(
236224
"Unexpected symlink at some-dir/nested, GitHub API only supports files and directories. You may need to add this file to .gitignore",
237225
);
238226
});
@@ -255,7 +243,7 @@ describe("getFileChanges", () => {
255243
fixture.getPath("some-dir/nested"),
256244
);
257245

258-
const result = await getFileChanges(fixture.path, fixture.path, "HEAD");
246+
const result = await getFileChanges(fixture.path, "HEAD");
259247
expect(result).toEqual({ additions: [], deletions: [] });
260248
});
261249

@@ -269,9 +257,7 @@ describe("getFileChanges", () => {
269257
fixture.getPath("some-dir/nested"),
270258
);
271259

272-
await expect(
273-
getFileChanges(fixture.path, fixture.path, "HEAD"),
274-
).rejects.toThrow(
260+
await expect(getFileChanges(fixture.path, "HEAD")).rejects.toThrow(
275261
"Unexpected symlink at some-dir/nested, GitHub API only supports files and directories. You may need to add this file to .gitignore",
276262
);
277263
});
@@ -288,9 +274,7 @@ describe("getFileChanges", () => {
288274
fixture.getPath("some-dir/nested"),
289275
);
290276

291-
await expect(
292-
getFileChanges(fixture.path, fixture.path, "HEAD"),
293-
).rejects.toThrow(
277+
await expect(getFileChanges(fixture.path, "HEAD")).rejects.toThrow(
294278
"Unexpected symlink at some-dir/nested, GitHub API only supports files and directories. You may need to add this file to .gitignore",
295279
);
296280
});
@@ -302,9 +286,7 @@ describe("getFileChanges", () => {
302286
await fixture.writeFile("executable-file.sh", "#!/bin/bash\necho hello");
303287
await fs.chmod(fixture.getPath("executable-file.sh"), 0o755);
304288

305-
await expect(
306-
getFileChanges(fixture.path, fixture.path, "HEAD"),
307-
).rejects.toThrow(
289+
await expect(getFileChanges(fixture.path, "HEAD")).rejects.toThrow(
308290
"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",
309291
);
310292
});

0 commit comments

Comments
 (0)