Skip to content

Commit b9b22b4

Browse files
authored
Merge pull request #295 from Bwko/Lint/repo_
Lint models/repo
2 parents 8704f48 + bad1bc6 commit b9b22b4

File tree

3 files changed

+86
-17
lines changed

3 files changed

+86
-17
lines changed

models/repo.go

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,40 @@ const (
4141
var repoWorkingPool = sync.NewExclusivePool()
4242

4343
var (
44-
ErrRepoFileNotExist = errors.New("Repository file does not exist")
44+
// ErrRepoFileNotExist repository file does not exist error
45+
ErrRepoFileNotExist = errors.New("Repository file does not exist")
46+
47+
// ErrRepoFileNotLoaded repository file not loaded error
4548
ErrRepoFileNotLoaded = errors.New("Repository file not loaded")
46-
ErrMirrorNotExist = errors.New("Mirror does not exist")
47-
ErrInvalidReference = errors.New("Invalid reference specified")
48-
ErrNameEmpty = errors.New("Name is empty")
49+
50+
// ErrMirrorNotExist mirror does not exist error
51+
ErrMirrorNotExist = errors.New("Mirror does not exist")
52+
53+
// ErrInvalidReference invalid reference specified error
54+
ErrInvalidReference = errors.New("Invalid reference specified")
55+
56+
// ErrNameEmpty name is empty error
57+
ErrNameEmpty = errors.New("Name is empty")
4958
)
5059

5160
var (
52-
Gitignores, Licenses, Readmes, LabelTemplates []string
61+
// Gitignores contains the gitiginore files
62+
Gitignores []string
63+
64+
// Licenses contains the license files
65+
Licenses []string
66+
67+
// Readmes contains the readme files
68+
Readmes []string
69+
70+
// LabelTemplates contains the label template files
71+
LabelTemplates []string
5372

54-
// Maximum items per page in forks, watchers and stars of a repo
73+
// ItemsPerPage maximum items per page in forks, watchers and stars of a repo
5574
ItemsPerPage = 40
5675
)
5776

77+
// LoadRepoConfig loads the repository config
5878
func LoadRepoConfig() {
5979
// Load .gitignore and license files and readme templates.
6080
types := []string{"gitignore", "license", "readme", "label"}
@@ -104,6 +124,7 @@ func LoadRepoConfig() {
104124
Licenses = sortedLicenses
105125
}
106126

127+
// NewRepoContext creates a new repository context
107128
func NewRepoContext() {
108129
zip.Verbose = false
109130

@@ -200,15 +221,18 @@ type Repository struct {
200221
UpdatedUnix int64
201222
}
202223

224+
// BeforeInsert is invoked from XORM before inserting an object of this type.
203225
func (repo *Repository) BeforeInsert() {
204226
repo.CreatedUnix = time.Now().Unix()
205227
repo.UpdatedUnix = repo.CreatedUnix
206228
}
207229

230+
// BeforeUpdate is invoked from XORM before updating this object.
208231
func (repo *Repository) BeforeUpdate() {
209232
repo.UpdatedUnix = time.Now().Unix()
210233
}
211234

235+
// AfterSet is invoked from XORM after setting the value of a field of this object.
212236
func (repo *Repository) AfterSet(colName string, _ xorm.Cell) {
213237
switch colName {
214238
case "default_branch":
@@ -241,14 +265,17 @@ func (repo *Repository) MustOwner() *User {
241265
return repo.mustOwner(x)
242266
}
243267

268+
// FullName returns the repository full name
244269
func (repo *Repository) FullName() string {
245270
return repo.MustOwner().Name + "/" + repo.Name
246271
}
247272

273+
// HTMLURL returns the repository HTML URL
248274
func (repo *Repository) HTMLURL() string {
249275
return setting.AppURL + repo.FullName()
250276
}
251277

278+
// APIFormat converts a Repository to api.Repository
252279
// Arguments that are allowed to be nil: permission
253280
func (repo *Repository) APIFormat(permission *api.Permission) *api.Repository {
254281
cloneLink := repo.CloneLink()
@@ -284,6 +311,7 @@ func (repo *Repository) getOwner(e Engine) (err error) {
284311
return err
285312
}
286313

314+
// GetOwner returns the repository owner
287315
func (repo *Repository) GetOwner() error {
288316
return repo.getOwner(x)
289317
}
@@ -381,11 +409,13 @@ func (repo *Repository) IssueStats(uid int64, filterMode int, isPull bool) (int6
381409
return GetRepoIssueStats(repo.ID, uid, filterMode, isPull)
382410
}
383411

412+
// GetMirror sets the repository mirror, returns an error upon failure
384413
func (repo *Repository) GetMirror() (err error) {
385414
repo.Mirror, err = GetMirrorByRepoID(repo.ID)
386415
return err
387416
}
388417

418+
// GetBaseRepo returns the base repository
389419
func (repo *Repository) GetBaseRepo() (err error) {
390420
if !repo.IsFork {
391421
return nil
@@ -399,31 +429,38 @@ func (repo *Repository) repoPath(e Engine) string {
399429
return RepoPath(repo.mustOwner(e).Name, repo.Name)
400430
}
401431

432+
// RepoPath returns the repository path
402433
func (repo *Repository) RepoPath() string {
403434
return repo.repoPath(x)
404435
}
405436

437+
// GitConfigPath returns the repository git config path
406438
func (repo *Repository) GitConfigPath() string {
407439
return filepath.Join(repo.RepoPath(), "config")
408440
}
409441

442+
// RelLink returns the repository relative link
410443
func (repo *Repository) RelLink() string {
411444
return "/" + repo.FullName()
412445
}
413446

447+
// Link returns the repository link
414448
func (repo *Repository) Link() string {
415449
return setting.AppSubURL + "/" + repo.FullName()
416450
}
417451

452+
// ComposeCompareURL returns the repository comparison URL
418453
func (repo *Repository) ComposeCompareURL(oldCommitID, newCommitID string) string {
419454
return fmt.Sprintf("%s/%s/compare/%s...%s", repo.MustOwner().Name, repo.Name, oldCommitID, newCommitID)
420455
}
421456

457+
// HasAccess returns true when user has access to this repository
422458
func (repo *Repository) HasAccess(u *User) bool {
423459
has, _ := HasAccess(u, repo, AccessModeRead)
424460
return has
425461
}
426462

463+
// IsOwnedBy returns true when user owns this repository
427464
func (repo *Repository) IsOwnedBy(userID int64) bool {
428465
return repo.OwnerID == userID
429466
}
@@ -438,7 +475,7 @@ func (repo *Repository) CanEnablePulls() bool {
438475
return !repo.IsMirror
439476
}
440477

441-
// AllowPulls returns true if repository meets the requirements of accepting pulls and has them enabled.
478+
// AllowsPulls returns true if repository meets the requirements of accepting pulls and has them enabled.
442479
func (repo *Repository) AllowsPulls() bool {
443480
return repo.CanEnablePulls() && repo.EnablePulls
444481
}
@@ -448,29 +485,31 @@ func (repo *Repository) CanEnableEditor() bool {
448485
return !repo.IsMirror
449486
}
450487

488+
// NextIssueIndex returns the next issue index
451489
// FIXME: should have a mutex to prevent producing same index for two issues that are created
452490
// closely enough.
453491
func (repo *Repository) NextIssueIndex() int64 {
454492
return int64(repo.NumIssues+repo.NumPulls) + 1
455493
}
456494

457495
var (
458-
DescPattern = regexp.MustCompile(`https?://\S+`)
496+
descPattern = regexp.MustCompile(`https?://\S+`)
459497
)
460498

461-
// DescriptionHtml does special handles to description and return HTML string.
462-
func (repo *Repository) DescriptionHtml() template.HTML {
499+
// DescriptionHTML does special handles to description and return HTML string.
500+
func (repo *Repository) DescriptionHTML() template.HTML {
463501
sanitize := func(s string) string {
464502
return fmt.Sprintf(`<a href="%[1]s" target="_blank">%[1]s</a>`, s)
465503
}
466-
return template.HTML(DescPattern.ReplaceAllStringFunc(markdown.Sanitizer.Sanitize(repo.Description), sanitize))
504+
return template.HTML(descPattern.ReplaceAllStringFunc(markdown.Sanitizer.Sanitize(repo.Description), sanitize))
467505
}
468506

507+
// LocalCopyPath returns the local repository copy path
469508
func (repo *Repository) LocalCopyPath() string {
470509
return path.Join(setting.AppDataPath, "tmp/local-rpeo", com.ToStr(repo.ID))
471510
}
472511

473-
// UpdateLocalCopy pulls latest changes of given branch from repoPath to localPath.
512+
// UpdateLocalCopyBranch pulls latest changes of given branch from repoPath to localPath.
474513
// It creates a new clone if local copy does not exist.
475514
// This function checks out target branch by default, it is safe to assume subsequent
476515
// operations are operating against target branch when caller has confidence for no race condition.
@@ -575,6 +614,7 @@ func (repo *Repository) CloneLink() (cl *CloneLink) {
575614
return repo.cloneLink(false)
576615
}
577616

617+
// MigrateRepoOptions contains the repository migrate options
578618
type MigrateRepoOptions struct {
579619
Name string
580620
Description string
@@ -711,7 +751,7 @@ func createUpdateHook(repoPath string) error {
711751
fmt.Sprintf(tplUpdateHook, setting.ScriptType, "\""+setting.AppPath+"\"", setting.CustomConf))
712752
}
713753

714-
// Finish migrating repository and/or wiki with things that don't need to be done for mirrors.
754+
// CleanUpMigrateInfo finishes migrating repository and/or wiki with things that don't need to be done for mirrors.
715755
func CleanUpMigrateInfo(repo *Repository) (*Repository, error) {
716756
repoPath := repo.RepoPath()
717757
if err := createUpdateHook(repoPath); err != nil {
@@ -759,6 +799,7 @@ func initRepoCommit(tmpPath string, sig *git.Signature) (err error) {
759799
return nil
760800
}
761801

802+
// CreateRepoOptions contains the create repository options
762803
type CreateRepoOptions struct {
763804
Name string
764805
Description string
@@ -897,6 +938,7 @@ var (
897938
reservedRepoPatterns = []string{"*.git", "*.wiki"}
898939
)
899940

941+
// IsUsableRepoName returns true when repository is usable
900942
func IsUsableRepoName(name string) error {
901943
return isUsableName(reservedRepoNames, reservedRepoPatterns, name)
902944
}
@@ -1030,6 +1072,7 @@ func CountUserRepositories(userID int64, private bool) int64 {
10301072
return countRepositories(userID, private)
10311073
}
10321074

1075+
// Repositories returns all repositories
10331076
func Repositories(page, pageSize int) (_ []*Repository, err error) {
10341077
repos := make([]*Repository, 0, pageSize)
10351078
return repos, x.Limit(pageSize, (page-1)*pageSize).Asc("id").Find(&repos)
@@ -1275,6 +1318,7 @@ func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err e
12751318
return nil
12761319
}
12771320

1321+
// UpdateRepository updates a repository
12781322
func UpdateRepository(repo *Repository, visibilityChanged bool) (err error) {
12791323
sess := x.NewSession()
12801324
defer sessionRelease(sess)
@@ -1474,7 +1518,7 @@ func GetUserRepositories(userID int64, private bool, page, pageSize int) ([]*Rep
14741518
return repos, sess.Find(&repos)
14751519
}
14761520

1477-
// GetUserRepositories returns a list of mirror repositories of given user.
1521+
// GetUserMirrorRepositories returns a list of mirror repositories of given user.
14781522
func GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
14791523
repos := make([]*Repository, 0, 10)
14801524
return repos, x.
@@ -1502,6 +1546,7 @@ func GetRepositoryCount(u *User) (int64, error) {
15021546
return getRepositoryCount(x, u)
15031547
}
15041548

1549+
// SearchRepoOptions holds the search options
15051550
type SearchRepoOptions struct {
15061551
Keyword string
15071552
OwnerID int64
@@ -1670,6 +1715,7 @@ func GitFsck() {
16701715
}
16711716
}
16721717

1718+
// GitGcRepos calls 'git gc' to remove unnecessary files and optimize the local repository
16731719
func GitGcRepos() error {
16741720
args := append([]string{"gc"}, setting.Git.GCArgs...)
16751721
return x.
@@ -1712,6 +1758,7 @@ func repoStatsCheck(checker *repoChecker) {
17121758
}
17131759
}
17141760

1761+
// CheckRepoStats checks the repository stats
17151762
func CheckRepoStats() {
17161763
if taskStatusTable.IsRunning(checkRepos) {
17171764
return
@@ -1806,6 +1853,7 @@ func CheckRepoStats() {
18061853
// ***** END: Repository.NumForks *****
18071854
}
18081855

1856+
// RepositoryList contains a list of repositories
18091857
type RepositoryList []*Repository
18101858

18111859
func (repos RepositoryList) loadAttributes(e Engine) error {
@@ -1838,10 +1886,12 @@ func (repos RepositoryList) loadAttributes(e Engine) error {
18381886
return nil
18391887
}
18401888

1889+
// LoadAttributes loads the attributes for the given RepositoryList
18411890
func (repos RepositoryList) LoadAttributes() error {
18421891
return repos.loadAttributes(x)
18431892
}
18441893

1894+
// MirrorRepositoryList contains the mirror repositories
18451895
type MirrorRepositoryList []*Repository
18461896

18471897
func (repos MirrorRepositoryList) loadAttributes(e Engine) error {
@@ -1876,6 +1926,7 @@ func (repos MirrorRepositoryList) loadAttributes(e Engine) error {
18761926
return nil
18771927
}
18781928

1929+
// LoadAttributes loads the attributes for the given MirrorRepositoryList
18791930
func (repos MirrorRepositoryList) LoadAttributes() error {
18801931
return repos.loadAttributes(x)
18811932
}
@@ -1925,7 +1976,7 @@ func watchRepo(e Engine, userID, repoID int64, watch bool) (err error) {
19251976
return err
19261977
}
19271978

1928-
// Watch or unwatch repository.
1979+
// WatchRepo watch or unwatch repository.
19291980
func WatchRepo(userID, repoID int64, watch bool) (err error) {
19301981
return watchRepo(x, userID, repoID, watch)
19311982
}
@@ -1940,7 +1991,7 @@ func GetWatchers(repoID int64) ([]*Watch, error) {
19401991
return getWatchers(x, repoID)
19411992
}
19421993

1943-
// Repository.GetWatchers returns range of users watching given repository.
1994+
// GetWatchers returns range of users watching given repository.
19441995
func (repo *Repository) GetWatchers(page int) ([]*User, error) {
19451996
users := make([]*User, 0, ItemsPerPage)
19461997
sess := x.
@@ -1993,13 +2044,14 @@ func NotifyWatchers(act *Action) error {
19932044
// /_______ /|__| (____ /__|
19942045
// \/ \/
19952046

2047+
// Star contains the star information
19962048
type Star struct {
19972049
ID int64 `xorm:"pk autoincr"`
19982050
UID int64 `xorm:"UNIQUE(s)"`
19992051
RepoID int64 `xorm:"UNIQUE(s)"`
20002052
}
20012053

2002-
// Star or unstar repository.
2054+
// StarRepo star or unstar repository.
20032055
func StarRepo(userID, repoID int64, star bool) (err error) {
20042056
if star {
20052057
if IsStaring(userID, repoID) {
@@ -2031,6 +2083,7 @@ func IsStaring(userID, repoID int64) bool {
20312083
return has
20322084
}
20332085

2086+
// GetStargazers returns the users who gave stars to this repository
20342087
func (repo *Repository) GetStargazers(page int) ([]*User, error) {
20352088
users := make([]*User, 0, ItemsPerPage)
20362089
sess := x.
@@ -2060,6 +2113,7 @@ func HasForkedRepo(ownerID, repoID int64) (*Repository, bool) {
20602113
return repo, has
20612114
}
20622115

2116+
// ForkRepository forks a repository
20632117
func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Repository, err error) {
20642118
repo := &Repository{
20652119
OwnerID: u.ID,
@@ -2109,6 +2163,7 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Reposit
21092163
return repo, sess.Commit()
21102164
}
21112165

2166+
// GetForks returns all the forks of the repository
21122167
func (repo *Repository) GetForks() ([]*Repository, error) {
21132168
forks := make([]*Repository, 0, repo.NumForks)
21142169
return forks, x.Find(&forks, &Repository{ForkID: repo.ID})
@@ -2122,6 +2177,7 @@ func (repo *Repository) GetForks() ([]*Repository, error) {
21222177
// \/ \/ \/ \/ \/
21232178
//
21242179

2180+
// CreateNewBranch creates a new repository branch
21252181
func (repo *Repository) CreateNewBranch(doer *User, oldBranchName, branchName string) (err error) {
21262182
repoWorkingPool.CheckIn(com.ToStr(repo.ID))
21272183
defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))

models/repo_collaboration.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Collaboration struct {
1616
Mode AccessMode `xorm:"DEFAULT 2 NOT NULL"`
1717
}
1818

19+
// ModeI18nKey returns the collaboration mode I18n Key
1920
func (c *Collaboration) ModeI18nKey() string {
2021
switch c.Mode {
2122
case AccessModeRead:

0 commit comments

Comments
 (0)