@@ -666,7 +666,6 @@ ipcMain.on('git-magic', async (event, branch) => {
666
666
let dir = path . join ( app . getPath ( 'appData' ) , '/EmuDeck/backend' ) ;
667
667
if ( os . platform ( ) . includes ( 'win32' ) ) {
668
668
repo = 'https://github.com/EmuDeck/emudeck-we.git' ;
669
- dir = path . join ( app . getPath ( 'appData' ) , '/EmuDeck/backend' ) ;
670
669
} else {
671
670
dir = path . join ( os . homedir ( ) , '.config/EmuDeck/backend' ) ;
672
671
}
@@ -679,113 +678,124 @@ ipcMain.on('git-magic', async (event, branch) => {
679
678
680
679
let status = await git . status ( { fs, dir, filepath : 'README.md' } ) ;
681
680
console . log ( { status } ) ;
681
+ //No backend? we clone it.
682
682
if ( status === 'absent' ) {
683
+ await fs . rm ( dir , { recursive : true , force : true } , ( err ) => {
684
+ if ( err ) {
685
+ // Ocurrió un error al eliminar el directorio
686
+ console . error ( `Error deleting: ${ err } ` ) ;
687
+ message = 'error' ;
688
+ }
689
+ message = 'success' ;
690
+ // Directorio eliminado con éxito
691
+ console . log ( 'backend deleted 0' ) ;
692
+ } ) ;
683
693
await git
684
694
. clone ( {
685
695
fs,
686
696
http,
687
697
dir,
688
698
url : repo ,
689
699
depth : 1 ,
690
- onMessage : console . log ,
691
700
} )
692
- . then ( ( message = 'success' ) )
693
- . catch ( ( message = 'error' ) ) ;
694
- } else {
695
- //Fetch of new branches
696
- await git . fetch ( {
697
- fs,
698
- http,
699
- dir,
700
- url : repo ,
701
- depth : 1 ,
702
- tags : false ,
703
- } ) ;
704
-
705
- // Status Matrix Row Indexes (git reset)
706
- const FILEPATH = 0 ;
707
- const HEAD = 1 ;
708
- const WORKDIR = 2 ;
709
- const STAGE = 3 ;
710
-
711
- // Status Matrix State
712
- const UNCHANGED = 1 ;
713
-
714
- const allFiles = await git . statusMatrix ( { dir, fs } ) ;
715
- // Get all files which have been modified or staged - does not include new untracked files or deleted files
716
-
717
- const modifiedFiles = allFiles
718
- . filter ( ( row ) => row [ WORKDIR ] > UNCHANGED && row [ STAGE ] > UNCHANGED )
719
- . map ( ( row ) => row [ FILEPATH ] ) ;
720
-
721
- console . log ( { modifiedFiles } ) ;
722
-
723
- // Delete modified/staged files
724
- await Promise . all ( modifiedFiles . map ( ( path ) => fs . promises . rm ( path ) ) ) ;
701
+ . then ( ( message = 'success' ) ) ;
725
702
726
- await git . checkout ( { dir, fs, ref : branch , force : true } ) ;
703
+ console . log ( { message } ) ;
704
+ } else {
705
+ //Git reset hard
706
+ try {
707
+ await git . checkout ( {
708
+ fs,
709
+ dir,
710
+ ref : 'HEAD' ,
711
+ force : true ,
712
+ } ) ;
713
+ console . log ( 'All changes discarded successfully.' ) ;
714
+ } catch ( error ) {
715
+ console . error ( 'Error discarding all changes:' , error ) ;
716
+ }
727
717
728
- await git
729
- . pull ( {
718
+ //Fetch of new branches
719
+ try {
720
+ await git . fetch ( {
730
721
fs,
731
722
http,
732
723
dir,
733
- singleBranch : true ,
724
+ url : repo ,
725
+ depth : 1 ,
726
+ tags : false ,
727
+ } ) ;
728
+ //Switch to the proper branch
729
+ await git . checkout ( {
730
+ fs,
731
+ dir,
732
+ ref : branch ,
733
+ } ) ;
734
+ //Pull
735
+ await git . merge ( {
736
+ fs,
737
+ dir,
738
+ ours : branch ,
739
+ theirs : `origin/${ branch } ` ,
734
740
author :
{ name :
'EmuDeck' , email :
'[email protected] ' } ,
735
- } )
736
- . then ( ( message = 'success' ) ) ;
737
- }
741
+ } ) ;
742
+ status = await git . status ( { fs, dir, filepath : 'README.md' } ) ;
743
+ if ( status === 'absent' || status === '*deleted' ) {
744
+ message = 'error' ;
745
+ } else {
746
+ message = 'success' ;
747
+ }
738
748
739
- status = await git . status ( { fs, dir, filepath : 'README.md' } ) ;
740
- if ( status === 'absent' ) {
741
- message = 'error' ;
742
- } else {
743
- message = 'success' ;
749
+ status = await git . status ( { fs, dir, filepath : '.git' } ) ;
750
+ console . log ( 'status2' , { status } ) ;
751
+ if ( status === 'absent' || status === '*deleted' ) {
752
+ message = 'error' ;
753
+ } else {
754
+ message = 'success' ;
755
+ }
756
+ } catch ( error ) {
757
+ await fs . rm ( dir , { recursive : true , force : true } , ( err ) => {
758
+ if ( err ) {
759
+ // Ocurrió un error al eliminar el directorio
760
+ console . error ( `Error deleting: ${ err } ` ) ;
761
+ }
762
+ message = 'error' ;
763
+ // Directorio eliminado con éxito
764
+ console . log ( 'backend deleted 1' ) ;
765
+ } ) ;
766
+ await git
767
+ . clone ( {
768
+ fs,
769
+ http,
770
+ dir,
771
+ url : repo ,
772
+ depth : 1 ,
773
+ } )
774
+ . then ( ( message = 'success' ) ) ;
775
+ }
776
+ if ( message === 'error' ) {
777
+ console . log ( 'GIT ERRRORRR' ) ;
778
+ //We delete the backend and we we force a reload
779
+ await fs . rm ( dir , { recursive : true , force : true } , ( err ) => {
780
+ if ( err ) {
781
+ // Ocurrió un error al eliminar el directorio
782
+ console . error ( `Error deleting: ${ err } ` ) ;
783
+ message = 'error' ;
784
+ }
785
+ message = 'success' ;
786
+ // Directorio eliminado con éxito
787
+ console . log ( 'backend deleted 2' ) ;
788
+ } ) ;
789
+ message = 'error' ;
790
+ }
744
791
}
745
-
746
- await git . checkout ( {
747
- fs,
748
- dir,
749
- ref : branch ,
750
- } ) ;
751
-
792
+ console . log ( 'GIT' , { message } ) ;
752
793
return exec ( `${ bashCommand } ` , shellType , ( error , stdout , stderr ) => {
753
794
logCommand ( bashCommand , error , stdout , stderr ) ;
754
795
event . reply ( 'git-magic' , message ) ;
755
796
} ) ;
756
-
757
- // const backChannel = 'check-git-status';
758
- // let bashCommand = `cd ~/.config/EmuDeck/backend && git status`;
759
- //
760
- // if (os.platform().includes('darwin')) {
761
- // bashCommand = `cd ~/.config/EmuDeck/backend && git status`;
762
- // }
763
- // if (os.platform().includes('win32')) {
764
- // bashCommand = `cd %userprofile% && cd AppData && cd Roaming && cd EmuDeck && cd backend && git status`;
765
- // }
766
- //
767
- // return exec(`${bashCommand}`, shellType, (error, stdout, stderr) => {
768
- // logCommand(bashCommand, error, stdout, stderr);
769
- // event.reply(backChannel, stdout);
770
- // });
771
797
} ) ;
772
798
773
- // Next release
774
- // ipcMain.on('pull', async (event, branch) => {
775
- // const branchGIT = branch;
776
- // const backChannel = 'pull';
777
- // const bashCommand = `API_pull "${branchGIT}"`;
778
- //
779
- // return exec(
780
- // `${startCommand} . ${allPath}; ${bashCommand} ${finishCommand}`,
781
- // shellType,
782
- // (error, stdout, stderr) => {
783
- // logCommand(bashCommand, error, stdout, stderr);
784
- // event.reply(backChannel, stdout);
785
- // }
786
- // );
787
- // });
788
-
789
799
ipcMain . on ( 'branch' , async ( event ) => {
790
800
event . reply ( 'branch-out' , process . env . BRANCH ) ;
791
801
} ) ;
0 commit comments