@@ -651,6 +651,63 @@ export function registerCommands(
651651 return { folderManager, pr } ;
652652 } ;
653653
654+ const applyPullRequestChanges = async ( folderManager : FolderRepositoryManager , pullRequest : PullRequestModel ) : Promise < void > => {
655+ let patch : string | undefined ;
656+ try {
657+ patch = await pullRequest . getPatch ( ) ;
658+
659+ if ( ! patch . trim ( ) ) {
660+ vscode . window . showErrorMessage ( vscode . l10n . t ( 'No patch data available for pull request #{0}' , pullRequest . number . toString ( ) ) ) ;
661+ return ;
662+ }
663+
664+ const tempFilePath = pathLib . join (
665+ folderManager . repository . rootUri . fsPath ,
666+ '.git' ,
667+ `pr-${ pullRequest . number } .patch` ,
668+ ) ;
669+ const encoder = new TextEncoder ( ) ;
670+ const tempUri = vscode . Uri . file ( tempFilePath ) ;
671+
672+ await vscode . window . withProgress (
673+ {
674+ location : vscode . ProgressLocation . Notification ,
675+ title : vscode . l10n . t ( 'Applying changes from PR #{0}' , pullRequest . number . toString ( ) ) ,
676+ cancellable : false
677+ } ,
678+ async ( task ) => {
679+ await vscode . workspace . fs . writeFile ( tempUri , encoder . encode ( patch ) ) ;
680+ try {
681+ await folderManager . repository . apply ( tempFilePath , false ) ;
682+ task . report ( { message : vscode . l10n . t ( 'Successfully applied changes from pull request #{0}' , pullRequest . number . toString ( ) ) , increment : 100 } ) ;
683+ } finally {
684+ await vscode . workspace . fs . delete ( tempUri ) ;
685+ }
686+ }
687+ ) ;
688+
689+ } catch ( error ) {
690+ const errorMessage = formatError ( error ) ;
691+ Logger . error ( `Failed to apply PR changes: ${ errorMessage } ` , 'Commands' ) ;
692+
693+ const copyGitApply = vscode . l10n . t ( 'Copy git apply' ) ;
694+ const result = await vscode . window . showErrorMessage (
695+ vscode . l10n . t ( 'Failed to apply changes from pull request: {0}' , errorMessage ) ,
696+ copyGitApply
697+ ) ;
698+
699+ if ( result === copyGitApply ) {
700+ if ( patch ) {
701+ const gitApplyCommand = `git apply --3way <<'EOF'\n${ patch } \nEOF` ;
702+ await vscode . env . clipboard . writeText ( gitApplyCommand ) ;
703+ vscode . window . showInformationMessage ( vscode . l10n . t ( 'Git apply command copied to clipboard' ) ) ;
704+ } else {
705+ vscode . window . showErrorMessage ( vscode . l10n . t ( 'Unable to copy git apply command - patch content is not available' ) ) ;
706+ }
707+ }
708+ }
709+ } ;
710+
654711 function contextHasPath ( ctx : OverviewContext | { path : string } | undefined ) : ctx is { path : string } {
655712 const contextAsPath : Partial < { path : string } > = ( ctx as { path : string } ) ;
656713 return ! ! contextAsPath . path ;
@@ -684,6 +741,34 @@ export function registerCommands(
684741
685742 } ) ) ;
686743
744+ context . subscriptions . push ( vscode . commands . registerCommand ( 'pr.applyChangesFromDescription' , async ( ctx : OverviewContext | { path : string } | undefined ) => {
745+ if ( ! ctx ) {
746+ return vscode . window . showErrorMessage ( vscode . l10n . t ( 'No pull request context provided for applying changes.' ) ) ;
747+ }
748+
749+ if ( contextHasPath ( ctx ) ) {
750+ const { path } = ctx ;
751+ const prNumber = Number ( Buffer . from ( path . substring ( 1 ) , 'base64' ) . toString ( 'utf8' ) ) ;
752+ if ( Number . isNaN ( prNumber ) ) {
753+ return vscode . window . showErrorMessage ( vscode . l10n . t ( 'Unable to parse pull request number.' ) ) ;
754+ }
755+ const folderManager = reposManager . folderManagers [ 0 ] ;
756+ const pullRequest = await folderManager . fetchById ( folderManager . gitHubRepositories [ 0 ] , Number ( prNumber ) ) ;
757+ if ( ! pullRequest ) {
758+ return vscode . window . showErrorMessage ( vscode . l10n . t ( 'Unable to find pull request #{0}' , prNumber . toString ( ) ) ) ;
759+ }
760+
761+ return applyPullRequestChanges ( folderManager , pullRequest ) ;
762+ }
763+
764+ const resolved = await resolvePr ( ctx ) ;
765+ if ( ! resolved ) {
766+ return vscode . window . showErrorMessage ( vscode . l10n . t ( 'Unable to resolve pull request for applying changes.' ) ) ;
767+ }
768+ return applyPullRequestChanges ( resolved . folderManager , resolved . pr ) ;
769+
770+ } ) ) ;
771+
687772 context . subscriptions . push (
688773 vscode . commands . registerCommand ( 'pr.openChanges' , async ( pr : PRNode | RepositoryChangesNode | PullRequestModel | OverviewContext | undefined ) => {
689774 if ( pr === undefined ) {
0 commit comments