@@ -33,17 +33,14 @@ export async function activate(context: vscode.ExtensionContext) {
3333 context . subscriptions . push (
3434 vscode . commands . registerCommand (
3535 "codesync.sendChanges" ,
36- async ( deviceId : number ) => {
36+ async ( { deviceId, projectId } ) => {
3737 const git = await getGitAPI ( ) ;
38- console . log ( "sd" ) ;
3938 const repositories = git ?. repositories || [ ] ;
4039 // TODO: should pick correct repo if multiple are open
4140 const repo = await git ?. init ( repositories [ 0 ] . rootUri ) ;
42-
4341 const cmd_add_to_index = `git status --porcelain | sed -r 's/^.{3}//' | while read line; do git add -N $line; done` ;
44-
4542 const cwd = vscode . workspace . workspaceFolders ! [ 0 ] . uri . fsPath ;
46- console . log ( cwd ) ;
43+
4744 child_process . exec (
4845 cmd_add_to_index ,
4946 {
@@ -52,16 +49,11 @@ export async function activate(context: vscode.ExtensionContext) {
5249 ( error , stdout , stderr ) => {
5350 // TODO: move `| sed -r 's/^.{3}//' | while read line; do git add -N $line; done`
5451 // to here, to remove dependency on bash commands
55-
5652 if ( error ) {
5753 vscode . window . showErrorMessage (
58- "Failed to create patch " ,
54+ "Failed to add to index. " ,
5955 error . message
6056 ) ;
61- } else {
62- vscode . window . showInformationMessage (
63- "Patch files created successfully"
64- ) ;
6557 }
6658 }
6759 ) ;
@@ -73,23 +65,21 @@ export async function activate(context: vscode.ExtensionContext) {
7365 diff . staged = ( await repo ?. diff ( true ) ) || "" ;
7466 diff . unstaged = ( await repo ?. diff ( ) ) || "" ;
7567
76- console . log ( "STAGE" , diff . staged ) ;
77- console . log ( "UNSTAGE" , diff . unstaged ) ;
78-
7968 const response = await axios . post (
8069 "http://localhost:3001/changes" ,
8170 {
82- diff,
71+ diff : JSON . stringify ( diff ) ,
8372 deviceId,
73+ projectId,
8474 }
8575 ) ;
8676
8777 if ( response . data . success ) {
88- vscode . window . showInformationMessage ( "Changes saved" ) ;
89- } else {
90- vscode . window . showErrorMessage (
91- "Error sending and saving message"
78+ vscode . window . showInformationMessage (
79+ "Successfully created diff"
9280 ) ;
81+ } else {
82+ vscode . window . showErrorMessage ( "Error, try again" ) ;
9383 }
9484 }
9585 )
@@ -98,43 +88,78 @@ export async function activate(context: vscode.ExtensionContext) {
9888 context . subscriptions . push (
9989 vscode . commands . registerCommand (
10090 "codesync.applyChanges" ,
101- async ( deviceId : number ) => {
102- // const git = await getGitAPI();
103- // const repositories = git?.repositories || [];
104- // TODO: check if multiple repos open
105- // const repo = await git?.init(repositories[0].rootUri);
91+ async ( { deviceId, projectId } ) => {
92+ const git = await getGitAPI ( ) ;
93+ const repositories = git ?. repositories || [ ] ;
94+ // TODO: should pick correct repo if multiple are open
95+ const repo = await git ?. init ( repositories [ 0 ] . rootUri ) ;
10696
10797 const currentDir = vscode . workspace . workspaceFolders || [ ] ;
98+ const stagedPatch = `${ currentDir [ 0 ] . uri . fsPath } /staged.patch` ;
99+ const unstagedPatch = `${ currentDir [ 0 ] . uri . fsPath } /unstaged.patch` ;
108100
109101 try {
110- const retrievedChanges = await axios . get (
111- "http://localhost:3001/changes " ,
102+ const retrievedChanges = await axios . post (
103+ "http://localhost:3001/change " ,
112104 {
113- data : { deviceId } ,
105+ deviceId,
106+ projectId,
114107 }
115108 ) ;
116- const diff = retrievedChanges . data . diff ;
109+ if ( ! retrievedChanges . data . success ) {
110+ throw new Error ( ) ;
111+ }
112+
113+ const diff = JSON . parse ( retrievedChanges . data . diff ) ;
117114
118- const stagedPatch = `${ currentDir [ 0 ] . uri . fsPath } /staged.patch` ;
119115 await vscode . workspace . fs . writeFile (
120116 vscode . Uri . file ( stagedPatch ) ,
121117 Buffer . from ( diff . staged )
122118 ) ;
123119
124- const unstagedPatch = `${ currentDir [ 0 ] . uri . fsPath } /unstaged.patch` ;
125120 await vscode . workspace . fs . writeFile (
126121 vscode . Uri . file ( unstagedPatch ) ,
127122 Buffer . from ( diff . unstaged )
128123 ) ;
129- } catch ( error ) {
130- console . error ( error ) ;
131- }
132124
133- // repo?.apply(patcFilePath);
125+ await repo ?. apply ( stagedPatch ) ;
126+
127+ const cmd_stage = `git add -A && git reset -- *.patch` ;
128+ const cwd =
129+ vscode . workspace . workspaceFolders ! [ 0 ] . uri . fsPath ;
130+
131+ child_process . exec (
132+ cmd_stage ,
133+ {
134+ cwd,
135+ } ,
136+ ( error , stdout , stderr ) => {
137+ // TODO: move `| sed -r 's/^.{3}//' | while read line; do git add -N $line; done`
138+ // to here, to remove dependency on bash commands
139+ if ( error ) {
140+ vscode . window . showErrorMessage (
141+ "Failed to staged." ,
142+ error . message
143+ ) ;
144+ }
145+ }
146+ ) ;
134147
135- // await vscode.workspace.fs.delete(vscode.Uri.file(patcFilePath) );
148+ await repo ?. apply ( unstagedPatch ) ;
136149
137- vscode . window . showInformationMessage ( "Patch file created" ) ;
150+ await vscode . workspace . fs . delete (
151+ vscode . Uri . file ( stagedPatch )
152+ ) ;
153+ await vscode . workspace . fs . delete (
154+ vscode . Uri . file ( unstagedPatch )
155+ ) ;
156+
157+ vscode . window . showInformationMessage (
158+ "Success. Patch file created and applied"
159+ ) ;
160+ } catch ( error ) {
161+ vscode . window . showErrorMessage ( "Error, try again" ) ;
162+ }
138163 }
139164 )
140165 ) ;
0 commit comments