import * as git from "simple-git/promise"; import * as path from "path"; export default class Git { git: any; constructor(gitPath?: string) { this.git = git(gitPath); } status() { return this.git.status(); } pull(remote: string, branch: string, options?: any) { return this.git.pull(remote, branch, options); } async push(branch?: string, remote: string = "origin") { if (!branch) { branch = await this.getCurrentBranch(); } return this.git.push(remote, branch); } add(p: string) { return this.git.add(p); } commit(msg: string) { return this.git.commit(msg); } getLatestCommit(): Promise { return this.git.log(["-1"]).then(log => log.latest.hash); } async getFilesByCommitRange(from: string, to: string): Promise { const commits = await this.git .log({ from, to }) .then(log => log.all.map(item => item.hash)); return this.getFilesByCommits(commits); } getFilesByCommits(commits: string[]): Promise { return this.git.show(["--name-only", "--pretty=", ...commits]).then((result: string) => result .split("\n") .filter(item => !!item) .map(file => path.resolve(process.cwd(), file)) ); } async getCurrentBranch() { const stats = await this.git.status(); return stats.current; } }