Browse Source

models/repo: use reset --hard to align with remote branch (#4123)

If user has force pushed to a branch, git pull will fail.
pull/3785/merge
Unknwon 8 years ago
parent
commit
f35bd34002
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 20
      models/repo.go

20
models/repo.go

@ -468,35 +468,33 @@ func (repo *Repository) LocalCopyPath() string {
return path.Join(setting.AppDataPath, "tmp/local-repo", com.ToStr(repo.ID)) return path.Join(setting.AppDataPath, "tmp/local-repo", com.ToStr(repo.ID))
} }
// UpdateLocalCopy pulls latest changes of given branch from repoPath to localPath. // UpdateLocalCopy fetches latest changes of given branch from repoPath to localPath.
// It creates a new clone if local copy does not exist. // It creates a new clone if local copy does not exist.
// This function checks out target branch by default, it is safe to assume subsequent // This function checks out target branch by default, it is safe to assume subsequent
// operations are operating against target branch when caller has confidence for no race condition. // operations are operating against target branch when caller has confidence for no race condition.
func UpdateLocalCopyBranch(repoPath, localPath, branch string) error { func UpdateLocalCopyBranch(repoPath, localPath, branch string) (err error) {
if !com.IsExist(localPath) { if !com.IsExist(localPath) {
if err := git.Clone(repoPath, localPath, git.CloneRepoOptions{ if err = git.Clone(repoPath, localPath, git.CloneRepoOptions{
Timeout: time.Duration(setting.Git.Timeout.Clone) * time.Second, Timeout: time.Duration(setting.Git.Timeout.Clone) * time.Second,
Branch: branch, Branch: branch,
}); err != nil { }); err != nil {
return fmt.Errorf("git clone %s: %v", branch, err) return fmt.Errorf("git clone %s: %v", branch, err)
} }
} else { } else {
if err := git.Fetch(localPath, git.FetchRemoteOptions{ if err = git.Fetch(localPath, git.FetchRemoteOptions{
Prune: true, Prune: true,
}); err != nil { }); err != nil {
return fmt.Errorf("git fetch: %v", err) return fmt.Errorf("git fetch: %v", err)
} }
if err := git.Checkout(localPath, git.CheckoutOptions{ if err = git.Checkout(localPath, git.CheckoutOptions{
Branch: branch, Branch: branch,
}); err != nil { }); err != nil {
return fmt.Errorf("git checkout %s: %v", branch, err) return fmt.Errorf("git checkout %s: %v", branch, err)
} }
if err := git.Pull(localPath, git.PullRemoteOptions{
Timeout: time.Duration(setting.Git.Timeout.Pull) * time.Second, // Reset to align with remote in case of force push.
Remote: "origin", if err = git.ResetHEAD(localPath, true, "origin/"+branch); err != nil {
Branch: branch, return fmt.Errorf("git reset --hard origin/%s: %v", branch, err)
}); err != nil {
return fmt.Errorf("git pull origin %s: %v", branch, err)
} }
} }
return nil return nil

Loading…
Cancel
Save