diff options
author | Unknwon <u@gogs.io> | 2017-02-15 21:00:46 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2017-02-15 21:00:46 -0500 |
commit | f35bd3400206bf4c1b19050dba99fd3dc5d6edc4 (patch) | |
tree | ebc98d3e04a07aadc4c557b90a0ee357f34bd36d | |
parent | b9560ec9cbb05fddaa7ca0e2aee162a5ccdd0d01 (diff) |
models/repo: use reset --hard to align with remote branch (#4123)
If user has force pushed to a branch, git pull will fail.
-rw-r--r-- | models/repo.go | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/models/repo.go b/models/repo.go index 9a286db2..be46b797 100644 --- a/models/repo.go +++ b/models/repo.go @@ -468,35 +468,33 @@ func (repo *Repository) LocalCopyPath() string { 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. // 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. -func UpdateLocalCopyBranch(repoPath, localPath, branch string) error { +func UpdateLocalCopyBranch(repoPath, localPath, branch string) (err error) { 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, Branch: branch, }); err != nil { return fmt.Errorf("git clone %s: %v", branch, err) } } else { - if err := git.Fetch(localPath, git.FetchRemoteOptions{ + if err = git.Fetch(localPath, git.FetchRemoteOptions{ Prune: true, }); err != nil { return fmt.Errorf("git fetch: %v", err) } - if err := git.Checkout(localPath, git.CheckoutOptions{ + if err = git.Checkout(localPath, git.CheckoutOptions{ Branch: branch, }); err != nil { 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, - Remote: "origin", - Branch: branch, - }); err != nil { - return fmt.Errorf("git pull origin %s: %v", branch, err) + + // Reset to align with remote in case of force push. + if err = git.ResetHEAD(localPath, true, "origin/"+branch); err != nil { + return fmt.Errorf("git reset --hard origin/%s: %v", branch, err) } } return nil |