Skip to content

Commit

Permalink
newt: Fix shallow upgrade with specific commit version
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
m-gorecki authored and sjanc committed Mar 4, 2024
1 parent efe37ed commit 4632ba2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
18 changes: 18 additions & 0 deletions newt/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions newt/repo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 4632ba2

Please sign in to comment.