BREAKING CHANGE(dependencies): switch to isomorphic git
This commit is contained in:
parent
b83752a98a
commit
2771413723
23375
package-lock.json
generated
23375
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
20
package.json
20
package.json
@ -24,19 +24,19 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartgit",
|
||||
"dependencies": {
|
||||
"@pushrocks/smartfile": "^8.0.0",
|
||||
"@pushrocks/smartenv": "^4.0.16",
|
||||
"@pushrocks/smartfile": "^8.0.10",
|
||||
"@pushrocks/smartpath": "^4.0.3",
|
||||
"@pushrocks/smartpromise": "^3.0.6",
|
||||
"@pushrocks/smartshell": "^2.0.26",
|
||||
"@pushrocks/smartstring": "^3.0.18",
|
||||
"@types/minimatch": "^3.0.3",
|
||||
"@types/nodegit": "^0.26.7",
|
||||
"nodegit": "^0.27.0"
|
||||
"@pushrocks/smartpromise": "^3.1.6",
|
||||
"@pushrocks/smartshell": "^2.0.28",
|
||||
"@pushrocks/smartstring": "^3.0.24",
|
||||
"@types/minimatch": "^3.0.5",
|
||||
"isomorphic-git": "^1.10.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tstest": "^1.0.44",
|
||||
"@pushrocks/tapbundle": "^3.2.9",
|
||||
"@gitzone/tsbuild": "^2.1.28",
|
||||
"@gitzone/tstest": "^1.0.59",
|
||||
"@pushrocks/tapbundle": "^3.2.14",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
|
14
test/test.ts
14
test/test.ts
@ -3,10 +3,22 @@ import * as smartgit from '../ts/index';
|
||||
|
||||
import * as path from 'path';
|
||||
|
||||
let testSmartgitInstance: smartgit.Smartgit;
|
||||
const testRepoDir = path.join(__dirname, '../.nogit/testrepo');
|
||||
const testRepoDirSmartfile = path.join(__dirname, '../.nogit/pushrocks_smartfile')
|
||||
|
||||
tap.test('should create a valid smartgit instance', async () => {
|
||||
testSmartgitInstance = new smartgit.Smartgit();
|
||||
await testSmartgitInstance.init();
|
||||
expect(testSmartgitInstance).to.be.instanceOf(smartgit.Smartgit);
|
||||
})
|
||||
|
||||
tap.test('should create a new repo at .nogit', async () => {
|
||||
const gitRepo = await smartgit.GitRepo.fromCreatingRepoInDir(testRepoDir);
|
||||
const gitRepo = await testSmartgitInstance.createRepoByOpen(testRepoDir);
|
||||
});
|
||||
|
||||
tap.test('should clone a repo', async () => {
|
||||
const gitRepo = await testSmartgitInstance.createRepoByClone('https://gitlab.com/pushrocks/smartfile.git', testRepoDirSmartfile);
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
@ -1,3 +1,4 @@
|
||||
import plugins = require('./smartgit.plugins');
|
||||
|
||||
export * from './smartgit.classes.gitrepo';
|
||||
export * from './smartgit.classes.smartgit';
|
||||
|
@ -1,5 +1,7 @@
|
||||
import * as plugins from './smartgit.plugins';
|
||||
|
||||
import { Smartgit } from './smartgit.classes.smartgit';
|
||||
|
||||
/**
|
||||
* class GitRepo allows access to git directories from node
|
||||
*/
|
||||
@ -8,39 +10,61 @@ export class GitRepo {
|
||||
/**
|
||||
* creates a new GitRepo Instance after cloning a project
|
||||
*/
|
||||
public static async fromCloningIntoDir(fromArg: string, toArg: string): Promise<GitRepo> {
|
||||
public static async fromCloningIntoDir(
|
||||
smartgitRefArg: Smartgit,
|
||||
fromArg: string,
|
||||
toArg: string
|
||||
): Promise<GitRepo> {
|
||||
const dirArg = plugins.path.resolve(toArg);
|
||||
const ngRespository = await plugins.nodegit.Clone.clone(fromArg, toArg, {
|
||||
bare: 0,
|
||||
checkoutBranch: 'master',
|
||||
await plugins.isomorphicGit.clone({
|
||||
dir: toArg,
|
||||
fs: smartgitRefArg.envDeps.fs,
|
||||
http: smartgitRefArg.envDeps.http,
|
||||
url: fromArg,
|
||||
});
|
||||
return new GitRepo(ngRespository);
|
||||
return new GitRepo(smartgitRefArg, toArg);
|
||||
}
|
||||
|
||||
public static async fromCreatingRepoInDir(dirArg: string): Promise<GitRepo> {
|
||||
public static async fromCreatingRepoInDir(
|
||||
smartgitRefArg: Smartgit,
|
||||
dirArg: string
|
||||
): Promise<GitRepo> {
|
||||
dirArg = plugins.path.resolve(dirArg);
|
||||
const ngRepository = await plugins.nodegit.Repository.init(dirArg, 0);
|
||||
return new GitRepo(ngRepository);
|
||||
await plugins.isomorphicGit.init({
|
||||
dir: dirArg,
|
||||
fs: smartgitRefArg.envDeps.fs,
|
||||
});
|
||||
return new GitRepo(smartgitRefArg, dirArg);
|
||||
}
|
||||
|
||||
public static async fromOpeningRepoDir(dirArg: string) {
|
||||
public static async fromOpeningRepoDir(smartgitRefArg: Smartgit, dirArg: string) {
|
||||
dirArg = plugins.path.resolve(dirArg);
|
||||
const ngRepository = await plugins.nodegit.Repository.open(dirArg);
|
||||
return new GitRepo(ngRepository);
|
||||
return new GitRepo(smartgitRefArg, dirArg);
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
public nodegitRepo: plugins.nodegit.Repository;
|
||||
public smartgitRef: Smartgit;
|
||||
public repoDir: string;
|
||||
|
||||
constructor(nodegitRepoArg: plugins.nodegit.Repository) {
|
||||
this.nodegitRepo = nodegitRepoArg;
|
||||
constructor(smartgitRefArg: Smartgit, repoDirArg: string) {
|
||||
this.smartgitRef = smartgitRefArg;
|
||||
this.repoDir = repoDirArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* lists remotes
|
||||
*/
|
||||
public async listRemotes(): Promise<plugins.nodegit.Remote[]> {
|
||||
return this.nodegitRepo.getRemotes();
|
||||
public async listRemotes(): Promise<
|
||||
{
|
||||
remote: string;
|
||||
url: string;
|
||||
}[]
|
||||
> {
|
||||
const remotes = await plugins.isomorphicGit.listRemotes({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
dir: this.repoDir,
|
||||
});
|
||||
return remotes;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -49,31 +73,42 @@ export class GitRepo {
|
||||
* @param remoteUrlArg
|
||||
*/
|
||||
public async ensureRemote(remoteNameArg: string, remoteUrlArg: string): Promise<void> {
|
||||
const existingUrl = await this.getUrlForRemote(remoteNameArg);
|
||||
if (existingUrl === remoteUrlArg) {
|
||||
return;
|
||||
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;
|
||||
};
|
||||
}
|
||||
if (existingUrl) {
|
||||
await plugins.nodegit.Remote.delete(this.nodegitRepo, remoteNameArg);
|
||||
}
|
||||
await plugins.nodegit.Remote.create(this.nodegitRepo, remoteNameArg, remoteUrlArg);
|
||||
await plugins.isomorphicGit.addRemote({
|
||||
remote: remoteNameArg,
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
url: remoteUrlArg
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* gets the url for a specific remote
|
||||
*/
|
||||
public async getUrlForRemote(remoteName: string): Promise<string> {
|
||||
const ngRemote = await this.nodegitRepo.getRemote(remoteName);
|
||||
if (ngRemote) {
|
||||
return ngRemote.url();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
const remotes = await this.listRemotes();
|
||||
const existingRemote = remotes.find(remoteArg => remoteArg.remote === remoteName);
|
||||
return existingRemote?.url;
|
||||
}
|
||||
|
||||
public async pushBranchToRemote(branchName: string, remoteName: string) {
|
||||
await this.nodegitRepo.checkoutBranch(branchName);
|
||||
const ngRemote = await this.nodegitRepo.getRemote(remoteName);
|
||||
ngRemote.push([branchName]);
|
||||
await plugins.isomorphicGit.push({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
http: this.smartgitRef.envDeps.http,
|
||||
ref: branchName,
|
||||
remote: remoteName
|
||||
})
|
||||
}
|
||||
}
|
||||
|
36
ts/smartgit.classes.smartgit.ts
Normal file
36
ts/smartgit.classes.smartgit.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import * as plugins from './smartgit.plugins';
|
||||
import { GitRepo } from './smartgit.classes.gitrepo';
|
||||
|
||||
export class Smartgit {
|
||||
public smartenvInstance = new plugins.smartenv.Smartenv();
|
||||
public envDeps: {
|
||||
fs: any;
|
||||
http: any;
|
||||
} = {
|
||||
fs: null,
|
||||
http: null
|
||||
}
|
||||
|
||||
constructor() {};
|
||||
|
||||
public async init() {
|
||||
if (this.smartenvInstance.isNode) {
|
||||
this.envDeps.fs = this.smartenvInstance.getSafeNodeModule('fs');
|
||||
this.envDeps.http = this.smartenvInstance.getSafeNodeModule('isomorphic-git/http/node');
|
||||
}
|
||||
};
|
||||
|
||||
public async createRepoByClone(fromUrlArg: string, toDirArg: string) {
|
||||
const repo = await GitRepo.fromCloningIntoDir(this, fromUrlArg, toDirArg)
|
||||
};
|
||||
|
||||
public async createRepoByInit(dirArg: string) {
|
||||
const repo = await GitRepo.fromCreatingRepoInDir(this, dirArg);
|
||||
return repo;
|
||||
};
|
||||
|
||||
public async createRepoByOpen(dirArg: string) {
|
||||
const repo = await GitRepo.fromOpeningRepoDir(this, dirArg);
|
||||
return repo;
|
||||
}
|
||||
}
|
@ -3,14 +3,15 @@ import * as path from 'path';
|
||||
|
||||
export { path };
|
||||
|
||||
import * as smartenv from '@pushrocks/smartenv';
|
||||
import * as smartfile from '@pushrocks/smartfile';
|
||||
import * as smartpath from '@pushrocks/smartpath';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartstring from '@pushrocks/smartstring';
|
||||
|
||||
export { smartfile, smartpath, smartpromise, smartstring };
|
||||
export { smartenv, smartfile, smartpath, smartpromise, smartstring };
|
||||
|
||||
// third party
|
||||
import nodegit from 'nodegit';
|
||||
import isomorphicGit from 'isomorphic-git';
|
||||
|
||||
export { nodegit };
|
||||
export { isomorphicGit };
|
||||
|
Loading…
Reference in New Issue
Block a user