diff --git a/models/repo.go b/models/repo.go index 166097e44..9095bbda8 100755 --- a/models/repo.go +++ b/models/repo.go @@ -6,12 +6,14 @@ package models import ( - "code.gitea.io/gitea/modules/blockchain" "context" "crypto/md5" "errors" "fmt" "html/template" + "sync" + + "code.gitea.io/gitea/modules/blockchain" // Needed for jpeg support _ "image/jpeg" @@ -208,6 +210,9 @@ type Repository struct { Balance int64 `xorm:"NOT NULL DEFAULT 0"` BlockChainStatus RepoBlockChainStatus `xorm:"NOT NULL DEFAULT 0"` + // git clone total count + CloneCnt int64 `xorm:"NOT NULL DEFAULT 0"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } @@ -2397,6 +2402,15 @@ func (repo *Repository) GetTreePathLock(treePath string) (*LFSLock, error) { return nil, nil } +var lck sync.Mutex + +func (repo *Repository) IncreaseCloneCnt() { + lck.Lock() + defer lck.Unlock() + repo.CloneCnt++ + _ = UpdateRepositoryCols(repo, "clone_cnt") +} + func updateRepositoryCols(e Engine, repo *Repository, cols ...string) error { _, err := e.ID(repo.ID).Cols(cols...).Update(repo) return err diff --git a/routers/private/serv.go b/routers/private/serv.go index d5b5fcc8f..f405d1d75 100644 --- a/routers/private/serv.go +++ b/routers/private/serv.go @@ -126,6 +126,12 @@ func ServCommand(ctx *macaron.Context) { } } + for _, verb := range ctx.QueryStrings("verb") { // clone_cnt + if verb == "git-upload-pack" { + go repo.IncreaseCloneCnt() + } + } + if repoExist { repo.OwnerName = ownerName results.RepoID = repo.ID diff --git a/routers/repo/http.go b/routers/repo/http.go index 650642a58..ed6276466 100644 --- a/routers/repo/http.go +++ b/routers/repo/http.go @@ -313,6 +313,10 @@ func HTTP(ctx *context.Context) { environ = append(environ, models.ProtectedBranchRepoID+fmt.Sprintf("=%d", repo.ID)) + if service == "git-upload-pack" { // clone_cnt + go repo.IncreaseCloneCnt() + } + w := ctx.Resp r := ctx.Req.Request cfg := &serviceConfig{