git_protocol.go 953 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package gitutil
  2. import (
  3. "strings"
  4. "github.com/moby/buildkit/util/sshutil"
  5. )
  6. const (
  7. HTTPProtocol = iota + 1
  8. HTTPSProtocol
  9. SSHProtocol
  10. GitProtocol
  11. UnknownProtocol
  12. )
  13. // ParseProtocol parses a git URL and returns the remote url and protocol type
  14. func ParseProtocol(remote string) (string, int) {
  15. prefixes := map[string]int{
  16. "http://": HTTPProtocol,
  17. "https://": HTTPSProtocol,
  18. "git://": GitProtocol,
  19. "ssh://": SSHProtocol,
  20. }
  21. protocolType := UnknownProtocol
  22. for prefix, potentialType := range prefixes {
  23. if strings.HasPrefix(remote, prefix) {
  24. remote = strings.TrimPrefix(remote, prefix)
  25. protocolType = potentialType
  26. }
  27. }
  28. if protocolType == UnknownProtocol && sshutil.IsImplicitSSHTransport(remote) {
  29. protocolType = SSHProtocol
  30. }
  31. // remove name from ssh
  32. if protocolType == SSHProtocol {
  33. parts := strings.SplitN(remote, "@", 2)
  34. if len(parts) == 2 {
  35. remote = parts[1]
  36. }
  37. }
  38. return remote, protocolType
  39. }