@@ -9,10 +9,13 @@ type GitFetchParams = {
99 */
1010 remote : string ;
1111 /**
12- * Branch to fetch. It will be converted to a full refspec for fetching:
13- * e.g. `branch: 'main', remote: 'origin'` will be converted to `+main:refs/remotes/origin/main`.
12+ * Branch(es) to fetch. Each will be converted to a full refspec for fetching:
13+ * e.g. `branch: 'main', remote: 'origin'` will be converted to `+refs/heads/main:refs/remotes/origin/main`.
14+ * Pass an array to fetch multiple branches in a single git invocation, which lets a single
15+ * `--deepen` or `--unshallow` apply to all of them (saves network round-trips when both HEAD
16+ * and the target branch need deepening).
1417 */
15- branch : string ;
18+ branch : string | string [ ] ;
1619 /** Set depth to this number of commits (mutually exclusive with `deepen` and `unshallow`) */
1720 depth ?: number ;
1821 /** Deepen a shallow clone by this number of commits (mutually exclusive with `depth` and `unshallow`) */
@@ -31,7 +34,8 @@ type GitFetchParams = {
3134 * the remote branch is tracked or not in the local repository.
3235 */
3336export function gitFetch ( params : GitFetchParams ) : GitProcessOutput & { errorMessage ?: string } {
34- const { remote, branch, depth, deepen, unshallow, cwd, verbose } = params ;
37+ const { remote, depth, deepen, unshallow, cwd, verbose } = params ;
38+ const branches = Array . isArray ( params . branch ) ? params . branch : [ params . branch ] ;
3539 const { shouldLog } = getGitEnv ( verbose ) ;
3640
3741 if ( [ depth , deepen , unshallow ] . filter ( v => v !== undefined ) . length > 1 ) {
@@ -40,20 +44,24 @@ export function gitFetch(params: GitFetchParams): GitProcessOutput & { errorMess
4044
4145 const extraArgs = depth ? [ `--depth=${ depth } ` ] : deepen ? [ `--deepen=${ deepen } ` ] : unshallow ? [ '--unshallow' ] : [ ] ;
4246
43- // Be specific with the ref being fetched, so we don't have to worry about tracking configs.
47+ // Be specific with each ref being fetched, so we don't have to worry about tracking configs.
4448 // In git fetch <remote> +<src>:<dst>...
4549 // - The + means allow non-fast-forward updates (in case the remote was force pushed).
4650 // - <src> refs/heads/${branch} is resolved against the remote's advertised refs. The fully
4751 // qualified ref is unambiguous, whereas bare branch names can be silently misresolved,
4852 // causing git to treat the ref as absent and delete the local tracking ref.
4953 // - <dst> refs/remotes/${remote}/${branch} is resolved locally and only moves the tracking ref
5054 // for the remote branch, not the local refs/heads/${branch} or its tracking config.
51- const resolvedBranch = remote ? `+refs/heads/${ branch } :refs/remotes/${ remote } /${ branch } ` : undefined ;
55+ const resolvedRefspecs = remote ? branches . map ( b => `+refs/heads/${ b } :refs/remotes/${ remote } /${ b } ` ) : [ ] ;
5256
53- const shortDescription = `Fetching ${ resolvedBranch ? `branch "${ branch } " from remote "${ remote } "` : 'all remotes' } ` ;
57+ const branchLabel =
58+ branches . length > 1 ? `branches ${ branches . map ( b => `"${ b } "` ) . join ( ', ' ) } ` : `branch "${ branches [ 0 ] } "` ;
59+ const shortDescription = `Fetching ${
60+ resolvedRefspecs . length ? `${ branchLabel } from remote "${ remote } "` : 'all remotes'
61+ } `;
5462
5563 let description = shortDescription ;
56- resolvedBranch && ( description += ` (${ resolvedBranch } )` ) ;
64+ resolvedRefspecs . length && ( description += ` (${ resolvedRefspecs . join ( ' ' ) } )` ) ;
5765 extraArgs . length && ( description += ` (with ${ extraArgs . join ( ' ' ) } )` ) ;
5866 shouldLog && console . log ( description + '...' ) ;
5967
@@ -62,7 +70,7 @@ export function gitFetch(params: GitFetchParams): GitProcessOutput & { errorMess
6270 'fetch' ,
6371 ...extraArgs ,
6472 // If the remote is unknown, don't specify the branch (fetching a branch without a remote is invalid)
65- ...( resolvedBranch ? [ remote , resolvedBranch ] : [ ] ) ,
73+ ...( resolvedRefspecs . length ? [ remote , ... resolvedRefspecs ] : [ ] ) ,
6674 ] ,
6775 { cwd, stdio : shouldLog === 'live' ? 'inherit' : 'pipe' }
6876 ) ;
0 commit comments