@@ -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"`
@@ -225,9 +226,17 @@ func (repo *Repository) MustOwner() *User {
225
226
return repo .mustOwner (x )
226
227
}
227
228
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
+
228
237
// FullName returns the repository full name
229
238
func (repo * Repository ) FullName () string {
230
- return repo .MustOwner (). Name + "/" + repo .Name
239
+ return repo .MustOwnerName () + "/" + repo .Name
231
240
}
232
241
233
242
// HTMLURL returns the repository HTML URL
@@ -479,6 +488,41 @@ func (repo *Repository) mustOwner(e Engine) *User {
479
488
return repo .Owner
480
489
}
481
490
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
+
482
526
// ComposeMetas composes a map of metas for rendering external issue tracker URL.
483
527
func (repo * Repository ) ComposeMetas () map [string ]string {
484
528
unit , err := repo .GetUnit (UnitTypeExternalTracker )
@@ -590,7 +634,7 @@ func (repo *Repository) GetBaseRepo() (err error) {
590
634
}
591
635
592
636
func (repo * Repository ) repoPath (e Engine ) string {
593
- return RepoPath (repo .mustOwner (e ). Name , repo .Name )
637
+ return RepoPath (repo .mustOwnerName (e ), repo .Name )
594
638
}
595
639
596
640
// RepoPath returns the repository path
@@ -2141,7 +2185,7 @@ func ReinitMissingRepositories() error {
2141
2185
// SyncRepositoryHooks rewrites all repositories' pre-receive, update and post-receive hooks
2142
2186
// to make sure the binary and custom conf path are up-to-date.
2143
2187
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 ),
2145
2189
func (idx int , bean interface {}) error {
2146
2190
if err := createDelegateHooks (bean .(* Repository ).RepoPath ()); err != nil {
2147
2191
return fmt .Errorf ("SyncRepositoryHook: %v" , err )
0 commit comments