Skip to content

Commit 604d0d7

Browse files
committed
Allow for easier committing from subdirectories
fixes #29
1 parent 15cdc1b commit 604d0d7

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
@@ -9,14 +9,17 @@ import {
99

1010
export const commitChangesFromRepo = async ({
1111
base,
12-
repoDirectory = process.cwd(),
12+
repoDirectory,
13+
addDirectory,
1314
log,
1415
...otherArgs
1516
}: CommitChangesFromRepoArgs): Promise<CommitFilesResult> => {
1617
const ref = base?.commit ?? "HEAD";
18+
const resolvedRepoDirectory =
19+
repoDirectory ?? (await git.findRoot({ fs, filepath: process.cwd() }));
1720
const gitLog = await git.log({
1821
fs,
19-
dir: repoDirectory,
22+
dir: resolvedRepoDirectory,
2023
ref,
2124
depth: 1,
2225
});
@@ -37,7 +40,7 @@ export const commitChangesFromRepo = async ({
3740
};
3841
await git.walk({
3942
fs,
40-
dir: repoDirectory,
43+
dir: addDirectory ?? resolvedRepoDirectory,
4144
trees,
4245
map: async (filepath, [commit, workdir]) => {
4346
const prevOid = await commit?.oid();
@@ -50,7 +53,7 @@ export const commitChangesFromRepo = async ({
5053
if (
5154
await git.isIgnored({
5255
fs,
53-
dir: repoDirectory,
56+
dir: resolvedRepoDirectory,
5457
filepath,
5558
})
5659
) {

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)