1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Allow unstaged files to be reverted

This commit is contained in:
Nick O'Leary 2017-12-11 17:05:12 +00:00
parent 604e3068b2
commit bb59cd5742
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
8 changed files with 180 additions and 81 deletions

View File

@ -72,8 +72,9 @@
"abort-merge":"Git merge aborted", "abort-merge":"Git merge aborted",
"loaded":"Project '"+msg.project+"' loaded", "loaded":"Project '"+msg.project+"' loaded",
"updated":"Project '"+msg.project+"' updated", "updated":"Project '"+msg.project+"' updated",
"pull":"Project '"+msg.project+"' reloaded" "pull":"Project '"+msg.project+"' reloaded",
}[msg.action] "revert": "Project '"+msg.project+"' reloaded"
}[msg.action];
RED.notify(message); RED.notify(message);
RED.sidebar.info.refresh() RED.sidebar.info.refresh()
}); });

View File

@ -134,6 +134,7 @@ RED.projects.settings = (function() {
}, },
200: function(data) { 200: function(data) {
done(null,data); done(null,data);
RED.sidebar.versionControl.refresh(true);
}, },
400: { 400: {
'unexpected_error': function(error) { 'unexpected_error': function(error) {
@ -197,6 +198,7 @@ RED.projects.settings = (function() {
done(error,null); done(error,null);
}, },
200: function(data) { 200: function(data) {
RED.sidebar.versionControl.refresh(true);
done(null,data); done(null,data);
}, },
400: { 400: {
@ -326,6 +328,7 @@ RED.projects.settings = (function() {
done(error,null); done(error,null);
}, },
200: function(data) { 200: function(data) {
RED.sidebar.versionControl.refresh(true);
done(null,data); done(null,data);
}, },
400: { 400: {
@ -900,6 +903,7 @@ RED.projects.settings = (function() {
}, },
200: function(data) { 200: function(data) {
activeProject = data; activeProject = data;
RED.sidebar.versionControl.refresh(true);
updateForm(); updateForm();
done(); done();
}, },

View File

@ -39,26 +39,7 @@ RED.sidebar.versionControl = (function() {
var isMerging; var isMerging;
// TODO: DRY projectSummary.js function viewFileDiff(entry,state) {
function createChangeEntry(row, entry, status, state) {
row.addClass("sidebar-version-control-change-entry");
var container = $('<div>').appendTo(row);
if (entry.label) {
row.addClass('node-info-none');
container.text(entry.label);
return;
}
var icon = $('<i class=""></i>').appendTo(container);
var label = $('<span>').appendTo(container);
var bg = $('<div class="button-group"></div>').appendTo(row);
var viewDiffButton = $('<button class="editor-button editor-button-small"><i class="fa fa-'+(state==='unmerged'?'columns':'eye')+'"></i></button>')
.appendTo(bg)
.click(function(evt) {
evt.preventDefault();
var activeProject = RED.projects.getActiveProject(); var activeProject = RED.projects.getActiveProject();
var diffTarget = (state === 'staged')?"index":"tree"; var diffTarget = (state === 'staged')?"index":"tree";
utils.sendRequest({ utils.sendRequest({
@ -133,7 +114,80 @@ RED.sidebar.versionControl = (function() {
} }
} }
}) })
}
function createChangeEntry(row, entry, status, state) {
row.addClass("sidebar-version-control-change-entry");
var container = $('<div>').appendTo(row);
if (entry.label) {
row.addClass('node-info-none');
container.text(entry.label);
return;
}
var icon = $('<i class=""></i>').appendTo(container);
var entryLink = $('<a href="#">')
.appendTo(container)
.click(function(e) {
e.preventDefault();
viewFileDiff(entry,state);
});
var label = $('<span>').appendTo(entryLink);
var entryTools = $('<div class="sidebar-version-control-change-entry-tools">').appendTo(row);
var bg;
var revertButton;
if (state === 'unstaged') {
bg = $('<span class="button-group" style="margin-right: 5px;"></span>').appendTo(entryTools);
revertButton = $('<button class="editor-button editor-button-small"><i class="fa fa-reply"></i></button>')
.appendTo(bg)
.click(function(evt) {
evt.preventDefault();
var spinner = utils.addSpinnerOverlay(container).addClass('projects-dialog-spinner-contain');
var notification = RED.notify("Are you sure you want to revert the changes to '"+entry.file+"'? This cannot be undone.", {
type: "warning",
modal: true,
fixed: true,
buttons: [
{
text: RED._("common.label.cancel"),
click: function() {
spinner.remove();
notification.close();
}
},{
text: 'Revert changes',
click: function() {
notification.close();
var activeProject = RED.projects.getActiveProject();
var url = "projects/"+activeProject.name+"/files/_/"+entry.file;
var options = {
url: url,
type: "DELETE",
responses: {
200: function(data) {
spinner.remove();
},
400: {
'unexpected_error': function(error) {
spinner.remove();
console.log(error);
// done(error,null);
}
}
}
}
utils.sendRequest(options);
}
}
]
}) })
});
}
bg = $('<span class="button-group"></span>').appendTo(entryTools);
if (state !== 'unmerged') { if (state !== 'unmerged') {
$('<button class="editor-button editor-button-small"><i class="fa fa-'+((state==='unstaged')?"plus":"minus")+'"></i></button>') $('<button class="editor-button editor-button-small"><i class="fa fa-'+((state==='unstaged')?"plus":"minus")+'"></i></button>')
.appendTo(bg) .appendTo(bg)
@ -203,11 +257,10 @@ RED.sidebar.versionControl = (function() {
delete entry.spinner; delete entry.spinner;
} }
viewDiffButton.attr("disabled",(status === 'D' || status === '?')); if (revertButton) {
viewDiffButton.find("i") revertButton.toggle(status !== '?');
.toggleClass('fa-eye',!(status === 'D' || status === '?')) }
.toggleClass('fa-eye-slash',(status === 'D' || status === '?')) entryLink.toggleClass("disabled",(status === 'D' || status === '?'));
} }
entry["update"+((state==='unstaged')?"Unstaged":"Staged")](entry, status); entry["update"+((state==='unstaged')?"Unstaged":"Staged")](entry, status);
} }

View File

@ -444,7 +444,13 @@
span { span {
margin: 0 6px; margin: 0 6px;
} }
.button-group { a {
color: currentColor;
&.disabled {
pointer-events: none;
}
}
.sidebar-version-control-change-entry-tools {
position: absolute; position: absolute;
top: 4px; top: 4px;
right: 4px; right: 4px;
@ -455,7 +461,7 @@
} }
&:hover { &:hover {
.button-group { .sidebar-version-control-change-entry-tools {
display: block; display: block;
} }
} }

View File

@ -170,6 +170,22 @@ module.exports = {
}) })
}); });
// Revert a file
app.delete("/:id/files/_/*", needsPermission("projects.write"), function(req,res) {
var projectId = req.params.id;
var filePath = req.params[0];
runtime.storage.projects.revertFile(req.user, projectId,filePath).then(function() {
res.status(204).end();
})
.catch(function(err) {
console.log(err.stack);
res.status(400).json({error:"unexpected_error", message:err.toString()});
})
});
// Stage a file // Stage a file
app.post("/:id/stage/*", needsPermission("projects.write"), function(req,res) { app.post("/:id/stage/*", needsPermission("projects.write"), function(req,res) {
var projectName = req.params.id; var projectName = req.params.id;

View File

@ -329,6 +329,14 @@ Project.prototype.getFile = function (filePath,treeish) {
return fs.readFile(fspath.join(this.path,filePath),"utf8"); return fs.readFile(fspath.join(this.path,filePath),"utf8");
} }
}; };
Project.prototype.revertFile = function (filePath) {
var self = this;
return gitTools.revertFile(this.path, filePath).then(function() {
return self.load();
});
};
Project.prototype.status = function(user) { Project.prototype.status = function(user) {
var self = this; var self = this;

View File

@ -421,6 +421,10 @@ module.exports = {
return status.files; return status.files;
}) })
}, },
revertFile: function(cwd, filePath) {
var args = ["checkout",filePath];
return runGitCommand(args,cwd);
},
stageFile: function(cwd,file) { stageFile: function(cwd,file) {
var args = ["add"]; var args = ["add"];
if (Array.isArray(file)) { if (Array.isArray(file)) {

View File

@ -184,6 +184,12 @@ function getFile(user, project,filePath,sha) {
checkActiveProject(project); checkActiveProject(project);
return activeProject.getFile(filePath,sha); return activeProject.getFile(filePath,sha);
} }
function revertFile(user, project,filePath) {
checkActiveProject(project);
return activeProject.revertFile(filePath).then(function() {
return reloadActiveProject("revert");
})
}
function push(user, project,remoteBranchName,setRemote) { function push(user, project,remoteBranchName,setRemote) {
checkActiveProject(project); checkActiveProject(project);
return activeProject.push(user,remoteBranchName,setRemote); return activeProject.push(user,remoteBranchName,setRemote);
@ -413,6 +419,7 @@ module.exports = {
updateProject: updateProject, updateProject: updateProject,
getFiles: getFiles, getFiles: getFiles,
getFile: getFile, getFile: getFile,
revertFile: revertFile,
stageFile: stageFile, stageFile: stageFile,
unstageFile: unstageFile, unstageFile: unstageFile,
commit: commit, commit: commit,