Skip to content

Commit e2b975d

Browse files
authored
Remove recursivelyFindRoot and make cwd optional for commitChangesSinceBase (#118)
* Remove recursivelyFindRoot and make cwd optional * Fix test
1 parent 165c6cb commit e2b975d

4 files changed

Lines changed: 32 additions & 105 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: 10 additions & 38 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
};
@@ -171,7 +143,7 @@ async function findGitRoot(cwd: string): Promise<string> {
171143
throwOnError: true,
172144
nodeOptions: { cwd },
173145
});
174-
return path.resolve(cwd, stdout.trim());
146+
return path.dirname(path.resolve(cwd, stdout.trim()));
175147
} catch {
176148
return cwd;
177149
}

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 & 55 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
@@ -174,34 +165,6 @@ describe("getFileChanges", () => {
174165
});
175166
});
176167

177-
it("should filter files when running in a repository sub-directory", async () => {
178-
await using fixture = await createFixture({
179-
"foo.txt": "Hello, world!",
180-
"nested/foo.txt": "Hello, world!",
181-
});
182-
await setupGit(fixture.path);
183-
184-
await fixture.rm("foo.txt");
185-
await fixture.rm("nested/foo.txt");
186-
await fixture.writeFile("bar.txt", "This is a new file!");
187-
await fixture.writeFile("nested/bar.txt", "This is a new file!");
188-
189-
const result = await getFileChanges(
190-
path.join(fixture.path, "nested"),
191-
fixture.path,
192-
"HEAD",
193-
);
194-
expect(result).toEqual({
195-
additions: [
196-
{
197-
path: "nested/bar.txt",
198-
contents: await fixture.readFile("nested/bar.txt", "base64"),
199-
},
200-
],
201-
deletions: [{ path: "nested/foo.txt" }],
202-
});
203-
});
204-
205168
it("should allow existing symlinks", async () => {
206169
await using fixture = await createFixture({
207170
"foo.txt": "Hello, world!",
@@ -220,7 +183,7 @@ describe("getFileChanges", () => {
220183
});
221184

222185
// 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");
186+
const result = await getFileChanges(fixture.path, "HEAD");
224187
expect(result).toEqual({ additions: [], deletions: [] });
225188

226189
await fixture.rm("some-dir/nested");
@@ -230,9 +193,7 @@ describe("getFileChanges", () => {
230193
);
231194

232195
// 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(
196+
await expect(getFileChanges(fixture.path, "HEAD")).rejects.toThrow(
236197
"Unexpected symlink at some-dir/nested, GitHub API only supports files and directories. You may need to add this file to .gitignore",
237198
);
238199
});
@@ -255,7 +216,7 @@ describe("getFileChanges", () => {
255216
fixture.getPath("some-dir/nested"),
256217
);
257218

258-
const result = await getFileChanges(fixture.path, fixture.path, "HEAD");
219+
const result = await getFileChanges(fixture.path, "HEAD");
259220
expect(result).toEqual({ additions: [], deletions: [] });
260221
});
261222

@@ -269,9 +230,7 @@ describe("getFileChanges", () => {
269230
fixture.getPath("some-dir/nested"),
270231
);
271232

272-
await expect(
273-
getFileChanges(fixture.path, fixture.path, "HEAD"),
274-
).rejects.toThrow(
233+
await expect(getFileChanges(fixture.path, "HEAD")).rejects.toThrow(
275234
"Unexpected symlink at some-dir/nested, GitHub API only supports files and directories. You may need to add this file to .gitignore",
276235
);
277236
});
@@ -288,9 +247,7 @@ describe("getFileChanges", () => {
288247
fixture.getPath("some-dir/nested"),
289248
);
290249

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

305-
await expect(
306-
getFileChanges(fixture.path, fixture.path, "HEAD"),
307-
).rejects.toThrow(
262+
await expect(getFileChanges(fixture.path, "HEAD")).rejects.toThrow(
308263
"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",
309264
);
310265
});

0 commit comments

Comments
 (0)