Skip to content

Commit 10f0063

Browse files
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.
1 parent 092e4a2 commit 10f0063

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

newt/downloader/downloader.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ type Downloader interface {
8888
// the repo is in a "detached head" state.
8989
CurrentBranch(path string) (string, error)
9090

91+
// Retrieves commit SHA for current HEAD
92+
CurrentHead() string
93+
94+
// Retrieves full SHA for given commit
95+
CommitSha(path string, commit string) (string, error)
96+
9197
// LatestRc finds the commit of the latest release candidate. It looks
9298
// for commits with names matching the base commit string, but with with
9399
// "_rc#" inserted. This is useful when a release candidate is being
@@ -588,6 +594,13 @@ func (gd *GenericDownloader) HashFor(path string,
588594
return c.hash, nil
589595
}
590596

597+
if len(commit) < 40 {
598+
sha, err := gd.CommitSha(path, commit)
599+
if err == nil {
600+
commit = sha
601+
}
602+
}
603+
591604
return commit, nil
592605
}
593606

@@ -664,6 +677,20 @@ func (gd *GenericDownloader) CurrentBranch(path string) (string, error) {
664677
return branch, nil
665678
}
666679

680+
func (gd *GenericDownloader) CurrentHead() string {
681+
return gd.head
682+
}
683+
684+
func (gd *GenericDownloader) CommitSha(path string, commit string) (string, error) {
685+
cmd := []string{"rev-parse", commit}
686+
o, err := executeGitCommand(path, cmd, true)
687+
if err != nil {
688+
return "", err
689+
}
690+
691+
return strings.TrimSpace(string(o)), nil
692+
}
693+
667694
// Fetches the downloader's origin remote if it hasn't been fetched yet during
668695
// this run.
669696
func (gd *GenericDownloader) cachedFetch(fn func() error) error {

newt/project/project.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,11 @@ func (proj *Project) downloadRepositoryYmlFiles() error {
593593
"External repository \"%s\" does not specify valid commit version (%s)",
594594
r.Name(), ver.String())
595595
}
596+
597+
// No need to fetch if requested commit is already checked out
598+
if r.IsHeadCommit(ver.Commit) {
599+
continue
600+
}
596601
}
597602

598603
if _, err := r.UpdateDesc(); err != nil {

newt/repo/version.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package repo
2323

2424
import (
25+
"mynewt.apache.org/newt/newt/downloader"
2526
"strings"
2627

2728
log "github.com/sirupsen/logrus"
@@ -119,6 +120,19 @@ func (r *Repo) CommitFromVer(ver newtutil.RepoVersion) (string, error) {
119120
return commit, nil
120121
}
121122

123+
func (r *Repo) IsHeadCommit(commit string) bool {
124+
ct, _ := r.downloader.CommitType(r.Path(), commit)
125+
126+
if ct != downloader.COMMIT_TYPE_HASH {
127+
return false
128+
}
129+
130+
head := r.downloader.CurrentHead()
131+
sha, _ := r.downloader.CommitSha(r.Path(), commit)
132+
133+
return head == sha
134+
}
135+
122136
func (r *Repo) VersionIsValid(ver newtutil.RepoVersion) bool {
123137
if ver.Commit == "" {
124138
_, err := r.CommitFromVer(ver)

0 commit comments

Comments
 (0)