MCPcopy Create free account
hub / github.com/gogs/gogs / ForkRepository

Function ForkRepository

internal/database/repo.go:2539–2611  ·  view source on GitHub ↗

ForkRepository creates a fork of target repository under another user domain.

(doer, owner *User, baseRepo *Repository, name, desc string)

Source from the content-addressed store, hash-verified

2537
2538// ForkRepository creates a fork of target repository under another user domain.
2539func ForkRepository(doer, owner *User, baseRepo *Repository, name, desc string) (_ *Repository, err error) {
2540 if !owner.canCreateRepo() {
2541 return nil, ErrReachLimitOfRepo{Limit: owner.maxNumRepos()}
2542 }
2543
2544 repo := &Repository{
2545 OwnerID: owner.ID,
2546 Owner: owner,
2547 Name: name,
2548 LowerName: strings.ToLower(name),
2549 Description: desc,
2550 DefaultBranch: baseRepo.DefaultBranch,
2551 IsPrivate: baseRepo.IsPrivate,
2552 IsUnlisted: baseRepo.IsUnlisted,
2553 IsFork: true,
2554 ForkID: baseRepo.ID,
2555 }
2556
2557 sess := x.NewSession()
2558 defer sess.Close()
2559 if err = sess.Begin(); err != nil {
2560 return nil, err
2561 }
2562
2563 if err = createRepository(sess, doer, owner, repo); err != nil {
2564 return nil, err
2565 } else if _, err = sess.Exec("UPDATE `repository` SET num_forks=num_forks+1 WHERE id=?", baseRepo.ID); err != nil {
2566 return nil, err
2567 }
2568
2569 repoPath := repo.repoPath(sess)
2570 RemoveAllWithNotice("Repository path erase before creation", repoPath)
2571
2572 _, stderr, err := process.ExecTimeout(10*time.Minute,
2573 fmt.Sprintf("ForkRepository 'git clone': %s/%s", owner.Name, repo.Name),
2574 "git", "clone", "--bare", baseRepo.RepoPath(), repoPath)
2575 if err != nil {
2576 return nil, errors.Newf("git clone: %v - %s", err, stderr)
2577 }
2578
2579 _, stderr, err = process.ExecDir(-1,
2580 repoPath, fmt.Sprintf("ForkRepository 'git update-server-info': %s", repoPath),
2581 "git", "update-server-info")
2582 if err != nil {
2583 return nil, errors.Newf("git update-server-info: %v - %s", err, stderr)
2584 }
2585
2586 if err = createDelegateHooks(repoPath); err != nil {
2587 return nil, errors.Newf("createDelegateHooks: %v", err)
2588 }
2589
2590 if err = sess.Commit(); err != nil {
2591 return nil, errors.Newf("commit: %v", err)
2592 }
2593
2594 // Remember visibility preference
2595 err = Handle.Users().Update(context.TODO(), owner.ID, UpdateUserOptions{LastRepoVisibility: &repo.IsPrivate})
2596 if err != nil {

Callers 1

ForkPostFunction · 0.92

Calls 15

repoPathMethod · 0.95
UpdateSizeMethod · 0.95
APIFormatLegacyMethod · 0.95
ExecTimeoutFunction · 0.92
ExecDirFunction · 0.92
createRepositoryFunction · 0.85
RemoveAllWithNoticeFunction · 0.85
createDelegateHooksFunction · 0.85
PrepareWebhooksFunction · 0.85
canCreateRepoMethod · 0.80
maxNumReposMethod · 0.80
ExecMethod · 0.80

Tested by

no test coverage detected