@@ -110,6 +110,89 @@ webui.showWarning = function(message) {
110110 '</div>' ) . appendTo ( messageBox ) ;
111111}
112112
113+ webui . git_command = function ( command , callback ) {
114+ $ . ajax ( {
115+ url : "git-command" ,
116+ type : "POST" ,
117+ contentType : 'application/json' ,
118+ data : JSON . stringify ( {
119+ command : command
120+ } ) ,
121+ success : function ( data ) {
122+ // Convention : last lines are footer meta data like headers. An empty line marks the start if the footers
123+ var footers = { } ;
124+ var fIndex = data . length ;
125+ while ( true ) {
126+ var oldFIndex = fIndex ;
127+ fIndex = data . lastIndexOf ( "\r\n" , fIndex - 1 ) ;
128+ var line = data . substring ( fIndex + 2 , oldFIndex ) ;
129+ if ( line . length > 0 ) {
130+ var footer = line . split ( ": " ) ;
131+ footers [ footer [ 0 ] ] = footer [ 1 ] ;
132+ } else {
133+ break ;
134+ }
135+ }
136+ // Trims the the data variable to remove the footers extracted in the loop.
137+ // Windows adds \r\n for every line break but the Git-Stderr-Length variable,
138+ // counts it as only one character, throwing off the message length.
139+ var trimmedData = data . substring ( 0 , fIndex ) . replace ( / ( \r \n ) / gm, "\n" ) ;
140+ var fIndex = trimmedData . length
141+
142+ var messageLength = parseInt ( footers [ "Git-Stderr-Length" ] ) ;
143+ var messageStartIndex = fIndex - messageLength ;
144+ var message = trimmedData . substring ( messageStartIndex , fIndex ) ;
145+
146+ var output = trimmedData . substring ( 0 , messageStartIndex ) ;
147+ var rcode = parseInt ( footers [ "Git-Return-Code" ] ) ;
148+
149+ if ( rcode == 0 ) {
150+ if ( callback ) {
151+ callback ( output ) ;
152+ }
153+ // Return code is 0 but there is stderr output: this is a warning message
154+ if ( message . length > 0 ) {
155+ if ( warningCallback ) {
156+ warningCallback ( message ) ;
157+ } else {
158+ webui . showWarning ( message ) ;
159+ }
160+ }
161+ } else {
162+ var displayMessage = ""
163+ if ( output . length > 0 ) {
164+ displayMessage += ( output + "\n" ) ;
165+ }
166+ if ( message . length > 0 ) {
167+ displayMessage += message ;
168+ }
169+ if ( displayMessage . length > 0 ) {
170+ // if(errorCallback) {
171+ // errorCallback(displayMessage);
172+ // } else{
173+ if ( displayMessage . indexOf ( "self.document.Login" ) != - 1 ) {
174+ location . reload ( ) ;
175+ return false ;
176+ }
177+ webui . showError ( displayMessage ) ;
178+ //}
179+ } else {
180+ webui . showError ( "The command <pre>" + command . join ( " " ) + "</pre> failed because of an unknown reason. Returned response: \n\n" + data )
181+ }
182+ }
183+ } ,
184+ error : function ( data ) {
185+ var trimmedData = data . substring ( 0 , fIndex ) . replace ( / ( \r \n ) / gm, "\n" ) ;
186+ var fIndex = trimmedData . length
187+
188+ var messageLength = parseInt ( footers [ "Git-Stderr-Length" ] ) ;
189+ var messageStartIndex = fIndex - messageLength ;
190+ var message = trimmedData . substring ( messageStartIndex , fIndex ) ;
191+ webui . showError ( message ) ;
192+ } ,
193+ } ) ;
194+ }
195+
113196webui . git = function ( cmd , arg1 , arg2 , arg3 , arg4 ) {
114197 // cmd = git command line arguments
115198 // other arguments = optional stdin content and a callback function.
@@ -2649,43 +2732,32 @@ webui.NewChangedFilesView = function(workspaceView) {
26492732 }
26502733
26512734 self . amend = function ( message , details ) {
2652- var selectedFilesAsString = selectedItems . join ( " " ) ;
2653- message = self . doubleQuotesToSingleQuotes ( message ) ;
2654- details = self . doubleQuotesToSingleQuotes ( details ) ;
26552735
26562736 if ( self . commitMsgEmpty ( ) ) {
2657- webui . git ( "add " + selectedFilesAsString ) ;
2658- webui . git ( " commit --amend --no-edit -- " + selectedFilesAsString , function ( output ) {
2737+ webui . git_command ( [ "add" ] . concat ( selectedItems ) ) ;
2738+ webui . git_command ( [ ' commit' , ' --amend' , ' --no-edit' , '--' ] . concat ( selectedItems ) , function ( output ) {
26592739 webui . showSuccess ( output ) ;
26602740 workspaceView . update ( ) ;
2661- } )
2741+ } ) ;
26622742 } else if ( selectedItems . length != 0 ) {
2663- webui . git ( "add " + selectedFilesAsString ) ;
2664- webui . git ( 'commit --amend -m "' + message + '" -m "' + details + '" -- ' + selectedFilesAsString , function ( output ) {
2743+ webui . git_command ( [ "add" ] . concat ( selectedItems ) ) ;
2744+ webui . git_command ( [ 'commit' , ' --amend' , '-m' , message , '-m' , details , '--' ] . concat ( selectedItems ) , function ( output ) {
26652745 webui . showSuccess ( output ) ;
26662746 workspaceView . update ( ) ;
2667- } )
2747+ } ) ;
26682748 } else {
2669- webui . git ( 'commit --amend --allow-empty -m "' + message + '" -m "' + details + '"' , function ( output ) {
2749+ webui . git_command ( [ 'commit' , ' --amend' , ' --allow-empty' , '-m' , message , '-m' , details ] , function ( output ) {
26702750 webui . showSuccess ( output ) ;
26712751 workspaceView . update ( ) ;
2672- } )
2752+ } ) ;
26732753 }
26742754
26752755
26762756 }
26772757
2678- self . doubleQuotesToSingleQuotes = function ( string ) {
2679- return string . replace ( / " / g, "'" ) ;
2680- }
2681-
26822758 self . commit = function ( message , details ) {
2683- var selectedFilesAsString = selectedItems . join ( " " ) ;
2684- message = self . doubleQuotesToSingleQuotes ( message ) ;
2685- details = self . doubleQuotesToSingleQuotes ( details ) ;
2686-
2687- webui . git ( "add " + selectedFilesAsString ) ;
2688- webui . git ( 'commit -m "' + message + '" -m "' + details + '" -- ' + selectedFilesAsString , function ( output ) {
2759+ webui . git_command ( [ "add" ] . concat ( selectedItems ) ) ;
2760+ webui . git_command ( [ 'commit' , '-m' , message , '-m' , details , '--' ] . concat ( selectedItems ) , function ( output ) {
26892761 webui . showSuccess ( output ) ;
26902762 workspaceView . update ( ) ;
26912763 } ) ;
0 commit comments