Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const node = {
- execSync: require('child_process').execSync,
- };
- class GitCommand {
- constructor ({cwd, sync = true}) {
- this.cache = {
- cwd,
- }
- }
- get cwd () {
- return this.cache.cwd;
- }
- get branchName () {
- return this.execResult('git rev-parse --abbrev-ref HEAD');
- }
- execResult(cmd, callback) {
- return node.execSync(cmd, {cwd: this.cwd}).toString();
- }
- exec (cmd, callback) {
- let result = node.execSync(cmd, {cwd: this.cwd});
- callback && callback(result.toString());
- return this;
- // return new Promise((resolve, reject) => {
- // node.exec(cmd, {cwd: this.cwd}, (error, stdout, stderr) => {
- // if (error) {
- // reject(error.toString());
- // } else {
- // resolve(stdout.toString(), stderr.toString());
- // }
- // });
- // });
- }
- checkout (name, callback) {
- return this.exec(`git checkout ${name}`, callback);
- }
- hash (name, callback) {
- return this.exec(`git rev-parse ${name}`, callback);
- }
- merge (name, callback) {
- return this.exec(`git merge ${name}`, callback);
- }
- push (callback = null) {
- let name = `origin ${this.branchName}`;
- return this.exec(`git push ${name}`, callback);
- }
- listSHA () {
- return this.execResult(`git log --pretty=oneline`);
- }
- diff (a, b) {
- return this.execResult(`git diff --name-status ${a} ${b}`);
- }
- exportDiff({baseSHA, newSHA, path, exceStatus = null}) {
- this.checkout(newSHA, () => {
- let hasExce = Array.isArray(exceStatus);
- let files = this.diff(baseSHA, newSHA).split('\n')
- .filter(s => s)
- .map(s => ({
- status: s.substr(0, 1),
- path: s.substr(1).trim(),
- }))
- .filter(o => (! hasExce) || (exceStatus.indexOf(o.status) >= 0))
- .map(o => `"${o.path}"`)
- .join(' ');
- let result = this.execResult(`git archive --format zip -o "D:/test/${newSHA}.zip" ${newSHA} ${files}`);
- this.execResult(`compact /u D:/test/${newSHA}.zip`);
- console.info(result);
- });
- }
- test (sha) {
- this.execResult(`git archive --format zip -o "D:/test/file.zip" ${sha} "pub/6411.php"`);
- }
- }
- module.exports = GitCommand;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement