Skip to content

Commit 679ef41

Browse files
committed
Fix file added already before discard
1 parent d37a062 commit 679ef41

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

git-webui/release/share/git-webui/webui/js/git-webui.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ webui.parseGitResponse = function(data) {
146146
};
147147
}
148148

149-
webui.processGitResponse = function(data, command, callback, warningCallback) {
149+
webui.processGitResponse = function(data, command, callback, warningCallback,errorCallback) {
150150
var result = webui.parseGitResponse(data);
151151
var rcode = result.rcode;
152152
var output = result.output;
@@ -177,15 +177,20 @@ webui.processGitResponse = function(data, command, callback, warningCallback) {
177177
location.reload();
178178
return false;
179179
}
180-
webui.showError(displayMessage);
180+
if (errorCallback) {
181+
errorCallback(displayMessage);
182+
} else {
183+
webui.showError(displayMessage);
184+
}
185+
181186
//}
182187
} else {
183188
webui.showError("The command <pre>"+command.join(" ")+"</pre> failed because of an unknown reason. Returned response: \n\n"+data)
184189
}
185190
}
186191
}
187192

188-
webui.git_command = function(command, callback, warningCallback) {
193+
webui.git_command = function(command, callback, warningCallback, errorCallback) {
189194
$.ajax({
190195
url: "git-command",
191196
type: "POST",
@@ -194,11 +199,15 @@ webui.git_command = function(command, callback, warningCallback) {
194199
command: command
195200
}),
196201
success: function(data) {
197-
webui.processGitResponse(data, command, callback, warningCallback);
202+
webui.processGitResponse(data, command, callback, warningCallback, errorCallback);
198203
},
199204
error: function(data) {
200-
var trimmedData = data.replace(/(\r\n)/gm, "\n");
201-
webui.showError(trimmedData);
205+
if (errorCallback) {
206+
errorCallback(data.replace(/(\r\n)/gm, "\n"));
207+
} else {
208+
var trimmedData = data.replace(/(\r\n)/gm, "\n");
209+
webui.showError(trimmedData);
210+
}
202211
},
203212
});
204213
}
@@ -3020,11 +3029,13 @@ webui.NewChangedFilesView = function(workspaceView) {
30203029
}
30213030

30223031
self.discard = function() {
3023-
webui.git_command(["add", "--"].concat(selectedItems), function() {
3032+
function restoreCommand(data) {
30243033
webui.git_command(["restore", "--staged", "--worktree", "--"].concat(selectedItems), function() {
30253034
workspaceView.update();
30263035
});
3027-
});
3036+
}
3037+
// We want to try to run restore even if add fails (since the file might have already been added)
3038+
webui.git_command(["add", "--"].concat(selectedItems), restoreCommand,undefined,restoreCommand);
30283039
}
30293040

30303041
self.amend = function(message, details) {

git-webui/src/share/git-webui/webui/js/git-webui.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ webui.parseGitResponse = function(data) {
146146
};
147147
}
148148

149-
webui.processGitResponse = function(data, command, callback, warningCallback) {
149+
webui.processGitResponse = function(data, command, callback, warningCallback,errorCallback) {
150150
var result = webui.parseGitResponse(data);
151151
var rcode = result.rcode;
152152
var output = result.output;
@@ -177,15 +177,20 @@ webui.processGitResponse = function(data, command, callback, warningCallback) {
177177
location.reload();
178178
return false;
179179
}
180-
webui.showError(displayMessage);
180+
if (errorCallback) {
181+
errorCallback(displayMessage);
182+
} else {
183+
webui.showError(displayMessage);
184+
}
185+
181186
//}
182187
} else {
183188
webui.showError("The command <pre>"+command.join(" ")+"</pre> failed because of an unknown reason. Returned response: \n\n"+data)
184189
}
185190
}
186191
}
187192

188-
webui.git_command = function(command, callback, warningCallback) {
193+
webui.git_command = function(command, callback, warningCallback, errorCallback) {
189194
$.ajax({
190195
url: "git-command",
191196
type: "POST",
@@ -194,11 +199,15 @@ webui.git_command = function(command, callback, warningCallback) {
194199
command: command
195200
}),
196201
success: function(data) {
197-
webui.processGitResponse(data, command, callback, warningCallback);
202+
webui.processGitResponse(data, command, callback, warningCallback, errorCallback);
198203
},
199204
error: function(data) {
200-
var trimmedData = data.replace(/(\r\n)/gm, "\n");
201-
webui.showError(trimmedData);
205+
if (errorCallback) {
206+
errorCallback(data.replace(/(\r\n)/gm, "\n"));
207+
} else {
208+
var trimmedData = data.replace(/(\r\n)/gm, "\n");
209+
webui.showError(trimmedData);
210+
}
202211
},
203212
});
204213
}
@@ -3020,11 +3029,13 @@ webui.NewChangedFilesView = function(workspaceView) {
30203029
}
30213030

30223031
self.discard = function() {
3023-
webui.git_command(["add", "--"].concat(selectedItems), function() {
3032+
function restoreCommand(data) {
30243033
webui.git_command(["restore", "--staged", "--worktree", "--"].concat(selectedItems), function() {
30253034
workspaceView.update();
30263035
});
3027-
});
3036+
}
3037+
// We want to try to run restore even if add fails (since the file might have already been added)
3038+
webui.git_command(["add", "--"].concat(selectedItems), restoreCommand,undefined,restoreCommand);
30283039
}
30293040

30303041
self.amend = function(message, details) {

0 commit comments

Comments
 (0)