Skip to content

Commit

Permalink
update git support
Browse files Browse the repository at this point in the history
  • Loading branch information
xtuc committed May 28, 2020
1 parent 224a073 commit 1089299
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 44 deletions.
56 changes: 35 additions & 21 deletions cmd/autoupdate/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,40 @@ func updateGit(ctx context.Context, pckg *packages.Package) []newVersionToCommit

out, err := packages.GitClone(ctx, pckg, packageGitcache)
if err != nil {
util.Printf(ctx, "could not clone repo: %s: %s", err, out)
util.Printf(ctx, "could not clone repo: %s: %s\n", err, out)
return newVersionsToCommit
}
} else {
packages.GitFetch(ctx, packageGitcache)
}

gitVersions := packages.GitTags(ctx, pckg, packageGitcache)
util.Debugf(ctx, "found versions in git: %s", gitVersions)
gitTags := packages.GitTags(ctx, pckg, packageGitcache)
util.Debugf(ctx, "found tags in git: %s\n", gitTags)

gitVersions := make([]git.GitVersion, 0)
for _, tag := range gitTags {
gitVersions = append(gitVersions, git.GitVersion{
Tag: tag,
Version: strings.TrimPrefix(tag, "v"),
})
}

existingVersionSet := pckg.Versions()

existingVersionSet := getSemverOnly(pckg.Versions())
if len(existingVersionSet) > 0 {
lastExistingVersion, err := semver.Make(existingVersionSet[len(existingVersionSet)-1])
util.Check(err)
util.Debugf(ctx, "last exists version: %s", lastExistingVersion)
if err != nil {
util.Debugf(ctx, "error while getting the lastest version: %s\n", err)
return newVersionsToCommit
}
util.Debugf(ctx, "last exists version: %s\n", lastExistingVersion)

versionDiff := gitVersionDiff(gitVersions, existingVersionSet)

newGitVersions := make([]string, 0)
newGitVersions := make([]git.GitVersion, 0)

for i := len(versionDiff) - 1; i >= 0; i-- {
gitVersion, err := semver.Make(versionDiff[i])
gitVersion, err := semver.Make(versionDiff[i].Version)
if err != nil {
continue
}
Expand All @@ -62,7 +74,10 @@ func updateGit(ctx context.Context, pckg *packages.Package) []newVersionToCommit
}
}

util.Debugf(ctx, "new versions: %s", newGitVersions)
util.Debugf(ctx, "new versions: %s\n", newGitVersions)

sort.Sort(sort.Reverse(git.ByGitVersion(newGitVersions)))

newVersionsToCommit = doUpdateGit(ctx, pckg, packageGitcache, newGitVersions)
} else {
// Import all the versions since we have none locally.
Expand All @@ -84,24 +99,21 @@ func updateGit(ctx context.Context, pckg *packages.Package) []newVersionToCommit
return newVersionsToCommit
}

func doUpdateGit(ctx context.Context, pckg *packages.Package, gitpath string, versions []string) []newVersionToCommit {
func doUpdateGit(ctx context.Context, pckg *packages.Package, gitpath string, versions []git.GitVersion) []newVersionToCommit {
newVersionsToCommit := make([]newVersionToCommit, 0)

if len(versions) == 0 {
return newVersionsToCommit
}

for _, version := range versions {
packages.GitCheckout(ctx, pckg, gitpath, version)
for _, gitversion := range versions {
packages.GitForceCheckout(ctx, pckg, gitpath, gitversion.Tag)
filesToCopy := pckg.NpmFilesFrom(gitpath)

// Remove the v prefix in the version, for example v1.0.1
// Note that we do it after the checkout so the git tag is still valid
version = strings.TrimPrefix(version, "v")

pckgpath := path.Join(pckg.Path(), version)
pckgpath := path.Join(pckg.Path(), gitversion.Version)

if _, err := os.Stat(pckgpath); !os.IsNotExist(err) {
util.Debugf(ctx, "%s already exists; aborting\n", pckgpath)
continue
}

Expand Down Expand Up @@ -129,25 +141,27 @@ func doUpdateGit(ctx context.Context, pckg *packages.Package, gitpath string, ve

newVersionsToCommit = append(newVersionsToCommit, newVersionToCommit{
versionPath: pckgpath,
newVersion: version,
newVersion: gitversion.Version,
pckg: pckg,
})
} else {
util.Debugf(ctx, "no files matched\n")
}
}

return newVersionsToCommit
}

func gitVersionDiff(a []string, b []string) []string {
diff := make([]string, 0)
func gitVersionDiff(a []git.GitVersion, b []string) []git.GitVersion {
diff := make([]git.GitVersion, 0)
m := make(map[string]bool)

for _, item := range b {
m[item] = true
}

for _, item := range a {
if _, ok := m[item]; !ok {
if _, ok := m[item.Version]; !ok {
diff = append(diff, item)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/autoupdate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func main() {
newVersionsToCommit = updateNpm(ctx, pckg)
}

if pckg.Autoupdate.Source == "git" && pckg.Name == "hi-sven-git" {
if pckg.Autoupdate.Source == "git" {
util.Debugf(ctx, "running git update")
newVersionsToCommit = updateGit(ctx, pckg)
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/autoupdate/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func updateNpm(ctx context.Context, pckg *packages.Package) []newVersionToCommit
}
}

sort.Sort(sort.Reverse(npm.ByNpmVersion(npmVersions)))

newVersionsToCommit = doUpdateNpm(ctx, pckg, newNpmVersions)
} else {
// Import all the versions since we have none locally.
Expand Down Expand Up @@ -77,6 +79,7 @@ func doUpdateNpm(ctx context.Context, pckg *packages.Package, versions []npm.Npm
pckgpath := path.Join(pckg.Path(), version.Version)

if _, err := os.Stat(pckgpath); !os.IsNotExist(err) {
util.Debugf(ctx, "%s already exists; aborting", pckgpath)
continue
}

Expand Down
6 changes: 6 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package git

type GitVersion struct {
Tag string
Version string
}
23 changes: 3 additions & 20 deletions git/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,16 @@ import (
"github.com/blang/semver"
)

type ByGitVersion []string
type ByGitVersion []GitVersion

func (a ByGitVersion) Len() int { return len(a) }
func (a ByGitVersion) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByGitVersion) Less(i, j int) bool {
left, leftErr := semver.Make(a[i])
left, leftErr := semver.Make(a[i].Version)
if leftErr != nil {
return false
}
right, rightErr := semver.Make(a[j])
if rightErr != nil {
return true
}
return left.Compare(right) == 1
}

// ByGitVersionString implements sort.Interface for []String
type ByGitVersionString []string

func (a ByGitVersionString) Len() int { return len(a) }
func (a ByGitVersionString) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByGitVersionString) Less(i, j int) bool {
left, leftErr := semver.Make(a[i])
if leftErr != nil {
return false
}
right, rightErr := semver.Make(a[j])
right, rightErr := semver.Make(a[j].Version)
if rightErr != nil {
return true
}
Expand Down
4 changes: 2 additions & 2 deletions packages/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func GitTags(ctx context.Context, pckg *Package, gitpath string) []string {
return strings.Split(out, "\n")
}

func GitCheckout(ctx context.Context, pckg *Package, gitpath string, tag string) {
args := []string{"checkout", tag}
func GitForceCheckout(ctx context.Context, pckg *Package, gitpath string, tag string) {
args := []string{"checkout", tag, "-f"}

cmd := exec.Command("git", args...)
cmd.Dir = gitpath
Expand Down

0 comments on commit 1089299

Please sign in to comment.