@@ -163,6 +163,7 @@ func NewRepoContext() {
163
163
type Repository struct {
164
164
ID int64 `xorm:"pk autoincr"`
165
165
OwnerID int64 `xorm:"UNIQUE(s)"`
166
+ OwnerName string `xorm:"-"`
166
167
Owner * User `xorm:"-"`
167
168
LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
168
169
Name string `xorm:"INDEX NOT NULL"`
@@ -223,9 +224,17 @@ func (repo *Repository) MustOwner() *User {
223
224
return repo .mustOwner (x )
224
225
}
225
226
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
+
226
235
// FullName returns the repository full name
227
236
func (repo * Repository ) FullName () string {
228
- return repo .MustOwner (). Name + "/" + repo .Name
237
+ return repo .MustOwnerName () + "/" + repo .Name
229
238
}
230
239
231
240
// HTMLURL returns the repository HTML URL
@@ -477,6 +486,41 @@ func (repo *Repository) mustOwner(e Engine) *User {
477
486
return repo .Owner
478
487
}
479
488
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
+
480
524
// ComposeMetas composes a map of metas for rendering external issue tracker URL.
481
525
func (repo * Repository ) ComposeMetas () map [string ]string {
482
526
unit , err := repo .GetUnit (UnitTypeExternalTracker )
@@ -588,7 +632,7 @@ func (repo *Repository) GetBaseRepo() (err error) {
588
632
}
589
633
590
634
func (repo * Repository ) repoPath (e Engine ) string {
591
- return RepoPath (repo .mustOwner (e ). Name , repo .Name )
635
+ return RepoPath (repo .mustOwnerName (e ), repo .Name )
592
636
}
593
637
594
638
// RepoPath returns the repository path
@@ -2133,7 +2177,7 @@ func ReinitMissingRepositories() error {
2133
2177
// SyncRepositoryHooks rewrites all repositories' pre-receive, update and post-receive hooks
2134
2178
// to make sure the binary and custom conf path are up-to-date.
2135
2179
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 ),
2137
2181
func (idx int , bean interface {}) error {
2138
2182
if err := createDelegateHooks (bean .(* Repository ).RepoPath ()); err != nil {
2139
2183
return fmt .Errorf ("SyncRepositoryHook: %v" , err )
0 commit comments