feat(repos): 新增 检出指定 commit 功能。
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { F_OK } from 'constants';
|
||||
import { access, mkdir } from 'fs/promises';
|
||||
@@ -9,8 +9,8 @@ import { Project } from '../projects/project.entity';
|
||||
import { ListBranchesArgs } from './dtos/list-branches.args';
|
||||
import { ListLogsArgs } from './dtos/list-logs.args';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { log } from 'console';
|
||||
|
||||
const DEFAULT_REMOTE_NAME = 'origin';
|
||||
@Injectable()
|
||||
export class ReposService {
|
||||
constructor(
|
||||
@@ -19,21 +19,25 @@ export class ReposService {
|
||||
private readonly configService: ConfigService,
|
||||
) {}
|
||||
|
||||
async getGit(project: Project) {
|
||||
const workspacePath = join(
|
||||
getWorkspaceRoot(project: Project): string {
|
||||
return join(
|
||||
this.configService.get<string>('workspaces.root'),
|
||||
project.name,
|
||||
);
|
||||
const firstInit = await access(workspacePath, F_OK)
|
||||
}
|
||||
|
||||
async getGit(project: Project) {
|
||||
const workspaceRoot = this.getWorkspaceRoot(project);
|
||||
const firstInit = await access(workspaceRoot, F_OK)
|
||||
.then(() => false)
|
||||
.catch(async () => {
|
||||
await mkdir(workspacePath);
|
||||
await mkdir(workspaceRoot);
|
||||
return true;
|
||||
});
|
||||
const git = gitP(workspacePath);
|
||||
const git = gitP(workspaceRoot);
|
||||
if (firstInit) {
|
||||
await git.init();
|
||||
await git.addRemote('origin', project.sshUrl);
|
||||
await git.addRemote(DEFAULT_REMOTE_NAME, project.sshUrl);
|
||||
}
|
||||
return git;
|
||||
}
|
||||
@@ -46,7 +50,7 @@ export class ReposService {
|
||||
await git.fetch();
|
||||
return await git.log({
|
||||
'--branches': dto.branch ?? '',
|
||||
'--remotes': 'origin',
|
||||
'--remotes': DEFAULT_REMOTE_NAME,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -57,4 +61,34 @@ export class ReposService {
|
||||
const git = await this.getGit(project);
|
||||
return git.branch();
|
||||
}
|
||||
|
||||
async checkoutBranch(project: Project, branch: string) {
|
||||
const git = await this.getGit(project);
|
||||
try {
|
||||
await git.fetch(DEFAULT_REMOTE_NAME, branch);
|
||||
} catch (err) {
|
||||
if (err.message.includes("couldn't find remote ref nonexistent")) {
|
||||
throw new NotFoundException(err.message);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
await git.checkout([
|
||||
'-B',
|
||||
branch,
|
||||
'--track',
|
||||
`${DEFAULT_REMOTE_NAME}/${branch}`,
|
||||
]);
|
||||
}
|
||||
|
||||
async checkoutCommit(project: Project, commitNumber: string) {
|
||||
const git = await this.getGit(project);
|
||||
try {
|
||||
await git.checkout([commitNumber]);
|
||||
} catch (err) {
|
||||
if (err.message.includes("couldn't find remote ref nonexistent")) {
|
||||
throw new NotFoundException(err.message);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user