From 1ff444a54c29df9e454fec60d3e6b015e8de96d4 Mon Sep 17 00:00:00 2001 From: Michal Gorecki Date: Fri, 23 Feb 2024 09:49:09 +0100 Subject: [PATCH] newt: Fix shallow upgrade with specific commit version Upgrading project using shallow option was causing an error if repository version was specified as specific commit. Because this specific commit could not yet be fetched it could not be found during version validation. Now if specific commit could not be found, instead of returning an error immediately, we first try to fetch it. --- newt/downloader/downloader.go | 18 ++++++++++++++++++ newt/repo/version.go | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/newt/downloader/downloader.go b/newt/downloader/downloader.go index 8ed2927b6..eea3249a8 100644 --- a/newt/downloader/downloader.go +++ b/newt/downloader/downloader.go @@ -61,6 +61,9 @@ type Downloader interface { // Fetches all remotes. Fetch(path string) error + // Fetches specific commit + FetchCommit(path string, commit string) error + // Checks out the specified commit (hash, tag, or branch). Always puts the // repo in a "detached head" state. Checkout(path string, commit string) error @@ -774,6 +777,11 @@ func (gd *GithubDownloader) Fetch(repoDir string) error { }) } +func (gd *GithubDownloader) FetchCommit(repoDir string, commit string) error { + _, err := executeGitCommand(repoDir, []string{"fetch", "--depth=1", "origin", commit}, true) + return err +} + func (gd *GithubDownloader) password() string { if gd.Password != "" { return gd.Password @@ -943,6 +951,11 @@ func (gd *GitDownloader) Fetch(repoDir string) error { }) } +func (gd *GitDownloader) FetchCommit(repoDir string, commit string) error { + _, err := executeGitCommand(repoDir, []string{"fetch", "--depth=1", "origin", commit}, true) + return err +} + func (gd *GitDownloader) FetchFile( commit string, path string, filename string, dstDir string) error { @@ -1043,6 +1056,11 @@ func (ld *LocalDownloader) Fetch(path string) error { return ld.Clone(ld.MainBranch(), path) } +func (ld *LocalDownloader) FetchCommit(path string, commit string) error { + _, err := executeGitCommand(path, []string{"fetch", "--depth=1", "origin", commit}, true) + return err +} + func (ld *LocalDownloader) Checkout(path string, commit string) error { _, err := executeGitCommand(path, []string{"checkout", commit}, true) return err diff --git a/newt/repo/version.go b/newt/repo/version.go index 5f0329be0..b4aa09c5b 100644 --- a/newt/repo/version.go +++ b/newt/repo/version.go @@ -126,6 +126,15 @@ func (r *Repo) VersionIsValid(ver newtutil.RepoVersion) bool { } cs, _ := r.downloader.CommitsFor(r.Path(), ver.Commit) + + // Try to fetch commit if it was not found + if len(cs) <= 0 { + err := r.Downloader().FetchCommit(r.Path(), ver.Commit) + if err != nil { + return false + } + cs, _ = r.downloader.CommitsFor(r.Path(), ver.Commit) + } return len(cs) > 0 }