github.com/fredbi/git-chglog@v0.0.0-20190706071416-d35c598eac81/cmd/git-chglog/utils.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"regexp"
     7  	"strings"
     8  )
     9  
    10  var reSSH = regexp.MustCompile("^\\w+@([\\w\\.\\-]+):([\\w\\.\\-]+)\\/([\\w\\.\\-]+)$")
    11  
    12  func remoteOriginURLToHTTP(rawurl string) string {
    13  	if rawurl == "" {
    14  		return ""
    15  	}
    16  
    17  	rawurl = strings.TrimSuffix(rawurl, ".git")
    18  
    19  	// for normal url format
    20  	originURL, err := url.Parse(rawurl)
    21  
    22  	if err == nil {
    23  		scheme := originURL.Scheme
    24  		if scheme != "http" {
    25  			scheme = "https"
    26  		}
    27  		return fmt.Sprintf(
    28  			"%s://%s%s",
    29  			scheme,
    30  			originURL.Host,
    31  			originURL.Path,
    32  		)
    33  	}
    34  
    35  	// for `user@server:repo.git`
    36  	res := reSSH.FindAllStringSubmatch(rawurl, -1)
    37  	if len(res) > 0 {
    38  		return fmt.Sprintf(
    39  			"https://%s/%s/%s",
    40  			res[0][1],
    41  			res[0][2],
    42  			res[0][3],
    43  		)
    44  	}
    45  
    46  	return ""
    47  }