@@ -27,7 +27,7 @@ import { joinUrl } from "../../core/url.ts";
2727import { completeMessage , withSpinner } from "../../core/console.ts" ;
2828import { renderForPublish } from "../common/publish.ts" ;
2929import { RenderFlags } from "../../command/render/types.ts" ;
30- import { gitCmds , gitVersion } from "../../core/git.ts" ;
30+ import { gitBranchExists , gitCmds , gitVersion } from "../../core/git.ts" ;
3131import {
3232 anonymousAccount ,
3333 gitHubContextForPublish ,
@@ -71,7 +71,7 @@ async function publishRecord(
7171 input : string | ProjectContext ,
7272) : Promise < PublishRecord | undefined > {
7373 const ghContext = await gitHubContextForPublish ( input ) ;
74- if ( ghContext . ghPages ) {
74+ if ( ghContext . ghPagesRemote ) {
7575 return {
7676 id : "gh-pages" ,
7777 url : ghContext . siteUrl || ghContext . originUrl ,
@@ -114,17 +114,27 @@ async function publish(
114114 const ghContext = await gitHubContextForPublish ( options . input ) ;
115115 verifyContext ( ghContext , "GitHub Pages" ) ;
116116
117- // create gh pages branch if there is none yet
118- const createGhPagesBranch = ! ghContext . ghPages ;
119- if ( createGhPagesBranch ) {
117+ // create gh pages branch on remote and local if there is none yet
118+ const createGhPagesBranchRemote = ! ghContext . ghPagesRemote ;
119+ const createGhPagesBranchLocal = ! ghContext . ghPagesLocal ;
120+ if ( createGhPagesBranchRemote ) {
120121 // confirm
121- const confirmed = await Confirm . prompt ( {
122+ let confirmed = await Confirm . prompt ( {
122123 indent : "" ,
123124 message : `Publish site to ${
124125 ghContext . siteUrl || ghContext . originUrl
125126 } using gh-pages?`,
126127 default : true ,
127128 } ) ;
129+ if ( confirmed && ! createGhPagesBranchLocal ) {
130+ confirmed = await Confirm . prompt ( {
131+ indent : "" ,
132+ message :
133+ `A local gh-pages branch already exists. Should it be pushed to remote 'origin'?` ,
134+ default : true ,
135+ } ) ;
136+ }
137+
128138 if ( ! confirmed ) {
129139 throw new Error ( ) ;
130140 }
@@ -135,9 +145,29 @@ async function publish(
135145 }
136146 const oldBranch = await gitCurrentBranch ( input ) ;
137147 try {
138- await gitCreateGhPages ( input ) ;
148+ // Create and push if necessary, or just push local branch
149+ if ( createGhPagesBranchLocal ) {
150+ await gitCreateGhPages ( input ) ;
151+ } else {
152+ await gitPushGhPages ( input ) ;
153+ }
154+ } catch {
155+ // Something failed so clean up, i.e
156+ // if we created the branch then delete it.
157+ // Example of failure: Auth error on push (https://github.com/quarto-dev/quarto-cli/issues/9585)
158+ if ( createGhPagesBranchLocal && await gitBranchExists ( "gh-pages" ) ) {
159+ await gitCmds ( input , [
160+ [ "checkout" , oldBranch ] ,
161+ [ "branch" , "-D" , "gh-pages" ] ,
162+ ] ) ;
163+ }
164+ throw new Error (
165+ "Publishing to gh-pages with `quarto publish gh-pages` failed." ,
166+ ) ;
139167 } finally {
140- await gitCmds ( input , [ [ "checkout" , oldBranch ] ] ) ;
168+ if ( await gitCurrentBranch ( input ) !== oldBranch ) {
169+ await gitCmds ( input , [ [ "checkout" , oldBranch ] ] ) ;
170+ }
141171 if ( stash ) {
142172 await gitStashApply ( input ) ;
143173 }
@@ -360,7 +390,14 @@ async function gitCreateGhPages(dir: string) {
360390 await gitCmds ( dir , [
361391 [ "checkout" , "--orphan" , "gh-pages" ] ,
362392 [ "rm" , "-rf" , "--quiet" , "." ] ,
363- [ "commit" , "--allow-empty" , "-m" , `Initializing gh-pages branch` ] ,
364- [ "push" , "origin" , `HEAD:gh-pages` ] ,
393+ [ "commit" , "--allow-empty" , "-m" , "Initializing gh-pages branch" ] ,
365394 ] ) ;
395+ await gitPushGhPages ( dir ) ;
396+ }
397+
398+ async function gitPushGhPages ( dir : string ) {
399+ if ( await gitCurrentBranch ( dir ) !== "gh-pages" ) {
400+ await gitCmds ( dir , [ [ "checkout" , "gh-pages" ] ] ) ;
401+ }
402+ await gitCmds ( dir , [ [ "push" , "origin" , "HEAD:gh-pages" ] ] ) ;
366403}
0 commit comments