You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.3 KiB

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