MigrateRepository migrates a existing repository from other project hosting.
(doer, owner *User, opts MigrateRepoOptions)
| 794 | |
| 795 | // MigrateRepository migrates a existing repository from other project hosting. |
| 796 | func MigrateRepository(doer, owner *User, opts MigrateRepoOptions) (*Repository, error) { |
| 797 | repo, err := CreateRepository(doer, owner, CreateRepoOptionsLegacy{ |
| 798 | Name: opts.Name, |
| 799 | Description: opts.Description, |
| 800 | IsPrivate: opts.IsPrivate, |
| 801 | IsUnlisted: opts.IsUnlisted, |
| 802 | IsMirror: opts.IsMirror, |
| 803 | }) |
| 804 | if err != nil { |
| 805 | return nil, err |
| 806 | } |
| 807 | |
| 808 | repoPath := RepoPath(owner.Name, opts.Name) |
| 809 | wikiPath := WikiPath(owner.Name, opts.Name) |
| 810 | |
| 811 | if owner.IsOrganization() { |
| 812 | t, err := owner.GetOwnerTeam() |
| 813 | if err != nil { |
| 814 | return nil, err |
| 815 | } |
| 816 | repo.NumWatches = t.NumMembers |
| 817 | } else { |
| 818 | repo.NumWatches = 1 |
| 819 | } |
| 820 | |
| 821 | migrateTimeout := time.Duration(conf.Git.Timeout.Migrate) * time.Second |
| 822 | |
| 823 | RemoveAllWithNotice("Repository path erase before creation", repoPath) |
| 824 | cloneArgs := append(mirrorGitArgs(), "clone", "--mirror", "--quiet", "--end-of-options", opts.RemoteAddr, repoPath) |
| 825 | if _, stderr, err := process.ExecTimeoutEnv(migrateTimeout, mirrorGitEnv(), |
| 826 | fmt.Sprintf("MigrateRepository 'git clone': %s/%s", owner.Name, opts.Name), |
| 827 | "git", cloneArgs...); err != nil { |
| 828 | return repo, errors.Newf("clone: %v - %s", err, stderr) |
| 829 | } |
| 830 | |
| 831 | wikiRemotePath := wikiRemoteURL(opts.RemoteAddr) |
| 832 | if len(wikiRemotePath) > 0 { |
| 833 | RemoveAllWithNotice("Repository wiki path erase before creation", wikiPath) |
| 834 | wikiCloneArgs := append(mirrorGitArgs(), "clone", "--mirror", "--quiet", "--end-of-options", wikiRemotePath, wikiPath) |
| 835 | if _, stderr, err := process.ExecTimeoutEnv(migrateTimeout, mirrorGitEnv(), |
| 836 | fmt.Sprintf("MigrateRepository 'git clone' wiki: %s/%s", owner.Name, opts.Name), |
| 837 | "git", wikiCloneArgs...); err != nil { |
| 838 | log.Error("Failed to clone wiki: %v - %s", err, stderr) |
| 839 | RemoveAllWithNotice("Delete repository wiki for initialization failure", wikiPath) |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | // Check if repository is empty. |
| 844 | _, stderr, err := com.ExecCmdDir(repoPath, "git", "log", "-1") |
| 845 | if err != nil { |
| 846 | if strings.Contains(stderr, "fatal: bad default revision 'HEAD'") { |
| 847 | repo.IsBare = true |
| 848 | } else { |
| 849 | return repo, errors.Newf("check bare: %v - %s", err, stderr) |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | if !repo.IsBare { |
no test coverage detected