@@ -125,6 +125,27 @@ webui.gitVersion = function() {
125125 } )
126126}
127127
128+ webui . parseGitResponse = function ( data ) {
129+ var matches = data . match ( / ( [ \S \s ] * ) G i t - S t d e r r - L e n g t h : \s ( \d * ) [ \r \n ] { 1 , 2 } G i t - R e t u r n - C o d e : \s ( \d * ) / ) ;
130+ if ( ! matches || ( matches . length < 4 ) ) {
131+ return {
132+ output : '' ,
133+ message : '' ,
134+ rcode : 1
135+ }
136+ }
137+ var messageData = matches [ 1 ] . replace ( / ( \r \n ) / gm, "\n" ) ;
138+ var errorLength = parseInt ( matches [ 2 ] , 10 ) ;
139+ var boundary = messageData . length - errorLength ;
140+ var stdout = messageData . substring ( 0 , boundary )
141+ var stderr = messageData . substring ( boundary ) ;
142+ return {
143+ output : stdout ,
144+ message : stderr ,
145+ rcode : matches [ 3 ]
146+ } ;
147+ }
148+
128149webui . git_command = function ( command , callback ) {
129150 $ . ajax ( {
130151 url : "git-command" ,
@@ -134,32 +155,10 @@ webui.git_command = function(command, callback) {
134155 command : command
135156 } ) ,
136157 success : function ( data ) {
137- // Convention : last lines are footer meta data like headers. An empty line marks the start if the footers
138- var footers = { } ;
139- var fIndex = data . length ;
140- while ( true ) {
141- var oldFIndex = fIndex ;
142- fIndex = data . lastIndexOf ( "\r\n" , fIndex - 1 ) ;
143- var line = data . substring ( fIndex + 2 , oldFIndex ) ;
144- if ( line . length > 0 ) {
145- var footer = line . split ( ": " ) ;
146- footers [ footer [ 0 ] ] = footer [ 1 ] ;
147- } else {
148- break ;
149- }
150- }
151- // Trims the the data variable to remove the footers extracted in the loop.
152- // Windows adds \r\n for every line break but the Git-Stderr-Length variable,
153- // counts it as only one character, throwing off the message length.
154- var trimmedData = data . substring ( 0 , fIndex ) . replace ( / ( \r \n ) / gm, "\n" ) ;
155- var fIndex = trimmedData . length
156-
157- var messageLength = parseInt ( footers [ "Git-Stderr-Length" ] ) ;
158- var messageStartIndex = fIndex - messageLength ;
159- var message = trimmedData . substring ( messageStartIndex , fIndex ) ;
160-
161- var output = trimmedData . substring ( 0 , messageStartIndex ) ;
162- var rcode = parseInt ( footers [ "Git-Return-Code" ] ) ;
158+ var result = webui . parseGitResponse ( data ) ;
159+ var rcode = result . rcode ;
160+ var output = result . output ;
161+ var message = result . message ;
163162
164163 if ( rcode == 0 ) {
165164 if ( callback ) {
@@ -197,13 +196,8 @@ webui.git_command = function(command, callback) {
197196 }
198197 } ,
199198 error : function ( data ) {
200- var trimmedData = data . substring ( 0 , fIndex ) . replace ( / ( \r \n ) / gm, "\n" ) ;
201- var fIndex = trimmedData . length
202-
203- var messageLength = parseInt ( footers [ "Git-Stderr-Length" ] ) ;
204- var messageStartIndex = fIndex - messageLength ;
205- var message = trimmedData . substring ( messageStartIndex , fIndex ) ;
206- webui . showError ( message ) ;
199+ var trimmedData = data . replace ( / ( \r \n ) / gm, "\n" ) ;
200+ webui . showError ( trimmedData ) ;
207201 } ,
208202 } ) ;
209203}
@@ -233,32 +227,10 @@ webui.git = function(cmd, arg1, arg2, arg3, arg4) {
233227
234228 $ . post ( "git" , { command : cmd } , function ( data , status , xhr ) {
235229 if ( xhr . status == 200 ) {
236- // Convention : last lines are footer meta data like headers. An empty line marks the start if the footers
237- var footers = { } ;
238- var fIndex = data . length ;
239- while ( true ) {
240- var oldFIndex = fIndex ;
241- fIndex = data . lastIndexOf ( "\r\n" , fIndex - 1 ) ;
242- var line = data . substring ( fIndex + 2 , oldFIndex ) ;
243- if ( line . length > 0 ) {
244- var footer = line . split ( ": " ) ;
245- footers [ footer [ 0 ] ] = footer [ 1 ] ;
246- } else {
247- break ;
248- }
249- }
250- // Trims the the data variable to remove the footers extracted in the loop.
251- // Windows adds \r\n for every line break but the Git-Stderr-Length variable,
252- // counts it as only one character, throwing off the message length.
253- var trimmedData = data . substring ( 0 , fIndex ) . replace ( / ( \r \n ) / gm, "\n" ) ;
254- var fIndex = trimmedData . length
255-
256- var messageLength = parseInt ( footers [ "Git-Stderr-Length" ] ) ;
257- var messageStartIndex = fIndex - messageLength ;
258- var message = trimmedData . substring ( messageStartIndex , fIndex ) ;
259-
260- var output = trimmedData . substring ( 0 , messageStartIndex ) ;
261- var rcode = parseInt ( footers [ "Git-Return-Code" ] ) ;
230+ var result = webui . parseGitResponse ( data ) ;
231+ var rcode = result . rcode ;
232+ var output = result . output ;
233+ var message = result . message ;
262234
263235 if ( rcode == 0 ) {
264236 if ( callback ) {
@@ -295,15 +267,15 @@ webui.git = function(cmd, arg1, arg2, arg3, arg4) {
295267 }
296268 }
297269 } else {
298- if ( errorCallback ) {
299- errorCallback ( message ) ;
270+ if ( errorCallback ) {
271+ errorCallback ( data ) ;
300272 } else {
301- webui . showError ( message ) ;
273+ webui . showError ( data ) ;
302274 }
303275 }
304276 } , "text" )
305277 . fail ( function ( xhr , status , error ) {
306- webui . showError ( "Git webui server not running " ) ;
278+ webui . showError ( "An internal error occurred and has been logged. " ) ;
307279 } ) ;
308280} ;
309281
@@ -1401,21 +1373,17 @@ webui.StashListView = function(stashView) {
14011373 webui . git ( "stash list --format='%gd::%ch::%cL::%cN::%gs'" , function ( data ) {
14021374 var start = 0 ;
14031375 var count = 0 ;
1404- while ( true ) {
1405- var end = data . indexOf ( "\n" , start ) ;
1406- if ( end != - 1 ) {
1407- var len = end - start ;
1408- } else {
1409- break ;
1376+ var lines = data . split ( "\n" ) ;
1377+ for ( var i = 0 ; i < lines . length ; i ++ ) {
1378+ if ( lines [ i ] . length == 0 ) {
1379+ continue ;
14101380 }
1411- var entry = new Entry ( self , data . substring ( start , start + len ) ) ;
1381+ var entry = new Entry ( self , lines [ i ] ) ;
14121382 if ( start == 0 ) {
14131383 entry . select ( ) ;
14141384 }
14151385 content . appendChild ( entry . element ) ;
1416-
1417- start = end + 1 ;
1418- ++ count ;
1386+ count ++ ;
14191387 }
14201388 if ( count == 0 ) {
14211389 var emptyStash = $ ( '<h4 class="empty-stash">You have no stashed changes.</h4>' ) ;
@@ -1476,6 +1444,9 @@ webui.StashListView = function(stashView) {
14761444 self . message = ""
14771445
14781446 var pieces = data . split ( / : : | : \s / gm) ;
1447+ if ( pieces . length < 5 ) {
1448+ return ;
1449+ }
14791450 self . stashIndex = pieces [ 0 ] . substring ( pieces [ 0 ] . indexOf ( '{' ) + 1 , pieces [ 0 ] . indexOf ( '}' ) ) ;
14801451 self . date = pieces [ 1 ] ;
14811452 self . authorEmail = pieces [ 2 ] ;
@@ -1583,6 +1554,10 @@ webui.DiffView = function(sideBySide, hunkSelectionAllowed, parent, stashedCommi
15831554 }
15841555 } ;
15851556
1557+ self . clear = function ( ) {
1558+ self . refresh ( "" ) ;
1559+ }
1560+
15861561 self . reRun = function ( ) {
15871562 self . update ( this . cmd , this . diffOpts , this . file , this . mode )
15881563 }
@@ -1911,6 +1886,7 @@ webui.DiffView = function(sideBySide, hunkSelectionAllowed, parent, stashedCommi
19111886 webui . git ( "stash pop stash@{" + stashIndex + "}" , function ( output ) {
19121887 webui . showSuccess ( output ) ;
19131888 parent . stashView . update ( 0 ) ;
1889+ self . clear ( ) ;
19141890 } ) ;
19151891 }
19161892
@@ -1922,6 +1898,7 @@ webui.DiffView = function(sideBySide, hunkSelectionAllowed, parent, stashedCommi
19221898 webui . git ( "stash drop stash@{" + stashIndex + "}" , function ( output ) {
19231899 webui . showSuccess ( output . substring ( output . indexOf ( "Dropped" ) ) ) ;
19241900 parent . stashView . update ( 0 ) ;
1901+ self . clear ( ) ;
19251902 } ) ;
19261903 }
19271904
@@ -2484,7 +2461,7 @@ webui.WorkspaceView = function(mainView) {
24842461 self . update = function ( mode ) {
24852462 self . newChangedFilesView . update ( ) ;
24862463 if ( self . newChangedFilesView . getSelectedItemsCount ( ) == 0 ) {
2487- self . diffView . update ( undefined , undefined , undefined , mode ) ;
2464+ self . diffView . clear ( ) ;
24882465 }
24892466 } ;
24902467
0 commit comments