@@ -2,6 +2,7 @@ import * as vscode from 'vscode';
22import { exec } from 'child_process' ;
33import path from 'path' ;
44import * as fs from 'fs' ;
5+ import * as workspaceUtils from "./workspaceUtils" ;
56
67export class GitUtils {
78 private static instance : GitUtils ;
@@ -15,44 +16,36 @@ export class GitUtils {
1516
1617 public async submitToGit ( ) : Promise < any > {
1718 console . debug ( "[DEBUG]#### GitUtils submitToGit begin." ) ;
18- const gitRootPath = vscode . workspace . rootPath ;
19+ const activeDocumentPath = workspaceUtils . getActiveEditorPath ( ) ;
20+ const gitRootPath = path . dirname ( activeDocumentPath ) ;
1921 if ( ! gitRootPath ) {
2022 vscode . window . showErrorMessage ( 'Please open a workspace folder first!' ) ;
2123 return ;
2224 }
2325
24- while ( true ) {
25- if ( fs . existsSync ( path . join ( gitRootPath , '.git' ) ) ) {
26- vscode . window . showInformationMessage ( 'Trying to fetch from Git, please wait...' ) ;
27- await this . fetchFromGit ( ) ;
28- break ;
29- } else {
30- vscode . window . showInformationMessage ( `You are not in a git repository yet, trying to clone from your Git repo...` ) ;
31- await this . cloneFromGit ( ) ;
32- // retry
33- }
26+ if ( fs . existsSync ( path . join ( gitRootPath , '.git' ) ) ) {
27+ vscode . window . showInformationMessage ( 'Trying to fetch from Git, please wait...' ) ;
28+ await this . fetchFromGit ( gitRootPath ) ;
29+ } else {
30+ vscode . window . showInformationMessage ( `You are not in a git repository yet, trying to clone from your Git repo...` ) ;
31+ await this . cloneFromGit ( gitRootPath ) ;
3432 }
3533
36- exec ( 'git add . && git commit -m "committed by VS Code" && git push' , { cwd : gitRootPath } , ( error , stdout , stderr ) => {
37- if ( error ) {
38- vscode . window . showErrorMessage ( `Failed to submit code: ${ error . message } ` ) ;
39- return ;
40- }
41- if ( stderr ) {
42- vscode . window . showErrorMessage ( `Failed to submit code: ${ stderr } ` ) ;
43- return ;
44- }
45- vscode . window . showInformationMessage ( 'Code has been successfully submitted to Git repo!' ) ;
46- } ) ;
47- return ;
34+ console . debug ( "[DEBUG]#### GitUtils pushToGit begin." ) ;
35+ try {
36+ const output = await this . gitPush ( gitRootPath ) ;
37+ vscode . window . showInformationMessage ( `Code has been successfully submitted to Git repo!\nDetail:[${ output } ]` ) ;
38+ } catch ( error ) {
39+ vscode . window . showErrorMessage ( `Failed to submit code: ${ error . message } ` ) ;
40+ }
4841 }
4942
50- private async cloneFromGit ( ) : Promise < any > {
43+ private async cloneFromGit ( rootPath : string ) : Promise < any > {
5144 console . debug ( "[DEBUG]#### GitUtils cloneFromGit begin." ) ;
52- await vscode . window . showInputBox ( { prompt : 'Please enter the Git repository URL:' } ) . then ( ( url ) => {
45+ await vscode . window . showInputBox ( { prompt : 'Please enter the Git repository URL:' } ) . then ( async ( url ) => {
5346 if ( url ) {
5447 const folderName = url . split ( '/' ) . pop ( ) ?. replace ( '.git' , '' ) ;
55- const clonePath = vscode . workspace . rootPath ? `${ vscode . workspace . rootPath } /${ folderName } ` : folderName || '' ;
48+ const clonePath = rootPath ? `${ rootPath } /${ folderName } ` : folderName || '' ;
5649 if ( fs . existsSync ( clonePath ) ) {
5750 console . debug ( "[DEBUG]#### GitUtils cloneFromGit: clean and remove the clonePath before cloning the repository." ) ;
5851 fs . rm ( clonePath , { recursive : true } , ( ) => { } ) ;
@@ -72,23 +65,50 @@ export class GitUtils {
7265 } ) ;
7366 }
7467 } ) ;
75- return ;
7668 }
7769
78- private async fetchFromGit ( ) : Promise < any > {
70+ private gitPush ( rootPath : string ) : Promise < string > {
71+ return new Promise ( ( resolve , reject ) => {
72+ exec ( 'git add . && git commit -m "committed by VS Code" && git push' , { cwd : rootPath } , ( error , stdout , stderr ) => {
73+ if ( error ) {
74+ reject ( error ) ;
75+ return ;
76+ }
77+ if ( stdout ) {
78+ resolve ( stdout ) ;
79+ return ;
80+ }
81+ if ( stderr ) {
82+ reject ( new Error ( stderr ) ) ;
83+ }
84+ } ) ;
85+ } ) ;
86+ }
87+
88+ private gitPull ( rootPath : string ) : Promise < void > {
89+ return new Promise ( ( resolve , reject ) => {
90+ exec ( 'git pull' , { cwd : rootPath } , ( error , stdout , stderr ) => {
91+ if ( error ) {
92+ reject ( error ) ;
93+ return ;
94+ }
95+ if ( stderr ) {
96+ reject ( new Error ( stderr ) ) ;
97+ return ;
98+ }
99+ resolve ( ) ;
100+ } ) ;
101+ } ) ;
102+ }
103+
104+ private async fetchFromGit ( rootPath : string ) : Promise < any > {
79105 console . debug ( "[DEBUG]#### GitUtils fetchFromGit begin." ) ;
80- const gitRootPath = vscode . workspace . rootPath ;
81- exec ( 'git pull' , { cwd : gitRootPath } , ( error , stdout , stderr ) => {
82- if ( error ) {
83- vscode . window . showErrorMessage ( `Failed to fetch code: ${ error . message } ` ) ;
84- return ;
85- }
86- if ( stderr ) {
87- vscode . window . showErrorMessage ( `Failed to fetch code: ${ stderr } ` ) ;
88- return ;
89- }
106+ try {
107+ await this . gitPull ( rootPath ) ;
90108 vscode . window . showInformationMessage ( 'Code has been successfully fetched from Git repo!' ) ;
91- } ) ;
109+ } catch ( error ) {
110+ vscode . window . showErrorMessage ( `Failed to fetch code: ${ error . message } ` ) ;
111+ }
92112 }
93113
94114}
0 commit comments