Skip to content

Commit

Permalink
use new mirror format
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangyt committed Jul 28, 2023
1 parent 9bc6b63 commit 8b366da
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions git_instrument.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"fmt"
"log"
"net/url"
"os"
"strings"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -106,19 +109,27 @@ func ResetGithubRemote() {
func GeneratedMirroredURL(gitURLText string, mirrorURLText string) string {
var err error

var mirrorURL *url.URL
if mirrorURL, err = url.Parse(mirrorURLText); err != nil {
panic(errors.Wrapf(err, "无法解析URL: %s", mirrorURLText))
}

var gitURL *url.URL
if gitURL, err = url.Parse(gitURLText); err != nil {
panic(errors.Wrapf(err, "无法解析URL: %s", gitURLText))
}

gitURL.Scheme = mirrorURL.Scheme
gitURL.Host = mirrorURL.Host
gitURL.User = mirrorURL.User
gitURLPath := gitURL.Path

slashBeforeProj := strings.LastIndex(gitURLPath, "/")
if slashBeforeProj <= 0 || slashBeforeProj == len(gitURLPath)-1 {
panic(fmt.Sprintf("无法解析URL: %s", gitURLText))
}
gitProject := gitURLPath[slashBeforeProj+1:]
gitOrg := gitURLPath[1:slashBeforeProj]

return gitURL.String()
return os.Expand(mirrorURLText, func(varName string) string {
if varName == "org" {
return gitOrg
}
if varName == "project" {
return gitProject
}
return ""
})
}

0 comments on commit 8b366da

Please sign in to comment.