Browse Source

auto increase cnt via git-upload-pack hook

tags/v0.1.8
palytoxin 4 years ago
parent
commit
b9a5eee255
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

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


+ 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 {
repo.OwnerName = ownerName
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))

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

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


Loading…
Cancel
Save