Skip to content

Commit 5ffdf93

Browse files
authored
Fix to use only needed columns from tables to get repository git paths (#3870)
1 parent 9ec7f6b commit 5ffdf93

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

models/repo.go

Lines changed: 47 additions & 3 deletions
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"`
@@ -225,9 +226,17 @@ func (repo *Repository) MustOwner() *User {
225226
return repo.mustOwner(x)
226227
}
227228

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

233242
// HTMLURL returns the repository HTML URL
@@ -479,6 +488,41 @@ func (repo *Repository) mustOwner(e Engine) *User {
479488
return repo.Owner
480489
}
481490

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

592636
func (repo *Repository) repoPath(e Engine) string {
593-
return RepoPath(repo.mustOwner(e).Name, repo.Name)
637+
return RepoPath(repo.mustOwnerName(e), repo.Name)
594638
}
595639

596640
// RepoPath returns the repository path
@@ -2141,7 +2185,7 @@ func ReinitMissingRepositories() error {
21412185
// SyncRepositoryHooks rewrites all repositories' pre-receive, update and post-receive hooks
21422186
// to make sure the binary and custom conf path are up-to-date.
21432187
func SyncRepositoryHooks() error {
2144-
return x.Where("id > 0").Iterate(new(Repository),
2188+
return x.Cols("owner_id", "name").Where("id > 0").Iterate(new(Repository),
21452189
func(idx int, bean interface{}) error {
21462190
if err := createDelegateHooks(bean.(*Repository).RepoPath()); err != nil {
21472191
return fmt.Errorf("SyncRepositoryHook: %v", err)

models/wiki.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func UncycloPath(userName, repoName string) string {
6767

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

7373
// HasUncyclo returns true if repository has wiki.

0 commit comments

Comments
 (0)