Skip to content

Commit c1d8963

Browse files
authored
Merge pull request #689 from intersystems/issue-688
Fix Discard of deleted files
2 parents 7dabf65 + 9161b05 commit c1d8963

File tree

4 files changed

+84
-33
lines changed

4 files changed

+84
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Embedded Git commits settings when cloning empty repo to avert any issues
1717
- Fixed Import All options not importing the Embedded Git configuration file
1818
- Improved performance of IDE editing and baselining of decomposed productions
19+
- Fixed Discard / Stash not working on deletes (#688)
1920

2021
## [2.9.0] - 2025-01-09
2122

cls/SourceControl/Git/DiscardState.cls

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Property Timestamp As %TimeStamp [ Required ];
1515

1616
Property ExternalFile As %Boolean [ Required ];
1717

18+
/// Boolean tracking whether or not file was deleted as part of change
19+
Property Deleted As %Boolean;
20+
1821
Index BranchMap On Branch [ Type = bitmap ];
1922

2023
Method RestoreToFileTree()
@@ -24,17 +27,22 @@ Method RestoreToFileTree()
2427
if ('##class(%File).DirectoryExists(dir)) {
2528
do ##class(%File).CreateDirectoryChain(dir)
2629
}
30+
31+
if (..Deleted) {
32+
do ##class(%File).Delete(..FullExternalName)
33+
} else {
2734

28-
// Recreate File
29-
set fileStream = ##class(%Stream.FileCharacter).%New()
30-
set fileStream.Filename = ..FullExternalName
31-
$$$ThrowOnError(fileStream.CopyFrom(..Contents))
32-
$$$ThrowOnError(fileStream.%Save())
33-
34-
// Add file to source-control / IRIS
35-
if '..ExternalFile {
36-
do ##class(SourceControl.Git.Utils).ImportItem(..Name, 1, 1, 1)
37-
do ##class(SourceControl.Git.Utils).AddToServerSideSourceControl(..Name)
35+
// Recreate File
36+
set fileStream = ##class(%Stream.FileCharacter).%New()
37+
set fileStream.Filename = ..FullExternalName
38+
$$$ThrowOnError(fileStream.CopyFrom(..Contents))
39+
$$$ThrowOnError(fileStream.%Save())
40+
41+
// Add file to source-control / IRIS
42+
if '..ExternalFile {
43+
do ##class(SourceControl.Git.Utils).ImportItem(..Name, 1, 1, 1)
44+
do ##class(SourceControl.Git.Utils).AddToServerSideSourceControl(..Name)
45+
}
3846
}
3947

4048
// Delete discard record
@@ -57,11 +65,16 @@ ClassMethod SaveDiscardState(InternalName As %String, name As %String) As %Statu
5765
set discardState.ExternalFile = 0
5866
}
5967
// Copy over file contents
60-
set fileStream = ##class(%Stream.FileCharacter).%New()
61-
set fileStream.Filename = discardState.FullExternalName
62-
do fileStream.%Open()
63-
do discardState.Contents.CopyFrom(fileStream)
64-
do fileStream.%Close()
68+
if (##class(%File).Exists(discardState.FullExternalName)) {
69+
set fileStream = ##class(%Stream.FileCharacter).%New()
70+
set fileStream.Filename = discardState.FullExternalName
71+
do fileStream.%Open()
72+
do discardState.Contents.CopyFrom(fileStream)
73+
do fileStream.%Close()
74+
} else {
75+
set discardState.Deleted = 1
76+
do discardState.Contents.Write("Deleted File")
77+
}
6578

6679
// Save extra information
6780
set discardState.Username = $USERNAME
@@ -126,6 +139,9 @@ Storage Default
126139
<Value name="9">
127140
<Value>ExternalFile</Value>
128141
</Value>
142+
<Value name="10">
143+
<Value>Deleted</Value>
144+
</Value>
129145
</Data>
130146
<DataLocation>^SourceControl22B9.DiscardStateD</DataLocation>
131147
<DefaultData>DiscardStateDefaultData</DefaultData>

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

Lines changed: 26 additions & 9 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
}
@@ -3005,20 +3014,28 @@ webui.NewChangedFilesView = function(workspaceView) {
30053014

30063015
self.stash = function() {
30073016
var selectedFilesAsString = selectedItems.join(" ");
3008-
webui.git("add -- " + selectedFilesAsString, function(output) {
3017+
webui.git("add -- " + selectedFilesAsString, undefined, function(output) {
30093018
webui.git("stash push --include-untracked -- " + selectedFilesAsString, function(output) {
30103019
webui.showSuccess(output);
30113020
workspaceView.update();
30123021
});
3022+
},function(output) {
3023+
if (output.includes("did not match any files")) {
3024+
webui.showError("Stashing deleted items does not work. Please discard the operation instead.")
3025+
} else {
3026+
webui.showError(output);
3027+
}
30133028
});
30143029
}
30153030

30163031
self.discard = function() {
3017-
webui.git_command(["add", "--"].concat(selectedItems), function() {
3032+
function restoreCommand(data) {
30183033
webui.git_command(["restore", "--staged", "--worktree", "--"].concat(selectedItems), function() {
30193034
workspaceView.update();
30203035
});
3021-
});
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);
30223039
}
30233040

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

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

Lines changed: 26 additions & 9 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
}
@@ -3005,20 +3014,28 @@ webui.NewChangedFilesView = function(workspaceView) {
30053014

30063015
self.stash = function() {
30073016
var selectedFilesAsString = selectedItems.join(" ");
3008-
webui.git("add -- " + selectedFilesAsString, function(output) {
3017+
webui.git("add -- " + selectedFilesAsString, undefined, function(output) {
30093018
webui.git("stash push --include-untracked -- " + selectedFilesAsString, function(output) {
30103019
webui.showSuccess(output);
30113020
workspaceView.update();
30123021
});
3022+
},function(output) {
3023+
if (output.includes("did not match any files")) {
3024+
webui.showError("Stashing deleted items does not work. Please discard the operation instead.")
3025+
} else {
3026+
webui.showError(output);
3027+
}
30133028
});
30143029
}
30153030

30163031
self.discard = function() {
3017-
webui.git_command(["add", "--"].concat(selectedItems), function() {
3032+
function restoreCommand(data) {
30183033
webui.git_command(["restore", "--staged", "--worktree", "--"].concat(selectedItems), function() {
30193034
workspaceView.update();
30203035
});
3021-
});
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);
30223039
}
30233040

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

0 commit comments

Comments
 (0)