66 CommitFilesFromBuffersArgs ,
77 CommitFilesResult ,
88} from "./interface" ;
9+ import { isAbsolute , relative } from "path" ;
910
1011/**
1112 * @see https://isomorphic-git.org/docs/en/walk#walkerentry-mode
@@ -19,14 +20,18 @@ const FILE_MODES = {
1920
2021export const commitChangesFromRepo = async ( {
2122 base,
22- repoDirectory = process . cwd ( ) ,
23+ repoDirectory,
24+ addFromDirectory,
25+ filterFiles,
2326 log,
2427 ...otherArgs
2528} : CommitChangesFromRepoArgs ) : Promise < CommitFilesResult > => {
2629 const ref = base ?. commit ?? "HEAD" ;
30+ const resolvedRepoDirectory =
31+ repoDirectory ?? ( await git . findRoot ( { fs, filepath : process . cwd ( ) } ) ) ;
2732 const gitLog = await git . log ( {
2833 fs,
29- dir : repoDirectory ,
34+ dir : resolvedRepoDirectory ,
3035 ref,
3136 depth : 1 ,
3237 } ) ;
@@ -37,6 +42,19 @@ export const commitChangesFromRepo = async ({
3742 throw new Error ( `Could not determine oid for ${ ref } ` ) ;
3843 }
3944
45+ if ( addFromDirectory && ! isAbsolute ( addFromDirectory ) ) {
46+ throw new Error (
47+ `addFromDirectory must be an absolute path, got ${ addFromDirectory } ` ,
48+ ) ;
49+ }
50+
51+ /**
52+ * The directory to add files from. This is relative to the repository
53+ * root, and is used to filter files.
54+ */
55+ const relativeStartDirectory =
56+ addFromDirectory && relative ( resolvedRepoDirectory , addFromDirectory ) + "/" ;
57+
4058 // Determine changed files
4159 const trees = [ git . TREE ( { ref : oid } ) , git . WORKDIR ( ) ] ;
4260 const additions : CommitFilesFromBuffersArgs [ "fileChanges" ] [ "additions" ] = [ ] ;
@@ -47,14 +65,14 @@ export const commitChangesFromRepo = async ({
4765 } ;
4866 await git . walk ( {
4967 fs,
50- dir : repoDirectory ,
68+ dir : resolvedRepoDirectory ,
5169 trees,
5270 map : async ( filepath , [ commit , workdir ] ) => {
5371 // Don't include ignored files
5472 if (
5573 await git . isIgnored ( {
5674 fs,
57- dir : repoDirectory ,
75+ dir : resolvedRepoDirectory ,
5876 filepath,
5977 } )
6078 ) {
@@ -88,6 +106,17 @@ export const commitChangesFromRepo = async ({
88106 // Iterate through these directories
89107 return true ;
90108 }
109+ if (
110+ relativeStartDirectory &&
111+ ! filepath . startsWith ( relativeStartDirectory )
112+ ) {
113+ // Ignore files that are not in the specified directory
114+ return null ;
115+ }
116+ if ( filterFiles && ! filterFiles ( filepath ) ) {
117+ // Ignore out files that don't match any specified filter
118+ return null ;
119+ }
91120 if ( ! workdir ) {
92121 // File was deleted
93122 deletions . push ( filepath ) ;
0 commit comments