smartgit/ts/smartgit.classes.gitrepo.ts

60 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-06-18 13:17:50 +00:00
import * as plugins from './smartgit.plugins';
2016-11-21 23:07:36 +00:00
/**
* class GitRepo allows access to git directories from node
*/
export class GitRepo {
2019-06-18 13:17:50 +00:00
// STATIC
/**
* creates a new GitRepo Instance after cloning a project
*/
public static async createRepoFromClone(fromArg: string, toArg: string): Promise<GitRepo> {
2019-06-18 13:41:03 +00:00
const dirArg = plugins.path.resolve(toArg);
const ngRespository = await plugins.nodegit.Clone.clone(fromArg, toArg, {
bare: 0,
checkoutBranch: 'master'
});
return new GitRepo(ngRespository);
}
2019-06-19 12:00:44 +00:00
public static async createNewRepoInDir(dirArg: string): Promise<GitRepo> {
2019-06-18 13:41:03 +00:00
dirArg = plugins.path.resolve(dirArg);
const ngRepository = await plugins.nodegit.Repository.init(dirArg, 0);
return new GitRepo(ngRepository);
}
public static async openRepoAt(dirArg: string) {
dirArg = plugins.path.resolve(dirArg);
const ngRepository = await plugins.nodegit.Repository.open(dirArg);
return new GitRepo(ngRepository);
}
// INSTANCE
public nodegitRepo: plugins.nodegit.Repository;
constructor(nodegitRepoArg: plugins.nodegit.Repository) {
this.nodegitRepo = nodegitRepoArg;
2019-06-18 13:17:50 +00:00
}
/**
2019-06-18 13:41:03 +00:00
* lists remotes
2019-06-18 13:17:50 +00:00
*/
2019-06-18 13:41:03 +00:00
public async listRemotes(): Promise<string[]> {
return this.nodegitRepo.getRemotes();
2019-06-18 13:17:50 +00:00
}
2019-06-18 13:41:03 +00:00
/**
* gets the url for a specific remote
*/
public async getUrlForRemote(remoteName: string) {
const ngRemote = await this.nodegitRepo.getRemote(remoteName);
return ngRemote.url;
}
public async pushBranchToRemote(branchName: string, remoteName: string) {
await this.nodegitRepo.checkoutBranch(branchName);
const ngRemote = await this.nodegitRepo.getRemote(remoteName);
ngRemote.push([branchName]);
}
2016-11-21 23:07:36 +00:00
}