Skip to content

Commit 40c6eb0

Browse files
authored
Fix to use only needed columns from tables to get repository git paths (#3870) (#3883)
1 parent 2996573 commit 40c6eb0

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

models/repo.go

+47-3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func NewRepoContext() {
163163
type Repository struct {
164164
ID int64 `xorm:"pk autoincr"`
165165
OwnerID int64 `xorm:"UNIQUE(s)"`
166+
OwnerName string `xorm:"-"`
166167
Owner *User `xorm:"-"`
167168
LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
168169
Name string `xorm:"INDEX NOT NULL"`
@@ -223,9 +224,17 @@ func (repo *Repository) MustOwner() *User {
223224
return repo.mustOwner(x)
224225
}
225226

227+
// MustOwnerName always returns valid owner name to avoid
228+
// conceptually impossible error handling.
229+
// It returns "error" and logs error details when error
230+
// occurs.
231+
func (repo *Repository) MustOwnerName() string {
232+
return repo.mustOwnerName(x)
233+
}
234+
226235
// FullName returns the repository full name
227236
func (repo *Repository) FullName() string {
228-
return repo.MustOwner().Name + "/" + repo.Name
237+
return repo.MustOwnerName() + "/" + repo.Name
229238
}
230239

231240
// HTMLURL returns the repository HTML URL
@@ -477,6 +486,41 @@ func (repo *Repository) mustOwner(e Engine) *User {
477486
return repo.Owner
478487
}
479488

489+
func (repo *Repository) getOwnerName(e Engine) error {
490+
if len(repo.OwnerName) > 0 {
491+
return nil
492+
}
493+
494+
if repo.Owner != nil {
495+
repo.OwnerName = repo.Owner.Name
496+
return nil
497+
}
498+
499+
u := new(User)
500+
has, err := e.ID(repo.OwnerID).Cols("name").Get(u)
501+
if err != nil {
502+
return err
503+
} else if !has {
504+
return ErrUserNotExist{repo.OwnerID, "", 0}
505+
}
506+
repo.OwnerName = u.Name
507+
return nil
508+
}
509+
510+
// GetOwnerName returns the repository owner name
511+
func (repo *Repository) GetOwnerName() error {
512+
return repo.getOwnerName(x)
513+
}
514+
515+
func (repo *Repository) mustOwnerName(e Engine) string {
516+
if err := repo.getOwnerName(e); err != nil {
517+
log.Error(4, "Error loading repository owner name: %v", err)
518+
return "error"
519+
}
520+
521+
return repo.OwnerName
522+
}
523+
480524
// ComposeMetas composes a map of metas for rendering external issue tracker URL.
481525
func (repo *Repository) ComposeMetas() map[string]string {
482526
unit, err := repo.GetUnit(UnitTypeExternalTracker)
@@ -588,7 +632,7 @@ func (repo *Repository) GetBaseRepo() (err error) {
588632
}
589633

590634
func (repo *Repository) repoPath(e Engine) string {
591-
return RepoPath(repo.mustOwner(e).Name, repo.Name)
635+
return RepoPath(repo.mustOwnerName(e), repo.Name)
592636
}
593637

594638
// RepoPath returns the repository path
@@ -2133,7 +2177,7 @@ func ReinitMissingRepositories() error {
21332177
// SyncRepositoryHooks rewrites all repositories' pre-receive, update and post-receive hooks
21342178
// to make sure the binary and custom conf path are up-to-date.
21352179
func SyncRepositoryHooks() error {
2136-
return x.Where("id > 0").Iterate(new(Repository),
2180+
return x.Cols("owner_id", "name").Where("id > 0").Iterate(new(Repository),
21372181
func(idx int, bean interface{}) error {
21382182
if err := createDelegateHooks(bean.(*Repository).RepoPath()); err != nil {
21392183
return fmt.Errorf("SyncRepositoryHook: %v", err)

models/wiki.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func WikiPath(userName, repoName string) string {
6767

6868
// WikiPath returns wiki data path for given repository.
6969
func (repo *Repository) WikiPath() string {
70-
return WikiPath(repo.MustOwner().Name, repo.Name)
70+
return WikiPath(repo.MustOwnerName(), repo.Name)
7171
}
7272

7373
// HasWiki returns true if repository has wiki.

0 commit comments

Comments
 (0)