Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update cloneOrOpenRepo to verify matching remote before updating repo. #308

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const (
releaseBranchPrefix = "release-"
)

var ErrNoMatchingRemote = errors.New("no matching remote")

// setVerboseTrace enables maximum verbosity output.
func setVerboseTrace() error {
if err := setVerbose(5, 2, 2, 2, 2, 2, 2, 2); err != nil {
Expand Down Expand Up @@ -441,7 +443,7 @@ func cloneOrOpenRepo(repoPath, repoURL string, updateRepository bool, opts *git.

// If the repo already exists, just update it
if preexisting {
return updateRepo(targetDir, true)
return updateRepo(targetDir, true, repoURL)
}

if opts == nil {
Expand Down Expand Up @@ -475,7 +477,7 @@ func cloneOrOpenRepo(repoPath, repoURL string, updateRepository bool, opts *git.
return nil, err
}

return updateRepo(targetDir, updateRepository)
return updateRepo(targetDir, updateRepository, repoURL)
}

// cloneRepository is a utility function that exposes the bare git clone
Expand Down Expand Up @@ -526,16 +528,32 @@ func ensureRepoPath(repoPath string) (targetDir string, exisitingDir bool, err e

// updateRepo tries to open the provided repoPath and fetches the latest
// changes from the configured remote location
func updateRepo(repoPath string, updateRepository bool) (*Repo, error) {
func updateRepo(repoPath string, updateRepository bool, repoURL string) (*Repo, error) {
r, err := OpenRepo(repoPath)
if err != nil {
return nil, err
}

if updateRepository {
remotes, err := r.Remotes()
if err != nil {
return nil, fmt.Errorf("unable to get repository remotes: %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
return nil, fmt.Errorf("unable to get repository remotes: %v", err)
return nil, fmt.Errorf("unable to get repository remotes: %w", err)

}
var remoteName string
for _, remote := range remotes {
for _, url := range remote.URLs() {
if url == repoURL {
remoteName = remote.Name()
break
}
}
}
if remoteName == "" {
return nil, fmt.Errorf("unable to find remote with matching URL: %w", ErrNoMatchingRemote)
}
// Update the repo
if err := filterCommand(
r.Dir(), "pull", "--rebase",
r.Dir(), "pull", remoteName, "--rebase",
).RunSilentSuccess(); err != nil {
return nil, fmt.Errorf("unable to pull from remote: %w", err)
}
Expand Down