Browse Source

repo: able to view size (#1158)

Unknwon 7 years ago
parent
commit
bb19bb601e

+ 1 - 0
conf/locale/locale_en-US.ini

@@ -1037,6 +1037,7 @@ repos.private = Private
 repos.watches = Watches
 repos.stars = Stars
 repos.issues = Issues
+repos.size = Size
 
 auths.auth_manage_panel = Authentication Manage Panel
 auths.new = Add New Source

+ 1 - 1
gogs.go

@@ -16,7 +16,7 @@ import (
 	"github.com/gogits/gogs/modules/setting"
 )
 
-const APP_VER = "0.10.15.0311"
+const APP_VER = "0.10.16.0312"
 
 func init() {
 	setting.AppVer = APP_VER

+ 2 - 0
models/migrations/migrations.go

@@ -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 - 0
models/migrations/v16.go

@@ -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
+		})
+}

+ 15 - 1
models/repo.go

@@ -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 {

+ 6 - 2
models/update.go

@@ -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)

+ 2 - 2
modules/base/tool.go

@@ -372,7 +372,7 @@ func logn(n, b float64) float64 {
 
 func humanateBytes(s uint64, base float64, sizes []string) string {
 	if s < 10 {
-		return fmt.Sprintf("%dB", s)
+		return fmt.Sprintf("%d B", s)
 	}
 	e := math.Floor(logn(float64(s), base))
 	suffix := sizes[int(e)]
@@ -382,7 +382,7 @@ func humanateBytes(s uint64, base float64, sizes []string) string {
 		f = "%.1f"
 	}
 
-	return fmt.Sprintf(f+"%s", val, suffix)
+	return fmt.Sprintf(f+" %s", val, suffix)
 }
 
 // FileSize calculates the file size and generate user-friendly string.

File diff suppressed because it is too large
+ 0 - 0
modules/bindata/bindata.go


+ 1 - 1
templates/.VERSION

@@ -1 +1 @@
-0.10.15.0311
+0.10.16.0312

+ 2 - 0
templates/admin/repo/list.tmpl

@@ -22,6 +22,7 @@
 								<th>{{.i18n.Tr "admin.repos.watches"}}</th>
 								<th>{{.i18n.Tr "admin.repos.stars"}}</th>
 								<th>{{.i18n.Tr "admin.repos.issues"}}</th>
+								<th>{{.i18n.Tr "admin.repos.size"}}</th>
 								<th>{{.i18n.Tr "admin.users.created"}}</th>
 								<th>{{.i18n.Tr "admin.notices.op"}}</th>
 							</tr>
@@ -36,6 +37,7 @@
 									<td>{{.NumWatches}}</td>
 									<td>{{.NumStars}}</td>
 									<td>{{.NumIssues}}</td>
+									<td>{{.Size | FileSize}}</td>
 									<td><span title="{{DateFmtLong .Created}}">{{DateFmtShort .Created}}</span></td>
 									<td><a class="delete-button" href="" data-url="{{$.Link}}/delete?page={{$.Page.Current}}" data-id="{{.ID}}"><i class="trash icon text red"></i></a></td>
 								</tr>

+ 1 - 1
vendor/github.com/gogits/git-module/git.go

@@ -10,7 +10,7 @@ import (
 	"time"
 )
 
-const _VERSION = "0.4.13"
+const _VERSION = "0.5.0"
 
 func Version() string {
 	return _VERSION

+ 59 - 0
vendor/github.com/gogits/git-module/repo.go

@@ -11,7 +11,10 @@ import (
 	"os"
 	"path"
 	"path/filepath"
+	"strings"
 	"time"
+
+	"github.com/Unknwon/com"
 )
 
 // Repository represents a Git repository.
@@ -219,3 +222,59 @@ func MoveFile(repoPath, oldTreeName, newTreeName string) error {
 	_, err := NewCommand("mv").AddArguments(oldTreeName, newTreeName).RunInDir(repoPath)
 	return err
 }
+
+// CountObject represents disk usage report of Git repository.
+type CountObject struct {
+	Count         int64
+	Size          int64
+	InPack        int64
+	Packs         int64
+	SizePack      int64
+	PrunePackable int64
+	Garbage       int64
+	SizeGarbage   int64
+}
+
+const (
+	_STAT_COUNT          = "count: "
+	_STAT_SIZE           = "size: "
+	_STAT_IN_PACK        = "in-pack: "
+	_STAT_PACKS          = "packs: "
+	_STAT_SIZE_PACK      = "size-pack: "
+	_STAT_PRUNE_PACKABLE = "prune-packable: "
+	_STAT_GARBAGE        = "garbage: "
+	_STAT_SIZE_GARBAGE   = "size-garbage: "
+)
+
+// GetRepoSize returns disk usage report of repository in given path.
+func GetRepoSize(repoPath string) (*CountObject, error) {
+	cmd := NewCommand("count-objects", "-v")
+	stdout, err := cmd.RunInDir(repoPath)
+	if err != nil {
+		return nil, err
+	}
+
+	countObject := new(CountObject)
+	for _, line := range strings.Split(stdout, "\n") {
+		switch {
+		case strings.HasPrefix(line, _STAT_COUNT):
+			countObject.Count = com.StrTo(line[7:]).MustInt64()
+		case strings.HasPrefix(line, _STAT_SIZE):
+			countObject.Size = com.StrTo(line[6:]).MustInt64() * 1024
+		case strings.HasPrefix(line, _STAT_IN_PACK):
+			countObject.InPack = com.StrTo(line[9:]).MustInt64() * 1024
+		case strings.HasPrefix(line, _STAT_PACKS):
+			countObject.Packs = com.StrTo(line[7:]).MustInt64()
+		case strings.HasPrefix(line, _STAT_SIZE_PACK):
+			countObject.SizePack = com.StrTo(line[11:]).MustInt64() * 1024
+		case strings.HasPrefix(line, _STAT_PRUNE_PACKABLE):
+			countObject.PrunePackable = com.StrTo(line[16:]).MustInt64()
+		case strings.HasPrefix(line, _STAT_GARBAGE):
+			countObject.Garbage = com.StrTo(line[9:]).MustInt64()
+		case strings.HasPrefix(line, _STAT_SIZE_GARBAGE):
+			countObject.SizeGarbage = com.StrTo(line[14:]).MustInt64() * 1024
+		}
+	}
+
+	return countObject, nil
+}

+ 3 - 3
vendor/vendor.json

@@ -159,10 +159,10 @@
 			"revisionTime": "2016-08-10T03:50:02Z"
 		},
 		{
-			"checksumSHA1": "sygMoTVpNmOTbsnZbgLR904p5GE=",
+			"checksumSHA1": "W2THy51ey2gNT9PbBS98GG4o3fQ=",
 			"path": "github.com/gogits/git-module",
-			"revision": "2c083b82191752340c76271876671e18130ad662",
-			"revisionTime": "2017-03-10T19:06:55Z"
+			"revision": "b6678775980f8bcd0bbf400a88c176210c8a22e6",
+			"revisionTime": "2017-03-12T06:43:33Z"
 		},
 		{
 			"checksumSHA1": "1p1/OSDPORWbSBCD791BbGh2vVc=",

Some files were not shown because too many files changed in this diff