|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"use strict"; |
|
|
|
|
|
var cwd = process.cwd(); |
|
var run = require(cwd + '/src/run'); |
|
var logger = require(cwd + '/src/logger').getLogger(); |
|
|
|
var repoPath; |
|
var currentRepo; |
|
var currentRepoPath; |
|
|
|
var gitPrettyConfig = [{ |
|
c: '%H', |
|
p: 'hash' |
|
}, { |
|
c: '%h', |
|
p: 'abbrevHash' |
|
}, { |
|
c: '%t', |
|
p: 'abbrevTreeHash' |
|
}, { |
|
c: '%P', |
|
p: 'parentHashes' |
|
}, { |
|
c: '%an', |
|
p: 'authorName' |
|
}, { |
|
c: '%ae', |
|
p: 'authorEmail' |
|
}, { |
|
c: '%ai', |
|
p: 'authorDate' |
|
}, { |
|
c: '%ar', |
|
p: 'authorDateRel' |
|
}, { |
|
c: '%cn', |
|
p: 'committerName' |
|
}, { |
|
c: '%ce', |
|
p: 'committerEmail' |
|
}, { |
|
c: '%cd', |
|
p: 'committerDate' |
|
}, { |
|
c: '%cr', |
|
p: 'committerDateRel' |
|
}, { |
|
c: '%s', |
|
p: 'subject' |
|
}]; |
|
|
|
function splitSpaces(data) { |
|
return data.split(" ").filter(function(s) { |
|
return s.length > 0; |
|
}); |
|
} |
|
|
|
module.exports = { |
|
setRepoPath: function(path) { |
|
repoPath = path; |
|
}, |
|
setRepo: function(repo) { |
|
currentRepo = repo; |
|
currentRepoPath = repoPath + '/' + currentRepo.name; |
|
}, |
|
clone: function() { |
|
if (currentRepoPath === undefined) { |
|
logger.error('clone repository not set'); |
|
return; |
|
} |
|
var args = ['clone', currentRepo.git.url, currentRepoPath]; |
|
var cmdout = run.execSync('git', args, null); |
|
return cmdout; |
|
}, |
|
fetch: function() { |
|
if (currentRepoPath === undefined) { |
|
logger.error('fetch repository not set'); |
|
return; |
|
} |
|
var cmdout = run.execSync('git', ['fetch'], currentRepoPath); |
|
if (cmdout.err) { |
|
logger.error('cannot do git fetch', currentRepoPath); |
|
return; |
|
} |
|
}, |
|
pull: function() { |
|
if (currentRepoPath === undefined) { |
|
logger.error('pull repository not set'); |
|
return; |
|
} |
|
var cmdout = run.execSync('git', ['pull'], currentRepoPath); |
|
if (cmdout.err) { |
|
logger.error('cannot do git pull', currentRepoPath); |
|
return; |
|
} |
|
}, |
|
repoResetHard: function(hash) { |
|
if (currentRepoPath === undefined) { |
|
logger.error('repoResetHard repository not set'); |
|
return; |
|
} |
|
var cmdout = run.execSync('git', ['reset', '--hard', hash], |
|
currentRepoPath); |
|
|
|
if (cmdout.err) { |
|
logger.error('cannot do git reset --hard ', currentRepoPath, hash); |
|
return; |
|
} |
|
|
|
cmdout = run.execSync('git', ['log', '-n1', '--pretty=format:%H'], |
|
currentRepoPath); |
|
if (cmdout.err) { |
|
logger.error('cannot do git log -n1', currentRepoPath); |
|
return; |
|
} |
|
var topHash = cmdout.stdout; |
|
if (topHash !== hash) { |
|
logger.error('Error after syncing ', hash, topHash); |
|
return; |
|
} |
|
return; |
|
}, |
|
repoResetHardPrevious: function(hash) { |
|
if (currentRepoPath === undefined) { |
|
logger.error('repoResetHardPrevious repository not set'); |
|
return; |
|
} |
|
var cmdout = run.execSync('git', ['reset', '--hard', 'HEAD^'], |
|
currentRepoPath); |
|
if (cmdout.err) { |
|
logger.error('cannot do git reset --hard ', currentRepoPath, hash); |
|
return; |
|
} |
|
cmdout = run.execSync('git', ['log', '-n1', '--pretty=format:%H'], |
|
currentRepoPath); |
|
if (cmdout.err) { |
|
logger.error('cannot do git log -n1', currentRepoPath); |
|
return; |
|
} |
|
return cmdout.stdout; |
|
}, |
|
getRemoteToT: function(branch) { |
|
if (currentRepoPath === undefined) { |
|
logger.error('getRemoteToT repository not set'); |
|
return; |
|
} |
|
var cmdout = run.execSync('git', ['log', branch, '-n1', |
|
'--pretty=format:%H' |
|
], currentRepoPath); |
|
|
|
if (cmdout.err) { |
|
logger.error('cannot do git log branch -n1 ', currentRepoPath, branch); |
|
return; |
|
} |
|
var tot = cmdout.stdout; |
|
return tot; |
|
}, |
|
getRepoToT: function() { |
|
if (currentRepoPath === undefined) { |
|
logger.error('getRepoToT repository not set'); |
|
return; |
|
} |
|
var cmdout = run.execSync('git', ['log', '-n1', '--pretty=format:%H'], |
|
currentRepoPath); |
|
|
|
if (cmdout.err) { |
|
logger.error('cannot do git log -n1 ', currentRepoPath); |
|
return; |
|
} |
|
var tot = cmdout.stdout; |
|
return tot; |
|
}, |
|
getBuildId: function(patch) { |
|
if (currentRepoPath === undefined) { |
|
logger.error('getBuildId repository not set'); |
|
return; |
|
} |
|
var cmdout = run.execSync('git', [ |
|
'log', '--oneline', '--pretty=format:%h', patch |
|
], currentRepoPath); |
|
var t = cmdout.stdout.split('\n'); |
|
var buildId = t.length; |
|
return buildId; |
|
}, |
|
getPatchInfo: function(patch) { |
|
if (currentRepoPath === undefined) { |
|
logger.error('getPatchInfo repository not set'); |
|
return; |
|
} |
|
|
|
var l = []; |
|
|
|
var targ = '--pretty=@tagStart@'; |
|
|
|
var ii; |
|
for (ii = 0; ii < gitPrettyConfig.length; ii++) |
|
targ += '@tagDelimiter@' + gitPrettyConfig[ii].c; |
|
targ += '@tagEnd@'; |
|
|
|
var cmdout = run.execSync('git', ['log', patch, '-n1', targ], |
|
currentRepoPath); |
|
|
|
if (cmdout.err) { |
|
console.log('ERR', cmdout.code); |
|
return; |
|
} |
|
var stdout = cmdout.stdout; |
|
logger.debug('commits stdout', stdout); |
|
|
|
|
|
var commits = stdout.split('@tagStart@'); |
|
|
|
|
|
if (commits.length === 1 && commits[0] === '') { |
|
commits.shift(); |
|
} |
|
commits.shift(); |
|
logger.debug('commits commits', commits); |
|
|
|
for (ii = 0; ii < commits.length; ii++) { |
|
var commit = commits[ii]; |
|
logger.debug('onecommit', ii, commits.length, commit); |
|
|
|
|
|
var parts = commit.split('@tagEnd@\n'); |
|
commit = parts[0].split('@tagDelimiter@'); |
|
|
|
|
|
commit.shift(); |
|
|
|
logger.debug('onecommit', commit); |
|
|
|
var entry = {}; |
|
for (var jj = 0; jj < gitPrettyConfig.length; jj++) { |
|
if (gitPrettyConfig[jj].p === 'parentHashes') { |
|
var tsplit = splitSpaces(commit[jj]); |
|
entry[gitPrettyConfig[jj].p] = tsplit; |
|
} else |
|
entry[gitPrettyConfig[jj].p] = commit[jj]; |
|
} |
|
|
|
|
|
logger.debug('entry', entry); |
|
|
|
l.push(entry); |
|
} |
|
|
|
return l; |
|
}, |
|
getLog: function(branch, startCommit, endCommit) { |
|
if (currentRepoPath === undefined) { |
|
logger.error('getLog repository not set'); |
|
return; |
|
} |
|
|
|
|
|
var cmdout = run.execSync('git', ['log', branch, '--oneline', |
|
'--pretty=format:%h' |
|
], currentRepoPath); |
|
|
|
var t = cmdout.stdout.split('\n'); |
|
var numPatches = t.length; |
|
logger.debug(cmdout, numPatches); |
|
|
|
var l = []; |
|
|
|
|
|
var args = []; |
|
args.push('log'); |
|
args.push(branch); |
|
|
|
if (endCommit !== undefined) { |
|
args.push(endCommit + '..' + startCommit); |
|
} |
|
|
|
var targ = '--pretty="@tagStart@'; |
|
var ii; |
|
for (ii = 0; ii < gitPrettyConfig.length; ii++) |
|
targ += '@tagDelimiter@' + gitPrettyConfig[ii].c; |
|
|
|
targ += '@tagEnd@"'; |
|
args.push(targ); |
|
|
|
cmdout = run.execSync('git', args, currentRepoPath); |
|
if (cmdout.err) { |
|
console.log('ERR', cmdout.code); |
|
return; |
|
} |
|
var stdout = cmdout.stdout; |
|
logger.debug('commits stdout', stdout); |
|
|
|
|
|
var commits = stdout.split('@tagStart@'); |
|
|
|
if (commits.length === 1 && commits[0] === '') { |
|
commits.shift(); |
|
} |
|
commits.shift(); |
|
logger.debug('commits commits', commits); |
|
|
|
for (ii = 0; ii < commits.length; ii++) { |
|
var commit = commits[ii]; |
|
logger.debug('onecommit', ii, commits.length, commit); |
|
|
|
|
|
var parts = commit.split('@tagEnd@\n'); |
|
commit = parts[0].split('@tagDelimiter@'); |
|
|
|
|
|
commit.shift(); |
|
|
|
logger.debug('onecommit', commit); |
|
|
|
var entry = {}; |
|
for (var jj = 0; jj < gitPrettyConfig.length; jj++) { |
|
if (gitPrettyConfig[jj].p === 'parentHashes') { |
|
var tsplit = splitSpaces(commit[jj]); |
|
entry[gitPrettyConfig[jj].p] = tsplit; |
|
} else |
|
entry[gitPrettyConfig[jj].p] = commit[jj]; |
|
} |
|
|
|
|
|
entry.buildId = numPatches - ii; |
|
logger.debug('entry', entry); |
|
|
|
l.push(entry); |
|
} |
|
return l; |
|
} |
|
}; |
|
|