feat(repos): 检出路径添加分支名称或Commit Number。

This commit is contained in:
Ivan
2021-02-25 18:15:12 +08:00
parent 5b2a017858
commit 1d8b99fe8e
3 changed files with 30 additions and 19 deletions

View File

@ -75,7 +75,7 @@ describe('ReposService', () => {
it('should be checkout', async () => {
await service.checkoutBranch(getTest1Project(), 'master');
const filePath = join(
service.getWorkspaceRoot(getTest1Project()),
service.getWorkspaceRoot(getTest1Project(), 'master'),
'README.md',
);
const text = await readFile(filePath, { encoding: 'utf-8' });
@ -86,7 +86,7 @@ describe('ReposService', () => {
await service.checkoutBranch(getTest1Project(), 'branch-a');
await service.checkoutBranch(getTest1Project(), 'branch-b');
const filePath = join(
service.getWorkspaceRoot(getTest1Project()),
service.getWorkspaceRoot(getTest1Project(), 'branch-b'),
'branch-b.md',
);
const text = await readFile(filePath, { encoding: 'utf-8' });
@ -100,7 +100,7 @@ describe('ReposService', () => {
it('checkout the specified version', async () => {
await service.checkoutBranch(getTest1Project(), 'master');
const filePath = join(
service.getWorkspaceRoot(getTest1Project()),
service.getWorkspaceRoot(getTest1Project(), 'master'),
'README.md',
);
const text = await readFile(filePath, { encoding: 'utf-8' });
@ -112,7 +112,7 @@ describe('ReposService', () => {
it('should be checkout', async () => {
await service.checkoutCommit(getTest1Project(), '498c782685');
const filePath = join(
service.getWorkspaceRoot(getTest1Project()),
service.getWorkspaceRoot(getTest1Project(), '498c782685'),
'README.md',
);
const text = await readFile(filePath, { encoding: 'utf-8' });
@ -121,7 +121,7 @@ describe('ReposService', () => {
it('should be checkout right commit', async () => {
await service.checkoutCommit(getTest1Project(), '7f7123fe5b');
const filePath = join(
service.getWorkspaceRoot(getTest1Project()),
service.getWorkspaceRoot(getTest1Project(), '7f7123fe5b'),
'README.md',
);
const text = await readFile(filePath, { encoding: 'utf-8' });

View File

@ -19,10 +19,11 @@ export class ReposService {
private readonly configService: ConfigService,
) {}
getWorkspaceRoot(project: Project): string {
getWorkspaceRoot(project: Project, subDir = ''): string {
return join(
this.configService.get<string>('workspaces.root'),
project.name,
encodeURIComponent(subDir),
);
}
@ -30,14 +31,14 @@ export class ReposService {
// TODO: 获取锁,失败抛错。
}
async getGit(project: Project) {
const workspaceRoot = this.getWorkspaceRoot(project);
async getGit(project: Project, subDir?: string) {
const workspaceRoot = this.getWorkspaceRoot(project, subDir);
await this.lockWorkspace(workspaceRoot);
const firstInit = await access(workspaceRoot, F_OK)
.then(() => false)
.catch(async () => {
await mkdir(workspaceRoot);
await mkdir(workspaceRoot, { recursive: true });
return true;
});
const git = gitP(workspaceRoot);
@ -69,7 +70,7 @@ export class ReposService {
}
async checkoutBranch(project: Project, branch: string) {
const git = await this.getGit(project);
const git = await this.getGit(project, branch);
try {
await git.fetch(DEFAULT_REMOTE_NAME, branch);
} catch (err) {
@ -87,7 +88,7 @@ export class ReposService {
}
async checkoutCommit(project: Project, commitNumber: string) {
const git = await this.getGit(project);
const git = await this.getGit(project, commitNumber);
try {
await git.fetch(DEFAULT_REMOTE_NAME);
} catch (err) {