Repository: go-gitea/gitea
File: services/repository/transfer.go (Go)

Code before:
// changeRepositoryName changes all corresponding setting from old repository name to new one.
func changeRepositoryName(ctx context.Context, repo *repo_model.Repository, newRepoName string) (err error) {
	oldRepoName := repo.Name
	newRepoName = strings.ToLower(newRepoName)
	if err = repo_model.IsUsableRepoName(newRepoName); err != nil {
		return err
	}

	if err := repo.LoadOwner(ctx); err != nil {
		return err
	}

	has, err := repo_model.IsRepositoryModelOrDirExist(ctx, repo.Owner, newRepoName)
	if err != nil {
		return fmt.Errorf("IsRepositoryExist: %w", err)
	} else if has {
		return repo_model.ErrRepoAlreadyExist{
			Uname: repo.OwnerName,
			Name:  newRepoName,
		}
	}

	if err = gitrepo.RenameRepository(ctx, repo,
		repo_model.StorageRepo{
			RelativePath:     repo_model.RelativePath(repo.OwnerName, newRepoName),
			ObjectFormatName: repo.ObjectFormatName,
		}); err != nil {
		return fmt.Errorf("rename repository directory: %w", err)
	}

	wikiPath := repo.WikiPath()
	isExist, err := util.IsExist(wikiPath)
	if err != nil {
		log.Error("Unable to check if %s exists. Error: %v", wikiPath, err)
		return err
	}
	if isExist {
		if err = util.Rename(wikiPath, repo_model.WikiPath(repo.Owner.Name, newRepoName)); err != nil {
			return fmt.Errorf("rename repository wiki: %w", err)
		}
	}

	return db.WithTx(ctx, func(ctx context.Context) error {
		return repo_model.NewRedirect(ctx, repo.Owner.ID, repo.ID, oldRepoName, newRepoName)
	})
}

// ChangeRepositoryName changes all corresponding setting from old repository name to new one.
func ChangeRepositoryName(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, newRepoName string) error {
	log.Trace("ChangeRepositoryName: %s/%s -> %s", doer.Name, repo.Name, newRepoName)


Review comment (bug):
You see, I have told you, it is not right.

I do not see why your design would work without buggy behaviors.

* https://github.com/go-gitea/gitea/pull/33911#discussion_r2018004373

Code after:
		return fmt.Errorf("repo_model.NewRedirect: %w", err)
	}

	newRepo, err := repo_model.GetRepositoryByID(ctx, repo.ID)
	if err != nil {
		return err
	}

	for _, team := range teams {
		if err := addRepositoryToTeam(ctx, team, newRepo); err != nil {
			return err
		}
	}

	return committer.Commit()
}

// changeRepositoryName changes all corresponding setting from old repository name to new one.
func changeRepositoryName(ctx context.Context, repo *repo_model.Repository, newRepoName string) (err error) {
	oldRepoName := repo.Name
	newRepoName = strings.ToLower(newRepoName)
	if err = repo_model.IsUsableRepoName(newRepoName); err != nil {
		return err
	}

	if err := repo.LoadOwner(ctx); err != nil {
		return err
	}

	has, err := repo_model.IsRepositoryModelOrDirExist(ctx, repo.Owner, newRepoName)
	if err != nil {
		return fmt.Errorf("IsRepositoryExist: %w", err)
	} else if has {
		return repo_model.ErrRepoAlreadyExist{
			Uname: repo.OwnerName,
			Name:  newRepoName,
		}
	}

	if err = gitrepo.RenameRepository(ctx, repo,
		repo_model.StorageRepo(repo_model.RelativePath(repo.OwnerName, newRepoName))); err != nil {
		return fmt.Errorf("rename repository directory: %w", err)
	}

	if HasWiki(ctx, repo) {
		if err = gitrepo.RenameRepository(ctx, repo.WikiStorageRepo(), repo_model.StorageRepo(
			repo_model.RelativeWikiPath(repo.OwnerName, newRepoName))); err != nil {
			return fmt.Errorf("rename repository wiki: %w", err)
		}
	}