Skip to content

Commit

Permalink
refactor: move common methods from project to common
Browse files Browse the repository at this point in the history
Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
  • Loading branch information
jiangxin committed Nov 13, 2020
1 parent 7b695e3 commit 593245d
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 277 deletions.
3 changes: 2 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/alibaba/git-repo-go/cap"
"github.com/alibaba/git-repo-go/color"
"github.com/alibaba/git-repo-go/common"
"github.com/alibaba/git-repo-go/config"
"github.com/alibaba/git-repo-go/errors"
"github.com/alibaba/git-repo-go/path"
Expand Down Expand Up @@ -372,7 +373,7 @@ Either delete the .repo folder in this workspace, or initialize in another locat

if !v.O.DetachHead {
if isNew || v.ws.ManifestProject.GetHead() == "" {
if !project.IsImmutable(v.ws.ManifestProject.Revision) {
if !common.IsImmutable(v.ws.ManifestProject.Revision) {
// Recreate default branch.
err := v.ws.ManifestProject.StartBranch("default", "", true)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package cmd

import (
"github.com/alibaba/git-repo-go/project"
"github.com/alibaba/git-repo-go/common"
log "github.com/jiangxin/multi-log"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -81,7 +81,7 @@ func (v startCommand) Execute(args []string) error {

for _, p := range allProjects {
merge := ""
if p.Revision == "" || project.IsImmutable(p.Revision) {
if p.Revision == "" || common.IsImmutable(p.Revision) {
if p.DestBranch != "" {
merge = p.DestBranch
} else {
Expand All @@ -91,7 +91,7 @@ func (v startCommand) Execute(args []string) error {
merge = rws.Manifest.Default.Revision
}
}
if merge != "" && project.IsImmutable(merge) {
if merge != "" && common.IsImmutable(merge) {
merge = ""
}
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sort"
"strings"

"github.com/alibaba/git-repo-go/common"
"github.com/alibaba/git-repo-go/config"
"github.com/alibaba/git-repo-go/editor"
"github.com/alibaba/git-repo-go/helper"
Expand Down Expand Up @@ -1018,7 +1019,7 @@ func (v uploadCommand) Execute(args []string) error {
head = config.RefsHeads + head
}
}
if !project.IsHead(head) {
if !common.IsHead(head) {
log.Debugf("detached at %s", head)
return fmt.Errorf("upload failed: not in a branch\n\n" +
"Please run command \"git checkout -b <branch>\" to create a new branch.")
Expand Down
65 changes: 65 additions & 0 deletions common/refs.go
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
}
59 changes: 59 additions & 0 deletions common/url.go
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
}
Loading

0 comments on commit 593245d

Please sign in to comment.