UpdateLocalCopy fetches latest changes of given branch from repoPath to localPath. It creates a new clone if local copy does not exist, but does not checks out to a specific branch if the local copy belongs to a wiki. For existing local copy, it checks out to target branch by default, and safe to as
(repoPath, localPath, branch string, isWiki bool)
| 656 | // assume subsequent operations are against target branch when caller has confidence |
| 657 | // about no race condition. |
| 658 | func UpdateLocalCopyBranch(repoPath, localPath, branch string, isWiki bool) (err error) { |
| 659 | if !osutil.Exist(localPath) { |
| 660 | // Checkout to a specific branch fails when wiki is an empty repository. |
| 661 | if isWiki { |
| 662 | branch = "" |
| 663 | } |
| 664 | if err = git.Clone(repoPath, localPath, git.CloneOptions{ |
| 665 | Branch: branch, |
| 666 | Timeout: time.Duration(conf.Git.Timeout.Clone) * time.Second, |
| 667 | }); err != nil { |
| 668 | return errors.Newf("git clone [branch: %s]: %v", branch, err) |
| 669 | } |
| 670 | return nil |
| 671 | } |
| 672 | |
| 673 | gitRepo, err := git.Open(localPath) |
| 674 | if err != nil { |
| 675 | return errors.Newf("open repository: %v", err) |
| 676 | } |
| 677 | |
| 678 | if err = gitRepo.Fetch(git.FetchOptions{ |
| 679 | Prune: true, |
| 680 | }); err != nil { |
| 681 | return errors.Newf("fetch: %v", err) |
| 682 | } |
| 683 | |
| 684 | if err = gitRepo.Checkout(branch); err != nil { |
| 685 | return errors.Newf("checkout [branch: %s]: %v", branch, err) |
| 686 | } |
| 687 | |
| 688 | // Reset to align with remote in case of force push. |
| 689 | rev := "origin/" + branch |
| 690 | if err = gitRepo.Reset(rev, git.ResetOptions{ |
| 691 | Hard: true, |
| 692 | }); err != nil { |
| 693 | return errors.Newf("reset [revision: %s]: %v", rev, err) |
| 694 | } |
| 695 | return nil |
| 696 | } |
| 697 | |
| 698 | // UpdateLocalCopyBranch makes sure local copy of repository in given branch is up-to-date. |
| 699 | func (r *Repository) UpdateLocalCopyBranch(branch string) error { |
no test coverage detected