mirror of
https://github.com/gogs/gogs.git
synced 2026-07-07 15:52:28 +02:00
repo: able to view size (#1158)
This commit is contained in:
@@ -60,6 +60,8 @@ var migrations = []Migration{
|
||||
NewMigration("set comment updated with created", setCommentUpdatedWithCreated),
|
||||
// v14 -> v15:v0.9.147
|
||||
NewMigration("generate and migrate Git hooks", generateAndMigrateGitHooks),
|
||||
// v15 -> v16:v0.10.16
|
||||
NewMigration("update repository sizes", updateRepositorySizes),
|
||||
}
|
||||
|
||||
// Migrate database to current version
|
||||
|
||||
60
models/migrations/v16.go
Normal file
60
models/migrations/v16.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright 2017 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
log "gopkg.in/clog.v1"
|
||||
|
||||
"github.com/gogits/git-module"
|
||||
|
||||
"github.com/gogits/gogs/modules/setting"
|
||||
)
|
||||
|
||||
func updateRepositorySizes(x *xorm.Engine) (err error) {
|
||||
type Repository struct {
|
||||
ID int64
|
||||
OwnerID int64
|
||||
Name string
|
||||
Size int64
|
||||
}
|
||||
type User struct {
|
||||
ID int64
|
||||
Name string
|
||||
}
|
||||
return x.Where("id > 0").Iterate(new(Repository),
|
||||
func(idx int, bean interface{}) error {
|
||||
repo := bean.(*Repository)
|
||||
if repo.Name == "." || repo.Name == ".." {
|
||||
return nil
|
||||
}
|
||||
|
||||
user := new(User)
|
||||
has, err := x.Where("id = ?", repo.OwnerID).Get(user)
|
||||
if err != nil {
|
||||
return fmt.Errorf("query owner of repository [repo_id: %d, owner_id: %d]: %v", repo.ID, repo.OwnerID, err)
|
||||
} else if !has {
|
||||
return nil
|
||||
}
|
||||
|
||||
repoPath := filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".git"
|
||||
log.Trace("[%04d]: %s", idx, repoPath)
|
||||
|
||||
countObject, err := git.GetRepoSize(repoPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetRepoSize: %v", err)
|
||||
}
|
||||
|
||||
repo.Size = countObject.Size + countObject.SizePack
|
||||
if _, err = x.Id(repo.ID).Cols("size").Update(repo); err != nil {
|
||||
return fmt.Errorf("update size: %v", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func NewRepoContext() {
|
||||
RemoveAllWithNotice("Clean up repository temporary data", filepath.Join(setting.AppDataPath, "tmp"))
|
||||
}
|
||||
|
||||
// Repository represents a git repository.
|
||||
// Repository contains information of a repository.
|
||||
type Repository struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
OwnerID int64 `xorm:"UNIQUE(s)"`
|
||||
@@ -150,6 +150,7 @@ type Repository struct {
|
||||
Description string
|
||||
Website string
|
||||
DefaultBranch string
|
||||
Size int64 `xorm:"NOT NULL DEFAULT 0"`
|
||||
|
||||
NumWatches int
|
||||
NumStars int
|
||||
@@ -292,6 +293,19 @@ func (repo *Repository) mustOwner(e Engine) *User {
|
||||
return repo.Owner
|
||||
}
|
||||
|
||||
func (repo *Repository) UpdateSize() error {
|
||||
countObject, err := git.GetRepoSize(repo.RepoPath())
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetRepoSize: %v", err)
|
||||
}
|
||||
|
||||
repo.Size = countObject.Size + countObject.SizePack
|
||||
if _, err = x.Id(repo.ID).Cols("size").Update(repo); err != nil {
|
||||
return fmt.Errorf("update size: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ComposeMetas composes a map of metas for rendering external issue tracker URL.
|
||||
func (repo *Repository) ComposeMetas() map[string]string {
|
||||
if !repo.EnableExternalTracker {
|
||||
|
||||
@@ -85,7 +85,11 @@ func PushUpdate(opts PushUpdateOptions) (err error) {
|
||||
return fmt.Errorf("GetRepositoryByName: %v", err)
|
||||
}
|
||||
|
||||
// Push tags.
|
||||
if err = repo.UpdateSize(); err != nil {
|
||||
return fmt.Errorf("UpdateSize: %v", err)
|
||||
}
|
||||
|
||||
// Push tags
|
||||
if strings.HasPrefix(opts.RefFullName, git.TAG_PREFIX) {
|
||||
if err := CommitRepoAction(CommitRepoActionOptions{
|
||||
PusherName: opts.PusherName,
|
||||
@@ -104,7 +108,7 @@ func PushUpdate(opts PushUpdateOptions) (err error) {
|
||||
var l *list.List
|
||||
// Skip read parent commits when delete branch
|
||||
if !isDelRef {
|
||||
// Push new branch.
|
||||
// Push new branch
|
||||
newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetCommit [commit_id: %s]: %v", opts.NewCommitID, err)
|
||||
|
||||
Reference in New Issue
Block a user