fix(repos): 修复在全新的工作目录中,检出指定commit时,找不到commit.

This commit is contained in:
Ivan
2021-02-25 15:36:13 +08:00
parent 90d851d85c
commit 5b2a017858
5 changed files with 527 additions and 10 deletions

View File

@ -0,0 +1,2 @@
import { ApplicationException } from '../../commons/exceptions/application.exception';
export class GetWorkspaceLockFailedException extends ApplicationException {}

View File

@ -2,7 +2,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Project } from '../projects/project.entity';
import { ReposService } from './repos.service';
import { ConfigService, ConfigModule } from '@nestjs/config';
import { ConfigModule } from '@nestjs/config';
import { readFile, rm } from 'fs/promises';
import { join } from 'path';
import configuration from '../commons/config/configuration';
@ -50,9 +50,7 @@ describe('ReposService', () => {
await rm(service.getWorkspaceRoot(getTest1Project()), {
recursive: true,
}).catch((err) => {
console.log('!!!!', err);
});
}).catch(() => undefined);
});
it('should be defined', () => {
@ -119,7 +117,7 @@ describe('ReposService', () => {
);
const text = await readFile(filePath, { encoding: 'utf-8' });
expect(text).toMatch(/Commit 1/gi);
});
}, 20_000);
it('should be checkout right commit', async () => {
await service.checkoutCommit(getTest1Project(), '7f7123fe5b');
const filePath = join(
@ -128,6 +126,6 @@ describe('ReposService', () => {
);
const text = await readFile(filePath, { encoding: 'utf-8' });
expect(text).toMatch(/(?!Commit 1)/gi);
});
}, 20_000);
});
});

View File

@ -26,8 +26,14 @@ export class ReposService {
);
}
async lockWorkspace(workspaceRoot: string) {
// TODO: 获取锁,失败抛错。
}
async getGit(project: Project) {
const workspaceRoot = this.getWorkspaceRoot(project);
await this.lockWorkspace(workspaceRoot);
const firstInit = await access(workspaceRoot, F_OK)
.then(() => false)
.catch(async () => {
@ -83,12 +89,13 @@ export class ReposService {
async checkoutCommit(project: Project, commitNumber: string) {
const git = await this.getGit(project);
try {
await git.checkout([commitNumber]);
await git.fetch(DEFAULT_REMOTE_NAME);
} catch (err) {
if (err.message.includes("couldn't find remote ref nonexistent")) {
throw new NotFoundException(err.message);
}
throw err;
}
await git.checkout([commitNumber]);
}
}