Browse Source

auto increase cnt via git-upload-pack hook

tags/v1.21.12.1
palytoxin 4 years ago
parent
commit
23e05b4b05
3 changed files with 25 additions and 1 deletions
  1. +15
    -1
      models/repo.go
  2. +6
    -0
      routers/private/serv.go
  3. +4
    -0
      routers/repo/http.go

+ 15
- 1
models/repo.go View File

@@ -6,12 +6,14 @@
package models package models


import ( import (
"code.gitea.io/gitea/modules/blockchain"
"context" "context"
"crypto/md5" "crypto/md5"
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
"sync"

"code.gitea.io/gitea/modules/blockchain"


// Needed for jpeg support // Needed for jpeg support
_ "image/jpeg" _ "image/jpeg"
@@ -208,6 +210,9 @@ type Repository struct {
Balance int64 `xorm:"NOT NULL DEFAULT 0"` Balance int64 `xorm:"NOT NULL DEFAULT 0"`
BlockChainStatus RepoBlockChainStatus `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"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
} }
@@ -2397,6 +2402,15 @@ func (repo *Repository) GetTreePathLock(treePath string) (*LFSLock, error) {
return nil, nil 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 { func updateRepositoryCols(e Engine, repo *Repository, cols ...string) error {
_, err := e.ID(repo.ID).Cols(cols...).Update(repo) _, err := e.ID(repo.ID).Cols(cols...).Update(repo)
return err return err


+ 6
- 0
routers/private/serv.go View File

@@ -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 { if repoExist {
repo.OwnerName = ownerName repo.OwnerName = ownerName
results.RepoID = repo.ID results.RepoID = repo.ID


+ 4
- 0
routers/repo/http.go View File

@@ -313,6 +313,10 @@ func HTTP(ctx *context.Context) {


environ = append(environ, models.ProtectedBranchRepoID+fmt.Sprintf("=%d", repo.ID)) environ = append(environ, models.ProtectedBranchRepoID+fmt.Sprintf("=%d", repo.ID))


if service == "git-upload-pack" { // clone_cnt
go repo.IncreaseCloneCnt()
}

w := ctx.Resp w := ctx.Resp
r := ctx.Req.Request r := ctx.Req.Request
cfg := &serviceConfig{ cfg := &serviceConfig{


Loading…
Cancel
Save