smartgit/ts/smartgit.classes.gitrepo.ts

80 lines
2.3 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
*/
2019-06-20 12:10:42 +00:00
public static async fromCloningIntoDir(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-20 12:10:42 +00:00
public static async fromCreatingRepoInDir(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);
}
2019-06-20 12:10:42 +00:00
public static async fromOpeningRepoDir(dirArg: string) {
2019-06-18 13:41:03 +00:00
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
*/
2020-08-15 13:46:56 +00:00
public async listRemotes(): Promise<plugins.nodegit.Remote[]> {
2019-06-18 13:41:03 +00:00
return this.nodegitRepo.getRemotes();
2019-06-18 13:17:50 +00:00
}
2019-08-27 14:02:04 +00:00
/**
* ensures the existance of a remote within a repository
* @param remoteNameArg
* @param remoteUrlArg
*/
2019-06-20 12:19:54 +00:00
public async ensureRemote(remoteNameArg: string, remoteUrlArg: string): Promise<void> {
const existingUrl = await this.getUrlForRemote(remoteNameArg);
if (existingUrl === remoteUrlArg) {
return;
}
if (existingUrl) {
await plugins.nodegit.Remote.delete(this.nodegitRepo, remoteNameArg);
}
await plugins.nodegit.Remote.create(this.nodegitRepo, remoteNameArg, remoteUrlArg);
}
2019-06-18 13:17:50 +00:00
2019-06-18 13:41:03 +00:00
/**
* gets the url for a specific remote
*/
2019-06-20 12:19:54 +00:00
public async getUrlForRemote(remoteName: string): Promise<string> {
2019-06-18 13:41:03 +00:00
const ngRemote = await this.nodegitRepo.getRemote(remoteName);
2019-06-20 12:19:54 +00:00
if (ngRemote) {
return ngRemote.url();
} else {
return null;
}
2019-06-18 13:41:03 +00:00
}
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
}