Skip to content

Commit

Permalink
Skip fetching external repos if version is SHA
Browse files Browse the repository at this point in the history
If external repo has version specified as commit SHA and that SHA is
currently checked out, we don't need to fetch that repo.
  • Loading branch information
andrzej-kaczmarek committed Oct 25, 2024
1 parent f7a54c8 commit 3f3af0d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
27 changes: 27 additions & 0 deletions newt/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions newt/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions newt/repo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package repo

import (
"mynewt.apache.org/newt/newt/downloader"
"strings"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3f3af0d

Please sign in to comment.