From c2ac77bd735ae743a509e29640466d049d86c65e Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Thu, 28 May 2020 14:23:49 +0100 Subject: [PATCH] update git support --- cmd/autoupdate/git.go | 69 ++++++++++++++++++++++++++++-------------- cmd/autoupdate/main.go | 2 +- cmd/autoupdate/npm.go | 3 ++ git/git.go | 6 ++++ git/sort.go | 23 ++------------ packages/git.go | 16 +++++++--- 6 files changed, 71 insertions(+), 48 deletions(-) create mode 100644 git/git.go diff --git a/cmd/autoupdate/git.go b/cmd/autoupdate/git.go index aed9aaee..2269963f 100644 --- a/cmd/autoupdate/git.go +++ b/cmd/autoupdate/git.go @@ -19,11 +19,15 @@ var ( GIT_CACHE = path.Join(BASE_PATH, "git-cache") ) +func isValidGit(ctx context.Context, pckgdir string) bool { + _, err := os.Stat(path.Join(pckgdir, ".git")) + return !os.IsNotExist(err) +} + func updateGit(ctx context.Context, pckg *packages.Package) []newVersionToCommit { var newVersionsToCommit []newVersionToCommit packageGitcache := path.Join(GIT_CACHE, pckg.Name) - // If the local copy of the package's git doesn't exists, Clone it. If it does // just fetch new tags if _, err := os.Stat(packageGitcache); os.IsNotExist(err) { @@ -31,28 +35,45 @@ 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) + if isValidGit(ctx, packageGitcache) { + packages.GitFetch(ctx, packageGitcache) + } else { + util.Printf(ctx, "invalid git repo\n") + return newVersionsToCommit + } + } + + 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"), + }) } - gitVersions := packages.GitTags(ctx, pckg, packageGitcache) - util.Debugf(ctx, "found versions in git: %s", gitVersions) + 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 } @@ -62,7 +83,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. @@ -84,24 +108,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 } @@ -129,17 +150,19 @@ 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 { @@ -147,7 +170,7 @@ func gitVersionDiff(a []string, b []string) []string { } for _, item := range a { - if _, ok := m[item]; !ok { + if _, ok := m[item.Version]; !ok { diff = append(diff, item) } } diff --git a/cmd/autoupdate/main.go b/cmd/autoupdate/main.go index abbd40f8..dd5852d1 100644 --- a/cmd/autoupdate/main.go +++ b/cmd/autoupdate/main.go @@ -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) } diff --git a/cmd/autoupdate/npm.go b/cmd/autoupdate/npm.go index edac56a3..ddfd5757 100644 --- a/cmd/autoupdate/npm.go +++ b/cmd/autoupdate/npm.go @@ -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. @@ -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 } diff --git a/git/git.go b/git/git.go new file mode 100644 index 00000000..2ba1b5fa --- /dev/null +++ b/git/git.go @@ -0,0 +1,6 @@ +package git + +type GitVersion struct { + Tag string + Version string +} diff --git a/git/sort.go b/git/sort.go index 892abe04..7fbb92b0 100644 --- a/git/sort.go +++ b/git/sort.go @@ -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 } diff --git a/packages/git.go b/packages/git.go index 12980dcc..9eeb6f71 100644 --- a/packages/git.go +++ b/packages/git.go @@ -85,7 +85,7 @@ func GitFetch(ctx context.Context, gitpath string) { cmd := exec.Command("git", args...) cmd.Dir = gitpath - util.Debugf(ctx, "run %s\n", cmd) + util.Debugf(ctx, "%s: run %s\n", gitpath, cmd) util.CheckCmd(cmd.CombinedOutput()) } @@ -115,11 +115,19 @@ func GitTags(ctx context.Context, pckg *Package, gitpath string) []string { cmd.Dir = gitpath util.Debugf(ctx, "run %s\n", cmd) out := util.CheckCmd(cmd.CombinedOutput()) - return strings.Split(out, "\n") + + tags := make([]string, 0) + for _, line := range strings.Split(out, "\n") { + if strings.Trim(line, " ") != "" { + tags = append(tags, line) + } + } + + return tags } -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