2022-07-31 13:14:18 +00:00
|
|
|
import * as plugins from './smartgit.plugins.js';
|
2016-11-21 23:07:36 +00:00
|
|
|
|
2022-07-31 13:14:18 +00:00
|
|
|
import { Smartgit } from './smartgit.classes.smartgit.js';
|
2021-10-21 17:09:14 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2021-10-21 17:09:14 +00:00
|
|
|
public static async fromCloningIntoDir(
|
|
|
|
smartgitRefArg: Smartgit,
|
|
|
|
fromArg: string,
|
|
|
|
toArg: string
|
|
|
|
): Promise<GitRepo> {
|
2019-06-18 13:41:03 +00:00
|
|
|
const dirArg = plugins.path.resolve(toArg);
|
2021-10-21 17:09:14 +00:00
|
|
|
await plugins.isomorphicGit.clone({
|
|
|
|
dir: toArg,
|
|
|
|
fs: smartgitRefArg.envDeps.fs,
|
|
|
|
http: smartgitRefArg.envDeps.http,
|
|
|
|
url: fromArg,
|
2019-06-18 13:41:03 +00:00
|
|
|
});
|
2021-10-21 17:09:14 +00:00
|
|
|
return new GitRepo(smartgitRefArg, toArg);
|
2019-06-18 13:41:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 17:09:14 +00:00
|
|
|
public static async fromCreatingRepoInDir(
|
|
|
|
smartgitRefArg: Smartgit,
|
|
|
|
dirArg: string
|
|
|
|
): Promise<GitRepo> {
|
2019-06-18 13:41:03 +00:00
|
|
|
dirArg = plugins.path.resolve(dirArg);
|
2021-10-21 17:09:14 +00:00
|
|
|
await plugins.isomorphicGit.init({
|
|
|
|
dir: dirArg,
|
|
|
|
fs: smartgitRefArg.envDeps.fs,
|
|
|
|
});
|
|
|
|
return new GitRepo(smartgitRefArg, dirArg);
|
2019-06-18 13:41:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 17:09:14 +00:00
|
|
|
public static async fromOpeningRepoDir(smartgitRefArg: Smartgit, dirArg: string) {
|
2019-06-18 13:41:03 +00:00
|
|
|
dirArg = plugins.path.resolve(dirArg);
|
2021-10-21 17:09:14 +00:00
|
|
|
return new GitRepo(smartgitRefArg, dirArg);
|
2019-06-18 13:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// INSTANCE
|
2021-10-21 17:09:14 +00:00
|
|
|
public smartgitRef: Smartgit;
|
|
|
|
public repoDir: string;
|
2019-06-18 13:41:03 +00:00
|
|
|
|
2021-10-21 17:09:14 +00:00
|
|
|
constructor(smartgitRefArg: Smartgit, repoDirArg: string) {
|
|
|
|
this.smartgitRef = smartgitRefArg;
|
|
|
|
this.repoDir = repoDirArg;
|
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
|
|
|
*/
|
2021-10-21 17:09:14 +00:00
|
|
|
public async listRemotes(): Promise<
|
|
|
|
{
|
|
|
|
remote: string;
|
|
|
|
url: string;
|
|
|
|
}[]
|
|
|
|
> {
|
|
|
|
const remotes = await plugins.isomorphicGit.listRemotes({
|
|
|
|
fs: this.smartgitRef.envDeps.fs,
|
|
|
|
dir: this.repoDir,
|
|
|
|
});
|
|
|
|
return remotes;
|
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
|
2020-08-15 13:51:24 +00:00
|
|
|
* @param remoteUrlArg
|
2019-08-27 14:02:04 +00:00
|
|
|
*/
|
2019-06-20 12:19:54 +00:00
|
|
|
public async ensureRemote(remoteNameArg: string, remoteUrlArg: string): Promise<void> {
|
2021-10-21 17:09:14 +00:00
|
|
|
const remotes = await this.listRemotes();
|
|
|
|
const existingRemote =
|
|
|
|
remotes.find((itemArg) => itemArg.remote === remoteNameArg);
|
|
|
|
if (existingRemote) {
|
|
|
|
if (existingRemote.url !== remoteUrlArg) {
|
|
|
|
await plugins.isomorphicGit.deleteRemote({
|
|
|
|
remote: remoteNameArg,
|
|
|
|
fs: this.smartgitRef.envDeps.fs,
|
|
|
|
dir: this.repoDir
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2019-06-20 12:19:54 +00:00
|
|
|
}
|
2021-10-21 17:09:14 +00:00
|
|
|
await plugins.isomorphicGit.addRemote({
|
|
|
|
remote: remoteNameArg,
|
|
|
|
fs: this.smartgitRef.envDeps.fs,
|
|
|
|
url: remoteUrlArg
|
|
|
|
})
|
2019-06-20 12:19:54 +00:00
|
|
|
}
|
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> {
|
2021-10-21 17:09:14 +00:00
|
|
|
const remotes = await this.listRemotes();
|
|
|
|
const existingRemote = remotes.find(remoteArg => remoteArg.remote === remoteName);
|
|
|
|
return existingRemote?.url;
|
2019-06-18 13:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async pushBranchToRemote(branchName: string, remoteName: string) {
|
2021-10-21 17:09:14 +00:00
|
|
|
await plugins.isomorphicGit.push({
|
|
|
|
fs: this.smartgitRef.envDeps.fs,
|
|
|
|
http: this.smartgitRef.envDeps.http,
|
|
|
|
ref: branchName,
|
|
|
|
remote: remoteName
|
|
|
|
})
|
2019-06-18 13:41:03 +00:00
|
|
|
}
|
2016-11-21 23:07:36 +00:00
|
|
|
}
|