-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move common methods from project to common
Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
- Loading branch information
Showing
13 changed files
with
297 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package common | ||
|
||
import ( | ||
"strings" | ||
"unicode" | ||
|
||
"github.com/alibaba/git-repo-go/config" | ||
) | ||
|
||
// IsSha indecates revision is a commit id (hash). | ||
func IsSha(revision string) bool { | ||
if config.CommitIDPattern.MatchString(revision) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// IsTag indecates revision is a tag. | ||
func IsTag(revision string) bool { | ||
if strings.HasPrefix(revision, config.RefsTags) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// IsHead indecates revision is a branch. | ||
func IsHead(revision string) bool { | ||
if strings.HasPrefix(revision, config.RefsHeads) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// IsRef indecates revision is a ref. | ||
func IsRef(revision string) bool { | ||
if strings.HasPrefix(revision, "refs/") { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// IsImmutable indecates revision is a tag or sha or change. | ||
func IsImmutable(revision string) bool { | ||
if IsSha(revision) || IsTag(revision) { | ||
return true | ||
} | ||
if IsHead(revision) { | ||
return false | ||
} | ||
if strings.HasPrefix(revision, "refs/") { | ||
return true | ||
} | ||
// is a head | ||
return false | ||
} | ||
|
||
// IsASCII indicates string contains only ASCII. | ||
func IsASCII(s string) bool { | ||
for i := 0; i < len(s); i++ { | ||
if s[i] > unicode.MaxASCII { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/alibaba/git-repo-go/config" | ||
) | ||
|
||
// URLJoin appends fetch path (in remote element) and project name to manifest url. | ||
func URLJoin(u string, paths ...string) (string, error) { | ||
var err error | ||
|
||
// remove last part of url | ||
if len(u) > 0 && u[len(u)-1] == '/' { | ||
u = u[0 : len(u)-1] | ||
} | ||
i := strings.LastIndex(u, "/") | ||
if i > 0 { | ||
u = u[0:i] | ||
} | ||
|
||
for _, p := range paths { | ||
u, err = joinTwoURL(u, p) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
return u, nil | ||
} | ||
|
||
func joinTwoURL(l, r string) (string, error) { | ||
lURL := config.ParseGitURL(l) | ||
rURL := config.ParseGitURL(r) | ||
|
||
if rURL != nil { | ||
return r, nil | ||
} | ||
if lURL == nil { | ||
return "", fmt.Errorf("fail to parse URL: %s", l) | ||
} | ||
if lURL.Repo == "" { | ||
lURL.Repo = r | ||
} else { | ||
lPath := lURL.Repo | ||
if !filepath.IsAbs(lPath) { | ||
lPath = "/" + lPath | ||
} | ||
lPath = filepath.Join(lPath, r) | ||
lPath = filepath.ToSlash(filepath.Clean(lPath)) | ||
if !lURL.IsLocal() && len(lPath) > 0 && lPath[0] == '/' { | ||
lPath = lPath[1:] | ||
} | ||
lURL.Repo = lPath | ||
} | ||
|
||
return lURL.String(), nil | ||
} |
Oops, something went wrong.