smartgit/ts/smartgit.classes.gitrepo.ts

151 lines
3.9 KiB
TypeScript
Raw Normal View History

2016-11-21 23:07:36 +00:00
import * as q from 'q'
import * as plugins from './smartgit.plugins'
/**
* class GitRepo allows access to git directories from node
*/
export class GitRepo {
repoBase: string
constructor(repoBaseArg: string) {
this.repoBase = repoBaseArg
2016-11-22 21:22:24 +00:00
if (!this.check()) {
throw new Error('no valid git repo')
}
2016-11-21 23:07:36 +00:00
}
/**
* checks if the Repo is valid
*/
check(): boolean {
2016-11-22 21:22:24 +00:00
return plugins.smartfile.fs.isDirectory(plugins.path.join(this.repoBase, '.git'))
2016-11-21 23:07:36 +00:00
}
/**
* stage all files in working directory
2016-12-31 22:17:03 +00:00
* @executes SYNC
2016-11-21 23:07:36 +00:00
*/
2016-12-31 22:17:03 +00:00
addAll() {
2016-11-21 23:07:36 +00:00
let done = q.defer()
2016-12-31 22:17:03 +00:00
plugins.shelljs.exec(`(cd ${this.repoBase} && git add -A && git status)`)
done.resolve(this.repoBase)
2016-11-21 23:07:36 +00:00
return done.promise
};
2016-11-22 20:49:40 +00:00
/**
* add a remote to the GitRepo
*/
remoteAdd(remoteNameArg: string, remoteLinkArg: string) {
let done = q.defer()
if (!remoteNameArg) {
let err = new Error('smartgit.remote.add expects a valid remote name')
plugins.beautylog.error(err.message)
done.reject(err)
return done.promise
};
if (!remoteLinkArg) {
let err = new Error()
plugins.beautylog.error(err.message)
done.reject(err)
return done.promise
};
// if everything is all right proceed
plugins.shelljs.exec(`cd ${this.repoBase} && git remote add ${remoteNameArg} ${remoteLinkArg}`)
done.resolve()
return done.promise
}
/**
* list remotes for a Gip
*/
2016-11-22 21:22:24 +00:00
remoteList() {
2016-11-22 20:49:40 +00:00
let done = q.defer()
let remotes = {}
2016-11-22 21:22:24 +00:00
plugins.shelljs.exec(`cd ${this.repoBase} && git remote -v`)
2016-11-22 20:49:40 +00:00
done.resolve(remotes)
return done.promise
};
/**
* remove remote
*/
remoteRemove(dirPathArg: string) {
let done = q.defer()
return done.promise
}
2016-11-21 23:07:36 +00:00
/**
* commit all files that are currently staged
*/
commit(commitMessage: string) {
let done = q.defer()
plugins.shelljs.exec(`(cd ${this.repoBase} && git commit -m "${commitMessage}")`)
done.resolve()
return done.promise
}
/**
* pull latest changes from remote
*/
pull(sourceArg: string = '', branchArg: string = '') {
let done = q.defer()
// if everything is allright proceed
plugins.shelljs.exec(`(cd ${this.repoBase} && git pull ${sourceArg} ${branchArg})`)
done.resolve()
return done.promise
}
/**
* push new commits to remote
*/
push(remoteNameArg: string = '', remoteBranchArg: string = '') {
let done = q.defer()
// if everything seems allright proceed
plugins.shelljs.exec(`(cd ${this.repoBase} && git push ${remoteNameArg} ${remoteBranchArg})`)
done.resolve()
return done.promise
}
/**
* sync
*/
sync() {
this.pull().then(() => {
this.push()
})
}
2016-11-22 20:49:40 +00:00
/**
* get the current status
*/
status() {
let done = q.defer()
plugins.shelljs.exec(`(cd ${this.repoBase} && git status)`)
done.resolve()
return done.promise
}
}
2016-11-21 23:07:36 +00:00
2016-11-22 20:49:40 +00:00
/**
* creates a new GitRepo Instance after cloning a project
*/
2016-11-21 23:07:36 +00:00
export let createRepoFromClone = (fromArg: string, toArg: string) => {
let done = q.defer<GitRepo>()
plugins.smartfile.fs.ensureDir(toArg)
plugins.shelljs.exec(`git clone ${fromArg} ${toArg}`)
let newRepo = new GitRepo(toArg)
done.resolve(newRepo)
return done.promise
}
2016-11-22 20:49:40 +00:00
/**
* creates a new GitRepo instance after initializing a new Git Repository
*/
2016-11-21 23:07:36 +00:00
export let createRepoFromInit = (destinationDirArg: string) => {
let done = q.defer<GitRepo>()
plugins.smartfile.fs.ensureDir(destinationDirArg)
plugins.shelljs.exec(`cd destinationDirArg && git init`)
let newRepo = new GitRepo(destinationDirArg)
done.resolve(newRepo)
}