Skip to content

Commit 7a89e3c

Browse files
committed
Check the global git config to ensure its setup
1 parent 64607df commit 7a89e3c

4 files changed

Lines changed: 54 additions & 12 deletions

File tree

editor/js/ui/projects/projects.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ RED.projects = (function() {
6767
var gitEmailInput;
6868
return {
6969
content: function(options) {
70+
var isGlobalConfig = false;
71+
var existingGitSettings = RED.settings.get('git');
72+
if (existingGitSettings && existingGitSettings.user) {
73+
existingGitSettings = existingGitSettings.user;
74+
} else if (RED.settings.git && RED.settings.git.globalUser) {
75+
isGlobalConfig = true;
76+
existingGitSettings = RED.settings.git.globalUser;
77+
}
78+
79+
var validateForm = function() {
80+
var name = gitUsernameInput.val().trim();
81+
var email = gitEmailInput.val().trim();
82+
var valid = name.length > 0 && email.length > 0;
83+
$("#projects-dialog-git-config").prop('disabled',!valid).toggleClass('disabled ui-button-disabled ui-state-disabled',!valid);
84+
85+
}
7086

7187
var container = $('<div class="projects-dialog-screen-start"></div>');
7288
migrateProjectHeader.appendTo(container);
@@ -75,23 +91,26 @@ RED.projects = (function() {
7591
$('<p>').text("Setup your version control client").appendTo(body);
7692
$('<p>').text("Node-RED uses the open source tool Git for version control. It tracks changes to your project files and lets you push them to remote repositories.").appendTo(body);
7793
$('<p>').text("When you commit a set of changes, Git records who made the changes with a username and email address. The Username can be anything you want - it does not need to be your real name.").appendTo(body);
78-
$('<p>').text("If your Git client is already configured, you can skip this step.").appendTo(body);
79-
80-
var currentGitSettings = RED.settings.get('git') || {};
81-
currentGitSettings.user = currentGitSettings.user || {};
8294

95+
if (isGlobalConfig) {
96+
$('<p>').text("Your Git client is already configured with the details below.").appendTo(body);
97+
}
98+
$('<p>').text("You can change these settings later under the 'Git config' tab of the settings dialog.").appendTo(body);
8399

84100
var row = $('<div class="form-row"></div>').appendTo(body);
85101
$('<label for="">Username</label>').appendTo(row);
86-
gitUsernameInput = $('<input type="text">').val(currentGitSettings.user.name||"").appendTo(row);
102+
gitUsernameInput = $('<input type="text">').val(existingGitSettings.name||"").appendTo(row);
87103
// $('<div style="position:relative;"></div>').text("This does not need to be your real name").appendTo(row);
104+
gitUsernameInput.on("change keyup paste",validateForm);
88105

89106
row = $('<div class="form-row"></div>').appendTo(body);
90107
$('<label for="">Email</label>').appendTo(row);
91-
gitEmailInput = $('<input type="text">').val(currentGitSettings.user.email||"").appendTo(row);
108+
gitEmailInput = $('<input type="text">').val(existingGitSettings.email||"").appendTo(row);
109+
gitEmailInput.on("change keyup paste",validateForm);
92110
// $('<div style="position:relative;"></div>').text("Something something email").appendTo(row);
93111
setTimeout(function() {
94112
gitUsernameInput.focus();
113+
validateForm();
95114
},50);
96115
return container;
97116
},
@@ -104,6 +123,7 @@ RED.projects = (function() {
104123
}
105124
},
106125
{
126+
id: "projects-dialog-git-config",
107127
text: "Next", // TODO: nls
108128
class: "primary",
109129
click: function() {

red/api/editor/settings.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ module.exports = {
5959
flow: runtime.storage.projects.getFlowFilename(),
6060
credentials: runtime.storage.projects.getCredentialsFilename()
6161
}
62+
safeSettings.git = {
63+
globalUser: runtime.storage.projects.getGlobalGitUser()
64+
}
6265
}
6366

6467
safeSettings.flowEncryptionType = runtime.nodes.getCredentialKeyType();

red/runtime/storage/localfilesystem/projects/git/index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,27 @@ module.exports = {
369369
init: function(_settings,_runtime) {
370370
log = _runtime.log
371371
return new Promise(function(resolve,reject) {
372-
runGitCommand(["--version"]).then(function(output) {
373-
var m = / (\d\S+)/.exec(output);
372+
Promise.all([
373+
runGitCommand(["--version"]),
374+
runGitCommand(["config","--global","user.name"]).catch(err=>""),
375+
runGitCommand(["config","--global","user.email"]).catch(err=>"")
376+
]).then(function(output) {
377+
var m = / (\d\S+)/.exec(output[0]);
374378
gitVersion = m[1];
375-
resolve(gitVersion);
379+
var globalUserName = output[1].trim();
380+
var globalUserEmail = output[2].trim();
381+
var result = {
382+
version: gitVersion
383+
};
384+
if (globalUserName && globalUserEmail) {
385+
result.user = {
386+
name: globalUserName,
387+
email: globalUserEmail
388+
}
389+
}
390+
resolve(result);
376391
}).catch(function(err) {
392+
console.log(err);
377393
resolve(null);
378394
});
379395
});

red/runtime/storage/localfilesystem/projects/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ var projectLogMessages = [];
3737
var projectsDir;
3838
var activeProject
3939

40+
var globalGitUser = false;
41+
4042
function init(_settings, _runtime) {
4143
settings = _settings;
4244
runtime = _runtime;
@@ -89,11 +91,12 @@ function init(_settings, _runtime) {
8991

9092
if (projectsEnabled) {
9193
return sshTools.init(settings,runtime).then(function() {
92-
gitTools.init(_settings, _runtime).then(function(gitVersion) {
93-
if (!gitVersion) {
94+
gitTools.init(_settings, _runtime).then(function(gitConfig) {
95+
if (!gitConfig) {
9496
projectLogMessages.push(log._("storage.localfilesystem.projects.git-not-found"))
9597
projectsEnabled = false;
9698
} else {
99+
globalGitUser = gitConfig.user;
97100
Projects.init(settings,runtime);
98101
sshTools.init(settings,runtime);
99102
projectsDir = fspath.join(settings.userDir,"projects");
@@ -553,7 +556,7 @@ module.exports = {
553556
updateRemote: updateRemote,
554557
getFlowFilename: getFlowFilename,
555558
getCredentialsFilename: getCredentialsFilename,
556-
559+
getGlobalGitUser: function() { return globalGitUser },
557560
getFlows: getFlows,
558561
saveFlows: saveFlows,
559562
getCredentials: getCredentials,

0 commit comments

Comments
 (0)