您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Git.service.ts 2.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import {ChildProcess, spawn} from 'child_process';
  2. import * as fs from 'fs/promises';
  3. export interface GitService {
  4. create(cwd: string): Promise<ChildProcess>
  5. delete(cwd: string): Promise<void>
  6. advertiseReceivePackRefs(cwd: string): Promise<ChildProcess>
  7. advertiseUploadPackRefs(cwd: string): Promise<ChildProcess>
  8. receivePack(cwd: string): Promise<ChildProcess>
  9. uploadPack(cwd: string): Promise<ChildProcess>
  10. }
  11. export class GitServiceImpl implements GitService {
  12. private static isWindows() {
  13. return /^win/.test(process.platform);
  14. }
  15. async create(cwd: string): Promise<ChildProcess> {
  16. await fs.mkdir(cwd, { recursive: true });
  17. return spawn('git', ['init', '--bare'], { cwd });
  18. }
  19. async delete(cwd: string): Promise<void> {
  20. await fs.rm(cwd, { recursive: true, force: true, });
  21. }
  22. async advertiseReceivePackRefs(cwd: string): Promise<ChildProcess> {
  23. const command = GitServiceImpl.isWindows() ? 'git' : 'git-receive-pack';
  24. const commonArgs = ['--stateless-rpc', '--advertise-refs', cwd];
  25. const args = GitServiceImpl.isWindows() ? ['receive-pack', ...commonArgs] : commonArgs;
  26. return spawn(command, args);
  27. }
  28. async advertiseUploadPackRefs(cwd: string): Promise<ChildProcess> {
  29. const command = GitServiceImpl.isWindows() ? 'git' : 'git-upload-pack';
  30. const commonArgs = ['--stateless-rpc', '--advertise-refs', cwd];
  31. const args = GitServiceImpl.isWindows() ? ['upload-pack', ...commonArgs] : commonArgs;
  32. return spawn(command, args);
  33. }
  34. async receivePack(cwd: string): Promise<ChildProcess> {
  35. const command = GitServiceImpl.isWindows() ? 'git' : 'git-receive-pack';
  36. const commonArgs = ['--stateless-rpc', cwd];
  37. const args = GitServiceImpl.isWindows() ? ['receive-pack', ...commonArgs] : commonArgs;
  38. return spawn(command, args);
  39. }
  40. async uploadPack(cwd: string): Promise<ChildProcess> {
  41. const command = GitServiceImpl.isWindows() ? 'git' : 'git-upload-pack';
  42. const commonArgs = ['--stateless-rpc', cwd];
  43. const args = GitServiceImpl.isWindows() ? ['upload-pack', ...commonArgs] : commonArgs;
  44. return spawn(command, args);
  45. }
  46. }