ref-helper.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {IGitCommandManager} from './git-command-manager'
  2. export interface ICheckoutInfo {
  3. ref: string
  4. startPoint: string
  5. }
  6. export async function getCheckoutInfo(
  7. git: IGitCommandManager,
  8. ref: string,
  9. commit: string
  10. ): Promise<ICheckoutInfo> {
  11. if (!git) {
  12. throw new Error('Arg git cannot be empty')
  13. }
  14. if (!ref && !commit) {
  15. throw new Error('Args ref and commit cannot both be empty')
  16. }
  17. const result = ({} as unknown) as ICheckoutInfo
  18. const upperRef = (ref || '').toUpperCase()
  19. // SHA only
  20. if (!ref) {
  21. result.ref = commit
  22. }
  23. // refs/heads/
  24. else if (upperRef.startsWith('REFS/HEADS/')) {
  25. const branch = ref.substring('refs/heads/'.length)
  26. result.ref = branch
  27. result.startPoint = `refs/remotes/origin/${branch}`
  28. }
  29. // refs/pull/
  30. else if (upperRef.startsWith('REFS/PULL/')) {
  31. const branch = ref.substring('refs/pull/'.length)
  32. result.ref = `refs/remotes/pull/${branch}`
  33. }
  34. // refs/tags/
  35. else if (upperRef.startsWith('REFS/')) {
  36. result.ref = ref
  37. }
  38. // Unqualified ref, check for a matching branch or tag
  39. else {
  40. if (await git.branchExists(true, `origin/${ref}`)) {
  41. result.ref = ref
  42. result.startPoint = `refs/remotes/origin/${ref}`
  43. } else if (await git.tagExists(`${ref}`)) {
  44. result.ref = `refs/tags/${ref}`
  45. } else {
  46. throw new Error(
  47. `A branch or tag with the name '${ref}' could not be found`
  48. )
  49. }
  50. }
  51. return result
  52. }
  53. export function getRefSpec(ref: string, commit: string): string[] {
  54. if (!ref && !commit) {
  55. throw new Error('Args ref and commit cannot both be empty')
  56. }
  57. const upperRef = (ref || '').toUpperCase()
  58. // SHA
  59. if (commit) {
  60. // refs/heads
  61. if (upperRef.startsWith('REFS/HEADS/')) {
  62. const branch = ref.substring('refs/heads/'.length)
  63. return [`+${commit}:refs/remotes/origin/${branch}`]
  64. }
  65. // refs/pull/
  66. else if (upperRef.startsWith('REFS/PULL/')) {
  67. const branch = ref.substring('refs/pull/'.length)
  68. return [`+${commit}:refs/remotes/pull/${branch}`]
  69. }
  70. // refs/tags/
  71. else if (upperRef.startsWith('REFS/TAGS/')) {
  72. return [`+${commit}:${ref}`]
  73. }
  74. // Otherwise no destination ref
  75. else {
  76. return [commit]
  77. }
  78. }
  79. // Unqualified ref, check for a matching branch or tag
  80. else if (!upperRef.startsWith('REFS/')) {
  81. return [
  82. `+refs/heads/${ref}*:refs/remotes/origin/${ref}*`,
  83. `+refs/tags/${ref}*:refs/tags/${ref}*`
  84. ]
  85. }
  86. // refs/heads/
  87. else if (upperRef.startsWith('REFS/HEADS/')) {
  88. const branch = ref.substring('refs/heads/'.length)
  89. return [`+${ref}:refs/remotes/origin/${branch}`]
  90. }
  91. // refs/pull/
  92. else if (upperRef.startsWith('REFS/PULL/')) {
  93. const branch = ref.substring('refs/pull/'.length)
  94. return [`+${ref}:refs/remotes/pull/${branch}`]
  95. }
  96. // refs/tags/
  97. else {
  98. return [`+${ref}:${ref}`]
  99. }
  100. }