Skip to content

Commit a81715e

Browse files
committed
Allow for easier committing from subdirectories
fixes #29
1 parent 21c9eaf commit a81715e

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

.changeset/mean-trainers-develop.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@changesets/ghcommit": minor
3+
---
4+
5+
Introduce `addDirectory` option for `commitChangesFromRepo` to allow users to
6+
specify a subdirectory of the git repository that should be used to add files
7+
from, rather then adding all changed files.
8+
9+
This is useful when trying to emulate the behavior of running `git add .`
10+
from a subdirectory of the repository.

.changeset/old-kings-collect.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@changesets/ghcommit": minor
3+
---
4+
5+
Automatically find root in `commitChangesFromRepo`
6+
when `repoDirectory` is unspecified.
7+
8+
While this does result in a behavioral change for an existing argument,
9+
it's considered non-breaking as before `commitChangesFromRepo` would just not
10+
work when run from a subdirectory of a repo when `repoDirectory` was not
11+
specified.

src/git.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ const FILE_MODES = {
1919

2020
export const commitChangesFromRepo = async ({
2121
base,
22-
repoDirectory = process.cwd(),
22+
repoDirectory,
23+
addDirectory,
2324
log,
2425
...otherArgs
2526
}: CommitChangesFromRepoArgs): Promise<CommitFilesResult> => {
2627
const ref = base?.commit ?? "HEAD";
28+
const resolvedRepoDirectory =
29+
repoDirectory ?? (await git.findRoot({ fs, filepath: process.cwd() }));
2730
const gitLog = await git.log({
2831
fs,
29-
dir: repoDirectory,
32+
dir: resolvedRepoDirectory,
3033
ref,
3134
depth: 1,
3235
});
@@ -47,14 +50,14 @@ export const commitChangesFromRepo = async ({
4750
};
4851
await git.walk({
4952
fs,
50-
dir: repoDirectory,
53+
dir: addDirectory ?? resolvedRepoDirectory,
5154
trees,
5255
map: async (filepath, [commit, workdir]) => {
5356
// Don't include ignored files
5457
if (
5558
await git.isIgnored({
5659
fs,
57-
dir: repoDirectory,
60+
dir: resolvedRepoDirectory,
5861
filepath,
5962
})
6063
) {

src/interface.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,17 @@ export interface CommitChangesFromRepoArgs extends CommitFilesBasedArgs {
107107
/**
108108
* The root of the repository.
109109
*
110-
* @default process.cwd()
110+
* When unspecified, the root of the repository will be found by recursively
111+
* searching for the `.git` directory from the current working directory.
111112
*/
112113
repoDirectory?: string;
114+
/**
115+
* Which directory to add the files from.
116+
*
117+
* Useful for monorepos where you want to add files from a specific directory only.
118+
*
119+
* Defaults to resolved value as {@link repoDirectory},
120+
* which will add all changed files in the repository.
121+
*/
122+
addDirectory?: string;
113123
}

0 commit comments

Comments
 (0)