@@ -58,21 +58,78 @@ function gitExec(command, options = {}) {
5858
5959// Save current branch and changes
6060function saveCurrentState ( ) {
61- const currentBranch = gitExec ( 'rev-parse --abbrev-ref HEAD' ) ;
61+ let currentBranch = gitExec ( 'rev-parse --abbrev-ref HEAD' ) ;
62+ const currentCommit = gitExec ( 'rev-parse HEAD' ) ;
6263 const hasChanges = gitExec ( 'status --porcelain' ) ;
6364
65+ // Check if we're in detached HEAD state (common in CI)
66+ if ( currentBranch === 'HEAD' ) {
67+ console . log ( 'Detected detached HEAD state (common in CI)' ) ;
68+ console . log ( `Current commit: ${ currentCommit } ` ) ;
69+
70+ // Try to find which branch we're on by checking which branches contain this commit
71+ try {
72+ const branches = gitExec ( `branch -r --contains ${ currentCommit } ` ) ;
73+ console . log ( 'Branches containing current commit:' , branches ) ;
74+
75+ // In CI, we might want to use the commit hash directly
76+ currentBranch = currentCommit ;
77+ console . log ( `Will restore to commit: ${ currentCommit } ` ) ;
78+ } catch ( e ) {
79+ console . log ( 'Could not determine branch from commit' ) ;
80+ }
81+ } else {
82+ console . log ( `Starting from branch: ${ currentBranch } ` ) ;
83+ }
84+
85+ console . log ( `Current working directory: ${ process . cwd ( ) } ` ) ;
86+
6487 if ( hasChanges ) {
6588 console . log ( 'Stashing current changes...' ) ;
6689 gitExec ( 'stash push -m "migrate-branches-to-versions temporary stash"' ) ;
6790 }
6891
69- return { currentBranch, hasChanges : ! ! hasChanges } ;
92+ return {
93+ currentBranch,
94+ currentCommit,
95+ isDetachedHead : currentBranch === currentCommit ,
96+ hasChanges : ! ! hasChanges ,
97+ startingDir : process . cwd ( )
98+ } ;
7099}
71100
72101// Restore original state
73102function restoreState ( state ) {
74- console . log ( `Switching back to ${ state . currentBranch } ...` ) ;
75- gitExec ( `checkout ${ state . currentBranch } ` ) ;
103+ console . log ( `\nRestoring original state...` ) ;
104+ const currentLocation = gitExec ( 'rev-parse --abbrev-ref HEAD' ) ;
105+ console . log ( `Current location: ${ currentLocation } ` ) ;
106+ console . log ( `Current directory: ${ process . cwd ( ) } ` ) ;
107+
108+ if ( state . isDetachedHead ) {
109+ // In CI with detached HEAD, checkout the specific commit
110+ console . log ( `Restoring to commit: ${ state . currentCommit } ` ) ;
111+ gitExec ( `checkout ${ state . currentCommit } ` ) ;
112+
113+ // Verify we're at the right commit
114+ const actualCommit = gitExec ( 'rev-parse HEAD' ) ;
115+ if ( actualCommit !== state . currentCommit ) {
116+ console . error ( `Warning: Expected commit ${ state . currentCommit } but at ${ actualCommit } ` ) ;
117+ } else {
118+ console . log ( `✓ Successfully restored to commit ${ state . currentCommit } ` ) ;
119+ }
120+ } else {
121+ // Normal branch checkout
122+ console . log ( `Switching back to branch: ${ state . currentBranch } ...` ) ;
123+ gitExec ( `checkout ${ state . currentBranch } ` ) ;
124+
125+ // Verify we're on the right branch
126+ const actualBranch = gitExec ( 'rev-parse --abbrev-ref HEAD' ) ;
127+ if ( actualBranch !== state . currentBranch ) {
128+ console . error ( `Warning: Expected to be on ${ state . currentBranch } but actually on ${ actualBranch } ` ) ;
129+ } else {
130+ console . log ( `✓ Successfully restored to branch ${ state . currentBranch } ` ) ;
131+ }
132+ }
76133
77134 if ( state . hasChanges ) {
78135 console . log ( 'Restoring stashed changes...' ) ;
@@ -517,6 +574,35 @@ async function migrate() {
517574 // Restore original state
518575 restoreState ( originalState ) ;
519576
577+ // After switching back, ensure the site directory exists
578+ // In CI, switching branches might have removed it
579+ console . log ( `\nChecking if site directory exists at: ${ SITE_DIR } ` ) ;
580+ if ( ! fs . existsSync ( SITE_DIR ) ) {
581+ console . error ( '\n⚠️ Warning: Site directory was removed during branch switching.' ) ;
582+ console . error ( `Expected site directory at: ${ SITE_DIR } ` ) ;
583+ console . error ( 'This can happen in CI when switching to older branches.' ) ;
584+ console . error ( 'The site directory should be restored by Git, but it may not be immediate.' ) ;
585+
586+ // Try to force Git to restore the directory
587+ console . log ( 'Attempting to restore site directory from Git...' ) ;
588+ try {
589+ gitExec ( 'checkout HEAD -- site' ) ;
590+ console . log ( '✓ Restored site directory from Git' ) ;
591+
592+ // Verify it was restored
593+ if ( fs . existsSync ( SITE_DIR ) ) {
594+ console . log ( '✓ Site directory now exists' ) ;
595+ } else {
596+ console . error ( '✗ Site directory still missing after restore attempt' ) ;
597+ }
598+ } catch ( e ) {
599+ console . error ( 'Could not restore site directory:' , e . message ) ;
600+ console . error ( 'Git may not have the site directory in the current branch' ) ;
601+ }
602+ } else {
603+ console . log ( '✓ Site directory exists' ) ;
604+ }
605+
520606 // Update and write docusaurus config after returning to original branch
521607 if ( docusaurusConfig && fs . existsSync ( DOCUSAURUS_CONFIG_PATH ) ) {
522608 const updatedConfig = updateDocusaurusConfig ( docusaurusConfig ) ;
0 commit comments