@@ -29,13 +29,7 @@ const TEMPLATE_DIRECTORIES = [
2929 "docs/PULL_REQUEST_TEMPLATE" ,
3030] as const ;
3131
32- // Recursive listings can exhaust the byte cap on unrelated documents and hide a valid template.
33- const TREE_PATHS = [
34- "." ,
35- ".github/" ,
36- "docs/" ,
37- ...TEMPLATE_DIRECTORIES . map ( ( directory ) => `${ directory } /` ) ,
38- ] ;
32+ const TEMPLATE_LOCATIONS = [ ".github/" , "" , "docs/" ] as const ;
3933
4034type ExecuteGit = SourceControlHost . SourceControlHost [ "Service" ] [ "git" ] [ "execute" ] ;
4135
@@ -145,33 +139,51 @@ export const detect = Effect.fn("gitHubChangeRequestTemplate.detect")(function*
145139 return yield * Effect . gen ( function * ( ) {
146140 // Worktree paths can be replaced between validation and open. Read regular blobs from the
147141 // committed base tree so repository-controlled symlinks and path races never reach the host filesystem.
148- const result = yield * executeGit ( {
149- operation : "gitHubChangeRequestTemplate.listTemplates" ,
150- cwd,
151- args : [ "ls-tree" , "-z" , "--full-tree" , treeish , "--" , ...TREE_PATHS ] ,
152- maxOutputBytes : TREE_LIST_MAX_BYTES ,
153- appendTruncationMarker : true ,
154- } ) ;
155- if ( result . stdoutTruncated ) {
156- return Option . none ( ) ;
157- }
142+ const listTemplates = ( paths : ReadonlyArray < string > ) =>
143+ executeGit ( {
144+ operation : "gitHubChangeRequestTemplate.listTemplates" ,
145+ cwd,
146+ args : [ "ls-tree" , "-z" , "--full-tree" , treeish , "--" , ...paths ] ,
147+ maxOutputBytes : TREE_LIST_MAX_BYTES ,
148+ appendTruncationMarker : true ,
149+ } ) ;
158150
159- const entries = parseTemplateTreeEntries ( result . stdout ) ;
160- for ( const templatePath of TEMPLATE_PATHS ) {
161- const candidates = entries
162- . filter ( ( entry ) => entry . path . toLowerCase ( ) === templatePath . toLowerCase ( ) )
163- . toSorted (
164- ( left , right ) => Number ( right . path === templatePath ) - Number ( left . path === templatePath ) ,
165- ) ;
166- for ( const entry of candidates ) {
167- const template = yield * readTemplateBlob ( { cwd, executeGit, entry } ) ;
168- if ( Option . isSome ( template ) ) {
169- return template ;
151+ // A lower-priority directory must not exhaust an earlier template's listing budget.
152+ for ( const location of TEMPLATE_LOCATIONS ) {
153+ const templatePaths = TEMPLATE_PATHS . filter (
154+ ( path ) => path . slice ( 0 , path . lastIndexOf ( "/" ) + 1 ) === location ,
155+ ) ;
156+ let result = yield * listTemplates ( [ location || "." ] ) ;
157+ if ( result . stdoutTruncated ) {
158+ result = yield * listTemplates ( templatePaths ) ;
159+ }
160+ if ( result . stdoutTruncated ) {
161+ return Option . none ( ) ;
162+ }
163+
164+ const entries = parseTemplateTreeEntries ( result . stdout ) ;
165+ for ( const templatePath of templatePaths ) {
166+ const candidates = entries
167+ . filter ( ( entry ) => entry . path . toLowerCase ( ) === templatePath . toLowerCase ( ) )
168+ . toSorted (
169+ ( left , right ) =>
170+ Number ( right . path === templatePath ) - Number ( left . path === templatePath ) ,
171+ ) ;
172+ for ( const entry of candidates ) {
173+ const template = yield * readTemplateBlob ( { cwd, executeGit, entry } ) ;
174+ if ( Option . isSome ( template ) ) {
175+ return template ;
176+ }
170177 }
171178 }
172179 }
173180
174181 for ( const directory of TEMPLATE_DIRECTORIES ) {
182+ const result = yield * listTemplates ( [ `${ directory } /` ] ) ;
183+ if ( result . stdoutTruncated ) {
184+ return Option . none ( ) ;
185+ }
186+ const entries = parseTemplateTreeEntries ( result . stdout ) ;
175187 const directoryTemplate = yield * readTemplateDirectory ( {
176188 cwd,
177189 executeGit,
0 commit comments