2017-09-20 11:30:07 +02:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var when = require('when');
|
|
|
|
var exec = require('child_process').exec;
|
|
|
|
var spawn = require('child_process').spawn;
|
2017-11-22 00:31:41 +01:00
|
|
|
var authResponseServer = require('./authServer').ResponseServer;
|
2017-12-15 16:07:47 +01:00
|
|
|
var sshResponseServer = require('./authServer').ResponseSSHServer;
|
2017-11-22 00:31:41 +01:00
|
|
|
var clone = require('clone');
|
|
|
|
var path = require("path");
|
2017-09-20 11:30:07 +02:00
|
|
|
|
2017-11-22 00:31:41 +01:00
|
|
|
var gitCommand = "git";
|
2017-12-13 10:44:48 +01:00
|
|
|
var gitVersion;
|
2017-11-22 00:31:41 +01:00
|
|
|
var log;
|
2017-09-20 11:30:07 +02:00
|
|
|
|
2017-11-22 00:31:41 +01:00
|
|
|
function runGitCommand(args,cwd,env) {
|
|
|
|
log.trace(gitCommand + JSON.stringify(args));
|
2017-09-20 11:30:07 +02:00
|
|
|
return when.promise(function(resolve,reject) {
|
2017-11-22 00:31:41 +01:00
|
|
|
args.unshift("credential.helper=")
|
|
|
|
args.unshift("-c");
|
|
|
|
var child = spawn(gitCommand, args, {cwd:cwd, detached:true, env:env});
|
2017-09-20 11:30:07 +02:00
|
|
|
var stdout = "";
|
|
|
|
var stderr = "";
|
|
|
|
child.stdout.on('data', function(data) {
|
|
|
|
stdout += data;
|
|
|
|
});
|
|
|
|
|
|
|
|
child.stderr.on('data', function(data) {
|
|
|
|
stderr += data;
|
|
|
|
});
|
|
|
|
|
|
|
|
child.on('close', function(code) {
|
|
|
|
if (code !== 0) {
|
|
|
|
var err = new Error(stderr);
|
2017-11-22 00:31:41 +01:00
|
|
|
err.stdout = stdout;
|
|
|
|
err.stderr = stderr;
|
2017-09-20 11:30:07 +02:00
|
|
|
if (/fatal: could not read Username/.test(stderr)) {
|
|
|
|
err.code = "git_auth_failed";
|
|
|
|
} else if(/HTTP Basic: Access denied/.test(stderr)) {
|
|
|
|
err.code = "git_auth_failed";
|
2017-12-15 16:07:47 +01:00
|
|
|
} else if(/Permission denied \(publickey\)/.test(stderr)) {
|
|
|
|
err.code = "git_auth_failed";
|
2017-11-22 00:31:41 +01:00
|
|
|
} else if(/Connection refused/.test(stderr)) {
|
|
|
|
err.code = "git_connection_failed";
|
2017-12-05 17:12:07 +01:00
|
|
|
} else if (/commit your changes or stash/.test(stderr)) {
|
|
|
|
err.code = "git_local_overwrite";
|
|
|
|
} else if (/CONFLICT/.test(err.stdout)) {
|
|
|
|
err.code = "git_pull_merge_conflict";
|
2017-12-08 17:31:42 +01:00
|
|
|
} else if (/not fully merged/.test(stderr)) {
|
|
|
|
err.code = "git_delete_branch_unmerged";
|
2017-12-20 15:37:34 +01:00
|
|
|
} else if (/remote .* already exists/.test(stderr)) {
|
|
|
|
err.code = "git_remote_already_exists";
|
2018-02-01 11:42:14 +01:00
|
|
|
} else if (/does not appear to be a git repository/.test(stderr)) {
|
|
|
|
err.code = "git_not_a_repository";
|
|
|
|
} else if (/Repository not found/i.test(stderr)) {
|
|
|
|
err.code = "git_repository_not_found";
|
2018-02-01 18:07:46 +01:00
|
|
|
} else if (/refusing to merge unrelated histories/.test(stderr)) {
|
|
|
|
err.code = "git_pull_unrelated_history"
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve(stdout);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-11-22 00:31:41 +01:00
|
|
|
function runGitCommandWithAuth(args,cwd,auth) {
|
|
|
|
return authResponseServer(auth).then(function(rs) {
|
|
|
|
var commandEnv = clone(process.env);
|
|
|
|
commandEnv.GIT_ASKPASS = path.join(__dirname,"node-red-ask-pass.sh");
|
|
|
|
commandEnv.NODE_RED_GIT_NODE_PATH = process.execPath;
|
|
|
|
commandEnv.NODE_RED_GIT_SOCK_PATH = rs.path;
|
|
|
|
commandEnv.NODE_RED_GIT_ASKPASS_PATH = path.join(__dirname,"authWriter.js");
|
|
|
|
return runGitCommand(args,cwd,commandEnv).finally(function() {
|
|
|
|
rs.close();
|
|
|
|
});
|
|
|
|
})
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
|
|
|
|
2017-12-15 16:07:47 +01:00
|
|
|
function runGitCommandWithSSHCommand(args,cwd,auth) {
|
|
|
|
return sshResponseServer(auth).then(function(rs) {
|
|
|
|
var commandEnv = clone(process.env);
|
|
|
|
commandEnv.SSH_ASKPASS = path.join(__dirname,"node-red-ask-pass.sh");
|
|
|
|
commandEnv.DISPLAY = "dummy:0";
|
|
|
|
commandEnv.NODE_RED_GIT_NODE_PATH = process.execPath;
|
|
|
|
commandEnv.NODE_RED_GIT_SOCK_PATH = rs.path;
|
|
|
|
commandEnv.NODE_RED_GIT_ASKPASS_PATH = path.join(__dirname,"authWriter.js");
|
|
|
|
commandEnv.GIT_SSH_COMMAND = "ssh -i " + auth.key_path + " -F /dev/null";
|
|
|
|
// console.log('commandEnv:', commandEnv);
|
|
|
|
return runGitCommand(args,cwd,commandEnv).finally(function() {
|
|
|
|
rs.close();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-07 01:18:20 +02:00
|
|
|
function cleanFilename(name) {
|
|
|
|
if (name[0] !== '"') {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
return name.substring(1,name.length-1);
|
|
|
|
}
|
|
|
|
function parseFilenames(name) {
|
|
|
|
var re = /([^ "]+|(".*?"))($| -> ([^ ]+|(".*"))$)/;
|
|
|
|
var m = re.exec(name);
|
|
|
|
var result = [];
|
|
|
|
if (m) {
|
|
|
|
result.push(cleanFilename(m[1]));
|
|
|
|
if (m[4]) {
|
|
|
|
result.push(cleanFilename(m[4]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2018-01-09 16:06:05 +01:00
|
|
|
// function getBranchInfo(localRepo) {
|
|
|
|
// return runGitCommand(["status","--porcelain","-b"],localRepo).then(function(output) {
|
|
|
|
// var lines = output.split("\n");
|
|
|
|
// var unknownDirs = [];
|
|
|
|
// var branchLineRE = /^## (No commits yet on )?(.+?)($|\.\.\.(.+?)($| \[(ahead (\d+))?.*?(behind (\d+))?\]))/m;
|
|
|
|
// console.log(output);
|
|
|
|
// console.log(lines);
|
|
|
|
// var m = branchLineRE.exec(output);
|
|
|
|
// console.log(m);
|
|
|
|
// var result = {}; //commits:{}};
|
|
|
|
// if (m) {
|
|
|
|
// if (m[1]) {
|
|
|
|
// result.empty = true;
|
|
|
|
// }
|
|
|
|
// result.local = m[2];
|
|
|
|
// if (m[4]) {
|
|
|
|
// result.remote = m[4];
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return result;
|
|
|
|
// });
|
|
|
|
// }
|
2017-11-22 00:31:41 +01:00
|
|
|
function getStatus(localRepo) {
|
2017-10-07 01:18:20 +02:00
|
|
|
// parseFilename('"test with space"');
|
|
|
|
// parseFilename('"test with space" -> knownFile.txt');
|
|
|
|
// parseFilename('"test with space" -> "un -> knownFile.txt"');
|
2017-11-22 00:31:41 +01:00
|
|
|
var result = {
|
|
|
|
files: {},
|
|
|
|
commits: {},
|
|
|
|
branches: {}
|
|
|
|
}
|
|
|
|
return runGitCommand(['rev-list', 'HEAD', '--count'],localRepo).then(function(count) {
|
|
|
|
result.commits.total = parseInt(count);
|
2018-01-09 16:06:05 +01:00
|
|
|
}).catch(function(err) {
|
|
|
|
if (/ambiguous argument/.test(err.message)) {
|
|
|
|
result.commits.total = 0;
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
2017-11-22 00:31:41 +01:00
|
|
|
}).then(function() {
|
|
|
|
return runGitCommand(["ls-files","--cached","--others","--exclude-standard"],localRepo).then(function(output) {
|
2017-10-07 01:18:20 +02:00
|
|
|
var lines = output.split("\n");
|
2017-11-22 00:31:41 +01:00
|
|
|
lines.forEach(function(l) {
|
|
|
|
if (l==="") {
|
2017-10-07 01:18:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-11-22 00:31:41 +01:00
|
|
|
var fullName = cleanFilename(l);
|
|
|
|
// parseFilename(l);
|
|
|
|
var parts = fullName.split("/");
|
|
|
|
var p = result.files;
|
|
|
|
var name;
|
|
|
|
for (var i = 0;i<parts.length-1;i++) {
|
|
|
|
var name = parts.slice(0,i+1).join("/")+"/";
|
|
|
|
if (!p.hasOwnProperty(name)) {
|
|
|
|
p[name] = {
|
|
|
|
type:"d"
|
|
|
|
}
|
|
|
|
}
|
2017-10-07 01:18:20 +02:00
|
|
|
}
|
2017-11-22 00:31:41 +01:00
|
|
|
result.files[fullName] = {
|
|
|
|
type: /\/$/.test(fullName)?"d":"f"
|
2017-10-07 01:18:20 +02:00
|
|
|
}
|
|
|
|
})
|
2017-11-22 00:31:41 +01:00
|
|
|
return runGitCommand(["status","--porcelain","-b"],localRepo).then(function(output) {
|
|
|
|
var lines = output.split("\n");
|
|
|
|
var unknownDirs = [];
|
2018-01-09 16:06:05 +01:00
|
|
|
var branchLineRE = /^## (?:No commits yet on )?(.+?)(?:$|\.\.\.(.+?)(?:$| \[(?:(?:ahead (\d+)(?:,\s*)?)?(?:behind (\d+))?|(gone))\]))/;
|
2017-11-22 00:31:41 +01:00
|
|
|
lines.forEach(function(line) {
|
|
|
|
if (line==="") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (line[0] === "#") {
|
|
|
|
var m = branchLineRE.exec(line);
|
|
|
|
if (m) {
|
|
|
|
result.branches.local = m[1];
|
2018-01-08 15:46:38 +01:00
|
|
|
if (m[2]) {
|
|
|
|
result.branches.remote = m[2];
|
2017-11-22 00:31:41 +01:00
|
|
|
result.commits.ahead = 0;
|
|
|
|
result.commits.behind = 0;
|
|
|
|
}
|
2018-01-08 15:46:38 +01:00
|
|
|
if (m[3] !== undefined) {
|
|
|
|
result.commits.ahead = parseInt(m[3]);
|
|
|
|
}
|
|
|
|
if (m[4] !== undefined) {
|
|
|
|
result.commits.behind = parseInt(m[4]);
|
2017-11-22 00:31:41 +01:00
|
|
|
}
|
2018-01-08 15:46:38 +01:00
|
|
|
if (m[5] !== undefined) {
|
|
|
|
result.commits.ahead = result.commits.total;
|
|
|
|
result.branches.remoteError = {
|
|
|
|
code: "git_remote_gone"
|
|
|
|
}
|
2017-11-22 00:31:41 +01:00
|
|
|
}
|
2017-10-07 01:18:20 +02:00
|
|
|
}
|
2017-11-22 00:31:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var status = line.substring(0,2);
|
|
|
|
var fileName;
|
|
|
|
var names;
|
|
|
|
if (status !== '??') {
|
|
|
|
names = parseFilenames(line.substring(3));
|
|
|
|
} else {
|
|
|
|
names = [cleanFilename(line.substring(3))];
|
|
|
|
}
|
|
|
|
fileName = names[0];
|
|
|
|
if (names.length > 1) {
|
|
|
|
fileName = names[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseFilename(fileName);
|
|
|
|
if (fileName.charCodeAt(0) === 34) {
|
|
|
|
fileName = fileName.substring(1,fileName.length-1);
|
|
|
|
}
|
|
|
|
if (result.files.hasOwnProperty(fileName)) {
|
|
|
|
result.files[fileName].status = status;
|
|
|
|
} else {
|
|
|
|
result.files[fileName] = {
|
|
|
|
type: "f",
|
|
|
|
status: status
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (names.length > 1) {
|
|
|
|
result.files[fileName].oldName = names[0];
|
|
|
|
}
|
|
|
|
if (status === "??" && fileName[fileName.length-1] === '/') {
|
|
|
|
unknownDirs.push(fileName);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
var allFilenames = Object.keys(result.files);
|
|
|
|
allFilenames.forEach(function(f) {
|
|
|
|
var entry = result.files[f];
|
|
|
|
if (!entry.hasOwnProperty('status')) {
|
|
|
|
unknownDirs.forEach(function(uf) {
|
|
|
|
if (f.startsWith(uf)) {
|
|
|
|
entry.status = "??"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// console.log(files);
|
|
|
|
return result;
|
2017-10-07 01:18:20 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-10 00:37:19 +02:00
|
|
|
function parseLog(log) {
|
|
|
|
var lines = log.split("\n");
|
2017-11-22 00:31:41 +01:00
|
|
|
var currentCommit = {};
|
2017-10-10 00:37:19 +02:00
|
|
|
var commits = [];
|
|
|
|
lines.forEach(function(l) {
|
2017-11-22 00:31:41 +01:00
|
|
|
if (l === "-----") {
|
|
|
|
commits.push(currentCommit);
|
2017-10-10 00:37:19 +02:00
|
|
|
currentCommit = {}
|
2017-11-22 00:31:41 +01:00
|
|
|
return;
|
2017-10-10 00:37:19 +02:00
|
|
|
}
|
|
|
|
var m = /^(.*): (.*)$/.exec(l);
|
|
|
|
if (m) {
|
2017-11-22 00:31:41 +01:00
|
|
|
if (m[1] === 'refs' && m[2]) {
|
|
|
|
currentCommit[m[1]] = m[2].split(",").map(function(v) { return v.trim() });
|
|
|
|
} else {
|
|
|
|
if (m[1] === 'parents') {
|
|
|
|
currentCommit[m[1]] = m[2].split(" ");
|
|
|
|
} else {
|
|
|
|
currentCommit[m[1]] = m[2];
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 00:37:19 +02:00
|
|
|
}
|
|
|
|
});
|
2017-11-22 00:31:41 +01:00
|
|
|
return commits;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRemotes(cwd) {
|
|
|
|
return runGitCommand(['remote','-v'],cwd).then(function(output) {
|
|
|
|
var result;
|
|
|
|
if (output.length > 0) {
|
|
|
|
result = {};
|
|
|
|
var remoteRE = /^(.+)\t(.+) \((.+)\)$/gm;
|
|
|
|
var m;
|
|
|
|
while ((m = remoteRE.exec(output)) !== null) {
|
|
|
|
result[m[1]] = result[m[1]]||{};
|
|
|
|
result[m[1]][m[3]] = m[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBranches(cwd, remote) {
|
2017-12-07 23:24:57 +01:00
|
|
|
var args = ['branch','-vv','--no-color'];
|
2017-11-22 00:31:41 +01:00
|
|
|
if (remote) {
|
|
|
|
args.push('-r');
|
2017-10-10 00:37:19 +02:00
|
|
|
}
|
2017-12-08 17:31:42 +01:00
|
|
|
var branchRE = /^([ \*] )(\S+) +(\S+)(?: \[(\S+?)(?:: (?:ahead (\d+)(?:, )?)?(?:behind (\d+))?)?\])? (.*)$/;
|
2017-11-22 00:31:41 +01:00
|
|
|
return runGitCommand(args,cwd).then(function(output) {
|
|
|
|
var branches = [];
|
|
|
|
var lines = output.split("\n");
|
2017-12-07 23:24:57 +01:00
|
|
|
branches = lines.map(function(l) {
|
|
|
|
var m = branchRE.exec(l);
|
|
|
|
var branch = null;
|
|
|
|
if (m) {
|
|
|
|
branch = {
|
|
|
|
name: m[2],
|
|
|
|
remote: m[4],
|
2017-12-08 17:31:42 +01:00
|
|
|
status: {
|
|
|
|
ahead: m[5]||0,
|
|
|
|
behind: m[6]||0,
|
|
|
|
},
|
2017-12-07 23:24:57 +01:00
|
|
|
commit: {
|
|
|
|
sha: m[3],
|
2017-12-08 17:31:42 +01:00
|
|
|
subject: m[7]
|
2017-12-07 23:24:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m[1] === '* ') {
|
|
|
|
branch.current = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return branch;
|
|
|
|
}).filter(function(v) { return !!v && v.commit.sha !== '->' });
|
|
|
|
|
2017-11-22 00:31:41 +01:00
|
|
|
return {branches:branches};
|
|
|
|
})
|
|
|
|
}
|
|
|
|
function getBranchStatus(cwd,remoteBranch) {
|
|
|
|
var commands = [
|
|
|
|
// #commits master ahead
|
|
|
|
runGitCommand(['rev-list', 'HEAD','^'+remoteBranch, '--count'],cwd),
|
|
|
|
// #commits master behind
|
|
|
|
runGitCommand(['rev-list', '^HEAD',remoteBranch, '--count'],cwd)
|
|
|
|
];
|
|
|
|
return when.all(commands).then(function(results) {
|
|
|
|
return {
|
|
|
|
commits: {
|
|
|
|
ahead: parseInt(results[0]),
|
|
|
|
behind: parseInt(results[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2017-10-10 00:37:19 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
function addRemote(cwd,name,options) {
|
|
|
|
var args = ["remote","add",name,options.url]
|
|
|
|
return runGitCommand(args,cwd);
|
|
|
|
}
|
|
|
|
function removeRemote(cwd,name) {
|
|
|
|
var args = ["remote","remove",name];
|
|
|
|
return runGitCommand(args,cwd);
|
|
|
|
}
|
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
module.exports = {
|
2017-11-22 00:31:41 +01:00
|
|
|
init: function(_settings,_runtime) {
|
|
|
|
log = _runtime.log
|
2017-12-13 10:44:48 +01:00
|
|
|
return new Promise(function(resolve,reject) {
|
2018-01-24 22:05:48 +01:00
|
|
|
Promise.all([
|
|
|
|
runGitCommand(["--version"]),
|
|
|
|
runGitCommand(["config","--global","user.name"]).catch(err=>""),
|
|
|
|
runGitCommand(["config","--global","user.email"]).catch(err=>"")
|
|
|
|
]).then(function(output) {
|
|
|
|
var m = / (\d\S+)/.exec(output[0]);
|
2017-12-13 10:44:48 +01:00
|
|
|
gitVersion = m[1];
|
2018-01-24 22:05:48 +01:00
|
|
|
var globalUserName = output[1].trim();
|
|
|
|
var globalUserEmail = output[2].trim();
|
|
|
|
var result = {
|
|
|
|
version: gitVersion
|
|
|
|
};
|
|
|
|
if (globalUserName && globalUserEmail) {
|
|
|
|
result.user = {
|
|
|
|
name: globalUserName,
|
|
|
|
email: globalUserEmail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resolve(result);
|
2017-12-13 10:44:48 +01:00
|
|
|
}).catch(function(err) {
|
2018-01-24 22:05:48 +01:00
|
|
|
console.log(err);
|
2017-12-13 10:44:48 +01:00
|
|
|
resolve(null);
|
|
|
|
});
|
|
|
|
});
|
2017-11-22 00:31:41 +01:00
|
|
|
},
|
2017-09-20 11:30:07 +02:00
|
|
|
initRepo: function(cwd) {
|
2017-11-22 00:31:41 +01:00
|
|
|
return runGitCommand(["init"],cwd);
|
|
|
|
},
|
|
|
|
setUpstream: function(cwd,remoteBranch) {
|
|
|
|
var args = ["branch","--set-upstream-to",remoteBranch];
|
|
|
|
return runGitCommand(args,cwd);
|
|
|
|
},
|
2017-12-04 12:42:44 +01:00
|
|
|
pull: function(cwd,remote,branch,auth) {
|
2017-11-22 00:31:41 +01:00
|
|
|
var args = ["pull"];
|
2017-12-04 12:42:44 +01:00
|
|
|
if (remote && branch) {
|
|
|
|
args.push(remote);
|
|
|
|
args.push(branch);
|
2017-11-22 00:31:41 +01:00
|
|
|
}
|
2017-11-23 21:52:15 +01:00
|
|
|
var promise;
|
|
|
|
if (auth) {
|
2017-12-15 16:07:47 +01:00
|
|
|
if ( auth.key_path ) {
|
|
|
|
promise = runGitCommandWithSSHCommand(args,cwd,auth);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
promise = runGitCommandWithAuth(args,cwd,auth);
|
|
|
|
}
|
2017-11-23 21:52:15 +01:00
|
|
|
} else {
|
|
|
|
promise = runGitCommand(args,cwd)
|
|
|
|
}
|
|
|
|
return promise.catch(function(err) {
|
2017-11-22 00:31:41 +01:00
|
|
|
if (/CONFLICT/.test(err.stdout)) {
|
|
|
|
var e = new Error("NLS: pull failed - merge conflict");
|
|
|
|
e.code = "git_pull_merge_conflict";
|
|
|
|
throw e;
|
|
|
|
} else if (/Please commit your changes or stash/.test(err.message)) {
|
|
|
|
var e = new Error("NLS: Pull failed - local changes would be overwritten");
|
|
|
|
e.code = "git_pull_overwrite";
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
});
|
2017-09-20 11:30:07 +02:00
|
|
|
},
|
2017-12-04 12:42:44 +01:00
|
|
|
push: function(cwd,remote,branch,setUpstream, auth) {
|
2017-11-22 00:31:41 +01:00
|
|
|
var args = ["push"];
|
2017-12-04 12:42:44 +01:00
|
|
|
if (branch) {
|
2017-11-22 00:31:41 +01:00
|
|
|
if (setUpstream) {
|
|
|
|
args.push("-u");
|
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
args.push(remote);
|
|
|
|
args.push("HEAD:"+branch);
|
2017-11-22 00:31:41 +01:00
|
|
|
} else {
|
2017-12-04 12:42:44 +01:00
|
|
|
args.push(remote);
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
2017-11-22 00:31:41 +01:00
|
|
|
args.push("--porcelain");
|
2017-11-23 01:27:13 +01:00
|
|
|
var promise;
|
|
|
|
if (auth) {
|
2017-12-15 16:07:47 +01:00
|
|
|
if ( auth.key_path ) {
|
|
|
|
promise = runGitCommandWithSSHCommand(args,cwd,auth);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
promise = runGitCommandWithAuth(args,cwd,auth);
|
|
|
|
}
|
2017-11-23 01:27:13 +01:00
|
|
|
} else {
|
|
|
|
promise = runGitCommand(args,cwd)
|
|
|
|
}
|
|
|
|
return promise.catch(function(err) {
|
2017-11-22 00:31:41 +01:00
|
|
|
if (err.code === 'git_error') {
|
|
|
|
if (/^!.*non-fast-forward/m.test(err.stdout)) {
|
|
|
|
err.code = 'git_push_failed';
|
|
|
|
}
|
|
|
|
throw err;
|
2018-01-11 12:19:04 +01:00
|
|
|
} else {
|
|
|
|
throw err;
|
2017-11-22 00:31:41 +01:00
|
|
|
}
|
|
|
|
});
|
2017-09-20 11:30:07 +02:00
|
|
|
},
|
2017-11-23 01:27:13 +01:00
|
|
|
clone: function(remote, auth, cwd) {
|
2017-11-22 00:31:41 +01:00
|
|
|
var args = ["clone",remote.url];
|
|
|
|
if (remote.name) {
|
|
|
|
args.push("-o");
|
|
|
|
args.push(remote.name);
|
|
|
|
}
|
|
|
|
if (remote.branch) {
|
|
|
|
args.push("-b");
|
|
|
|
args.push(remote.branch);
|
|
|
|
}
|
|
|
|
args.push(".");
|
2017-11-23 01:27:13 +01:00
|
|
|
if (auth) {
|
2017-12-15 16:07:47 +01:00
|
|
|
if ( auth.key_path ) {
|
|
|
|
return runGitCommandWithSSHCommand(args,cwd,auth);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return runGitCommandWithAuth(args,cwd,auth);
|
|
|
|
}
|
2017-11-22 00:31:41 +01:00
|
|
|
} else {
|
|
|
|
return runGitCommand(args,cwd);
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
2017-10-07 01:18:20 +02:00
|
|
|
},
|
2017-11-22 00:31:41 +01:00
|
|
|
getStatus: getStatus,
|
2017-10-25 16:26:24 +02:00
|
|
|
getFile: function(cwd, filePath, treeish) {
|
|
|
|
var args = ["show",treeish+":"+filePath];
|
2017-11-22 00:31:41 +01:00
|
|
|
return runGitCommand(args,cwd);
|
|
|
|
},
|
|
|
|
getFiles: function(cwd) {
|
|
|
|
return getStatus(cwd).then(function(status) {
|
|
|
|
return status.files;
|
|
|
|
})
|
2017-10-25 16:26:24 +02:00
|
|
|
},
|
2017-12-11 18:05:12 +01:00
|
|
|
revertFile: function(cwd, filePath) {
|
|
|
|
var args = ["checkout",filePath];
|
|
|
|
return runGitCommand(args,cwd);
|
|
|
|
},
|
2017-10-07 01:18:20 +02:00
|
|
|
stageFile: function(cwd,file) {
|
|
|
|
var args = ["add"];
|
|
|
|
if (Array.isArray(file)) {
|
|
|
|
args = args.concat(file);
|
|
|
|
} else {
|
|
|
|
args.push(file);
|
|
|
|
}
|
2017-11-22 00:31:41 +01:00
|
|
|
return runGitCommand(args,cwd);
|
2017-10-07 01:18:20 +02:00
|
|
|
},
|
|
|
|
unstageFile: function(cwd, file) {
|
|
|
|
var args = ["reset","--"];
|
|
|
|
if (file) {
|
|
|
|
args.push(file);
|
|
|
|
}
|
2017-11-22 00:31:41 +01:00
|
|
|
return runGitCommand(args,cwd);
|
2017-10-07 01:18:20 +02:00
|
|
|
},
|
2017-12-04 12:42:44 +01:00
|
|
|
commit: function(cwd, message, gitUser) {
|
2017-10-07 01:18:20 +02:00
|
|
|
var args = ["commit","-m",message];
|
2017-12-04 12:42:44 +01:00
|
|
|
var env;
|
|
|
|
if (gitUser && gitUser['name'] && gitUser['email']) {
|
|
|
|
args.unshift('user.name="'+gitUser['name']+'"');
|
|
|
|
args.unshift('-c');
|
|
|
|
args.unshift('user.email="'+gitUser['email']+'"');
|
|
|
|
args.unshift('-c');
|
|
|
|
}
|
|
|
|
return runGitCommand(args,cwd,env);
|
2017-10-09 01:11:07 +02:00
|
|
|
},
|
|
|
|
getFileDiff(cwd,file,type) {
|
|
|
|
var args = ["diff"];
|
|
|
|
if (type === "tree") {
|
|
|
|
// nothing else to do
|
|
|
|
} else if (type === "index") {
|
|
|
|
args.push("--cached");
|
|
|
|
}
|
|
|
|
args.push(file);
|
2017-11-22 00:31:41 +01:00
|
|
|
return runGitCommand(args,cwd);
|
|
|
|
},
|
2017-12-04 12:42:44 +01:00
|
|
|
fetch: function(cwd,remote,auth) {
|
|
|
|
var args = ["fetch",remote];
|
2017-11-23 01:27:13 +01:00
|
|
|
if (auth) {
|
2017-12-15 16:07:47 +01:00
|
|
|
if ( auth.key_path ) {
|
|
|
|
return runGitCommandWithSSHCommand(args,cwd,auth);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return runGitCommandWithAuth(args,cwd,auth);
|
|
|
|
}
|
2017-11-23 01:27:13 +01:00
|
|
|
} else {
|
|
|
|
return runGitCommand(args,cwd);
|
|
|
|
}
|
2017-10-10 00:37:19 +02:00
|
|
|
},
|
|
|
|
getCommits: function(cwd,options) {
|
2017-11-22 00:31:41 +01:00
|
|
|
var args = ["log", "--format=sha: %H%nparents: %p%nrefs: %D%nauthor: %an%ndate: %ct%nsubject: %s%n-----"];
|
|
|
|
var limit = parseInt(options.limit) || 20;
|
|
|
|
args.push("-n "+limit);
|
|
|
|
var before = options.before;
|
|
|
|
if (before) {
|
|
|
|
args.push(before);
|
|
|
|
}
|
|
|
|
var commands = [
|
|
|
|
runGitCommand(['rev-list', 'HEAD', '--count'],cwd),
|
|
|
|
runGitCommand(args,cwd).then(parseLog)
|
|
|
|
];
|
|
|
|
return when.all(commands).then(function(results) {
|
|
|
|
var result = results[0];
|
|
|
|
result.count = results[1].length;
|
|
|
|
result.before = before;
|
|
|
|
result.commits = results[1];
|
|
|
|
return {
|
|
|
|
count: results[1].length,
|
|
|
|
commits: results[1],
|
|
|
|
before: before,
|
|
|
|
total: parseInt(results[0])
|
|
|
|
};
|
|
|
|
})
|
2017-10-10 00:37:19 +02:00
|
|
|
},
|
|
|
|
getCommit: function(cwd,sha) {
|
|
|
|
var args = ["show",sha];
|
2017-11-22 00:31:41 +01:00
|
|
|
return runGitCommand(args,cwd);
|
|
|
|
},
|
|
|
|
abortMerge: function(cwd) {
|
|
|
|
return runGitCommand(['merge','--abort'],cwd);
|
|
|
|
},
|
|
|
|
getRemotes: getRemotes,
|
2017-12-04 12:42:44 +01:00
|
|
|
getRemoteBranch: function(cwd) {
|
2017-12-04 14:26:47 +01:00
|
|
|
return runGitCommand(['rev-parse','--abbrev-ref','--symbolic-full-name','@{u}'],cwd).catch(function(err) {
|
|
|
|
if (/no upstream configured for branch/.test(err.message)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
})
|
2017-12-04 12:42:44 +01:00
|
|
|
},
|
2017-11-22 00:31:41 +01:00
|
|
|
getBranches: getBranches,
|
2018-01-09 16:06:05 +01:00
|
|
|
// getBranchInfo: getBranchInfo,
|
2017-11-22 00:31:41 +01:00
|
|
|
checkoutBranch: function(cwd, branchName, isCreate) {
|
|
|
|
var args = ['checkout'];
|
|
|
|
if (isCreate) {
|
|
|
|
args.push('-b');
|
|
|
|
}
|
|
|
|
args.push(branchName);
|
|
|
|
return runGitCommand(args,cwd);
|
|
|
|
},
|
2017-12-08 17:31:42 +01:00
|
|
|
deleteBranch: function(cwd, branchName, isRemote, force) {
|
|
|
|
if (isRemote) {
|
|
|
|
throw new Error("Deleting remote branches not supported");
|
|
|
|
}
|
|
|
|
var args = ['branch'];
|
|
|
|
if (force) {
|
|
|
|
args.push('-D');
|
|
|
|
} else {
|
|
|
|
args.push('-d');
|
|
|
|
}
|
|
|
|
args.push(branchName);
|
|
|
|
return runGitCommand(args, cwd);
|
|
|
|
},
|
2017-12-04 12:42:44 +01:00
|
|
|
getBranchStatus: getBranchStatus,
|
|
|
|
addRemote: addRemote,
|
|
|
|
removeRemote: removeRemote
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|