Skip to content

Feature size repo in admin panel #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,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
Expand Down
146 changes: 146 additions & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
package models

import (
"bufio"
"bytes"
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -195,6 +198,7 @@ type Repository struct {
IsFork bool `xorm:"NOT NULL DEFAULT false"`
ForkID int64
BaseRepo *Repository `xorm:"-"`
RepoSize int64 `xorm:"NOT NULL DEFAULT 0"`

Created time.Time `xorm:"-"`
CreatedUnix int64
Expand Down Expand Up @@ -428,6 +432,19 @@ func (repo *Repository) IsOwnedBy(userID int64) bool {
return repo.OwnerID == userID
}

func (repo *Repository) GetRepoSize() int64 {
return repo.RepoSize
}

func (repo *Repository) ComputeRepoSize() (err error) {
repoInfoSize, err := GitRepoSize(repo.RepoPath())
if err != nil {
return err
}
repo.RepoSize = repoInfoSize.Size + repoInfoSize.SizePack
return nil
}

// CanBeForked returns true if repository meets the requirements of being forked.
func (repo *Repository) CanBeForked() bool {
return !repo.IsBare
Expand Down Expand Up @@ -675,6 +692,11 @@ func MigrateRepository(u *User, opts MigrateRepoOptions) (*Repository, error) {
}
}

err = repo.ComputeRepoSize()
if err != nil {
return repo, fmt.Errorf("ComputeRepoSize: %v", err)
}

if opts.IsMirror {
if _, err = x.InsertOne(&Mirror{
RepoID: repo.ID,
Expand Down Expand Up @@ -1268,6 +1290,11 @@ func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err e
return fmt.Errorf("updateRepository[%d]: %v", forkRepos[i].ID, err)
}
}

err = repo.ComputeRepoSize()
if err != nil {
return fmt.Errorf("ComputeRepoSize: %v", err)
}
}

return nil
Expand Down Expand Up @@ -1666,6 +1693,125 @@ func GitGcRepos() error {
})
}

// Maybe move this to gogits/git-module
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make a separate issue for that (when this is merged) instead of adding useless comments in the source-code 😉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copy/paste the comments because in the same file there is also another block to move.
Without comments and depending on the time for the PR to get merge, I will probably forget this (because I haven't only one PR)

type CountObject struct {
Count int
Size int64
InPack int64
Packs int
SizePack int64
PrunePack int
Garbage int
SizeGarbage int
}

const STAT_COUNT = "count: "
const STAT_SIZE = "size: "
const STAT_INPACK = "in-pack: "
const STAT_PACKS = "packs: "
const STAT_SIZEPACK = "size-pack: "
const STAT_PRUNEPACKAGE = "prune-package: "
const STAT_GARBAGE = "garbage: "
const STAT_SIZEGARBAGE = "size-garbage: "

func GitRepoSize(repoPath string) (*CountObject, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should be on the git module, not here

var cmd *exec.Cmd
cmd = exec.Command("git", "count-objects", "-v")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the flag --human-readable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should really in git repository.

cmd.Dir = repoPath
cmd.Stderr = os.Stderr

stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("StdoutPipe: %v", err)
}
if err = cmd.Start(); err != nil {
return nil, fmt.Errorf("Start: %v", err)
}

repoSize, err := ParseSize(stdout)
if err != nil {
return nil, fmt.Errorf("ParsePatch: %v", err)
}

if err = cmd.Wait(); err != nil {
return nil, fmt.Errorf("Wait: %v", err)
}

return repoSize, nil
}

func ParseSize(reader io.Reader) (*CountObject, error) {
repoSize := &CountObject{}
input := bufio.NewReader(reader)
isEOF := false

for !isEOF {
line, err := input.ReadString('\n')
if err != nil {
if err == io.EOF {
isEOF = true
continue
} else {
return nil, fmt.Errorf("ReadString: %v", err)
}
}

if len(line) > 0 && line[len(line)-1] == '\n' {
// Remove line break.
line = line[:len(line)-1]
}

switch {
case strings.HasPrefix(line, STAT_COUNT):
repoSize.Count, err = strconv.Atoi(line[7:])
case strings.HasPrefix(line, STAT_SIZE):
repoSize.Size, err = strconv.ParseInt(line[6:], 10, 64)
repoSize.Size = repoSize.Size * 1024
case strings.HasPrefix(line, STAT_INPACK):
repoSize.InPack, err = strconv.ParseInt(line[9:], 10, 64)
repoSize.InPack = repoSize.InPack * 1024
case strings.HasPrefix(line, STAT_PACKS):
repoSize.Packs, err = strconv.Atoi(line[7:])
case strings.HasPrefix(line, STAT_SIZEPACK):
repoSize.SizePack, err = strconv.ParseInt(line[11:], 10, 64)
repoSize.SizePack = repoSize.SizePack * 1024
case strings.HasPrefix(line, STAT_PRUNEPACKAGE):
repoSize.PrunePack, err = strconv.Atoi(line[16:])
case strings.HasPrefix(line, STAT_GARBAGE):
repoSize.Garbage, err = strconv.Atoi(line[9:])
case strings.HasPrefix(line, STAT_SIZEGARBAGE):
repoSize.SizeGarbage, err = strconv.Atoi(line[14:])
}
if err != nil {
log.Error(4, "Parsing count-objects failed: %v", err)
return nil, err
}
}
return repoSize, nil
}

func UpdateRepoSize(repoUserName, repoName string) error {
log.Info("Compute Size of %s - %s", repoUserName, repoName)

repoPath := RepoPath(repoUserName, repoName)
repoInfoSize, err := GitRepoSize(repoPath)
if err != nil {
return err
}
repoSize := repoInfoSize.Size + repoInfoSize.SizePack
log.Info(strconv.FormatInt(repoSize, 10))
sess := x.NewSession()
defer sessionRelease(sess)
if err = sess.Begin(); err != nil {
return nil
}
if _, err := sess.Where("name=?", repoName).Update(&Repository{RepoSize: repoSize}); err != nil {
return fmt.Errorf("Update repo size failed: %v", err)
}

return sess.Commit()
}

type repoChecker struct {
querySQL, correctSQL string
desc string
Expand Down
6 changes: 6 additions & 0 deletions models/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,11 @@ func PushUpdate(opts PushUpdateOptions) (err error) {
}); err != nil {
return fmt.Errorf("CommitRepoAction (branch): %v", err)
}

// Compute repo size
if err := UpdateRepoSize(opts.RepoUserName, opts.RepoName); err != nil {
return fmt.Errorf("Update repo size failed: %v", err)
}

return nil
}
3 changes: 3 additions & 0 deletions modules/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func NewFuncMap() []template.FuncMap {
"DateFmtShort": func(t time.Time) string {
return t.Format("Jan 02, 2006")
},
"SizeFmt": func(s int64) string {
return base.FileSize(s)
},
"List": List,
"SubStr": func(str string, start, length int) string {
if len(str) == 0 {
Expand Down
2 changes: 2 additions & 0 deletions templates/admin/repo/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -36,6 +37,7 @@
<td>{{.NumWatches}}</td>
<td>{{.NumStars}}</td>
<td>{{.NumIssues}}</td>
<td>{{SizeFmt .RepoSize}}</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>
Expand Down