From 3f3af0da7702d168a827dcd4670052abb69125c1 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Fri, 25 Oct 2024 14:43:46 +0200 Subject: [PATCH] Skip fetching external repos if version is SHA If external repo has version specified as commit SHA and that SHA is currently checked out, we don't need to fetch that repo. --- newt/downloader/downloader.go | 27 +++++++++++++++++++++++++++ newt/project/project.go | 5 +++++ newt/repo/version.go | 14 ++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/newt/downloader/downloader.go b/newt/downloader/downloader.go index 2be323a14..c3a9bc530 100644 --- a/newt/downloader/downloader.go +++ b/newt/downloader/downloader.go @@ -88,6 +88,12 @@ type Downloader interface { // the repo is in a "detached head" state. CurrentBranch(path string) (string, error) + // Retrieves commit SHA for current HEAD + CurrentHead() string + + // Retrieves full SHA for given commit + CommitSha(path string, commit string) (string, error) + // LatestRc finds the commit of the latest release candidate. It looks // for commits with names matching the base commit string, but with with // "_rc#" inserted. This is useful when a release candidate is being @@ -588,6 +594,13 @@ func (gd *GenericDownloader) HashFor(path string, return c.hash, nil } + if len(commit) < 40 { + sha, err := gd.CommitSha(path, commit) + if err == nil { + commit = sha + } + } + return commit, nil } @@ -664,6 +677,20 @@ func (gd *GenericDownloader) CurrentBranch(path string) (string, error) { return branch, nil } +func (gd *GenericDownloader) CurrentHead() string { + return gd.head +} + +func (gd *GenericDownloader) CommitSha(path string, commit string) (string, error) { + cmd := []string{"rev-parse", commit} + o, err := executeGitCommand(path, cmd, true) + if err != nil { + return "", err + } + + return strings.TrimSpace(string(o)), nil +} + // Fetches the downloader's origin remote if it hasn't been fetched yet during // this run. func (gd *GenericDownloader) cachedFetch(fn func() error) error { diff --git a/newt/project/project.go b/newt/project/project.go index 8c917714d..2bafb42fc 100644 --- a/newt/project/project.go +++ b/newt/project/project.go @@ -593,6 +593,11 @@ func (proj *Project) downloadRepositoryYmlFiles() error { "External repository \"%s\" does not specify valid commit version (%s)", r.Name(), ver.String()) } + + // No need to fetch if requested commit is already checked out + if r.IsHeadCommit(ver.Commit) { + continue + } } if _, err := r.UpdateDesc(); err != nil { diff --git a/newt/repo/version.go b/newt/repo/version.go index b4aa09c5b..bc31af2aa 100644 --- a/newt/repo/version.go +++ b/newt/repo/version.go @@ -22,6 +22,7 @@ package repo import ( + "mynewt.apache.org/newt/newt/downloader" "strings" log "github.com/sirupsen/logrus" @@ -119,6 +120,19 @@ func (r *Repo) CommitFromVer(ver newtutil.RepoVersion) (string, error) { return commit, nil } +func (r *Repo) IsHeadCommit(commit string) bool { + ct, _ := r.downloader.CommitType(r.Path(), commit) + + if ct != downloader.COMMIT_TYPE_HASH { + return false + } + + head := r.downloader.CurrentHead() + sha, _ := r.downloader.CommitSha(r.Path(), commit) + + return head == sha +} + func (r *Repo) VersionIsValid(ver newtutil.RepoVersion) bool { if ver.Commit == "" { _, err := r.CommitFromVer(ver)