Welcome to mirror list, hosted at ThFree Co, Russian Federation.

profile.go « stats « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b81feb475799c03c7161ffbb721b8b575f8db261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package stats

import (
	"context"
	"errors"
	"io/ioutil"
	"os"
	"path/filepath"
	"strconv"
	"time"

	"gitlab.com/gitlab-org/gitaly/internal/git"
	"gitlab.com/gitlab-org/gitaly/internal/git/repository"
)

// HasBitmap returns whether or not the repository contains an object bitmap.
func HasBitmap(repoPath string) (bool, error) {
	hasBitmap, err := hasBitmap(repoPath)
	if err != nil {
		return false, err
	}
	return hasBitmap, nil
}

// PackfilesCount returns the number of packfiles a repository has.
func PackfilesCount(repoPath string) (int, error) {
	packFiles, err := GetPackfiles(repoPath)
	if err != nil {
		return 0, err
	}

	return len(packFiles), nil
}

// GetPackfiles returns the FileInfo of packfiles inside a repository.
func GetPackfiles(repoPath string) ([]os.FileInfo, error) {
	files, err := ioutil.ReadDir(filepath.Join(repoPath, "objects/pack/"))
	if err != nil {
		return nil, err
	}

	var packFiles []os.FileInfo
	for _, f := range files {
		if filepath.Ext(f.Name()) == ".pack" {
			packFiles = append(packFiles, f)
		}
	}

	return packFiles, nil
}

// UnpackedObjects returns the number of loose objects that have a timestamp later than the newest
// packfile.
func UnpackedObjects(repoPath string) (int64, error) {
	unpackedObjects, err := getUnpackedObjects(repoPath)
	if err != nil {
		return 0, err
	}

	return unpackedObjects, nil
}

// LooseObjects returns the number of loose objects that are not in a packfile.
func LooseObjects(ctx context.Context, gitCmdFactory git.CommandFactory, repository repository.GitRepo) (int64, error) {
	cmd, err := gitCmdFactory.New(ctx, repository, git.SubCmd{Name: "count-objects", Flags: []git.Option{git.Flag{Name: "--verbose"}}})
	if err != nil {
		return 0, err
	}

	objectStats, err := readObjectInfoStatistic(cmd)
	if err != nil {
		return 0, err
	}

	count, ok := objectStats["count"].(int64)
	if !ok {
		return 0, errors.New("could not get object count")
	}

	return count, nil
}

func hasBitmap(repoPath string) (bool, error) {
	bitmaps, err := filepath.Glob(filepath.Join(repoPath, "objects", "pack", "*.bitmap"))
	if err != nil {
		return false, err
	}

	return len(bitmaps) > 0, nil
}

func getUnpackedObjects(repoPath string) (int64, error) {
	objectDir := filepath.Join(repoPath, "objects")

	packFiles, err := filepath.Glob(filepath.Join(objectDir, "pack", "*.pack"))
	if err != nil {
		return 0, err
	}

	var newestPackfileModtime time.Time

	for _, packFilePath := range packFiles {
		stat, err := os.Stat(packFilePath)
		if err != nil {
			return 0, err
		}
		if stat.ModTime().After(newestPackfileModtime) {
			newestPackfileModtime = stat.ModTime()
		}
	}

	var unpackedObjects int64
	if err = filepath.Walk(objectDir, func(path string, info os.FileInfo, err error) error {
		if objectDir == path {
			return nil
		}

		if info.IsDir() {
			if err := skipNonObjectDir(objectDir, path); err != nil {
				return err
			}
		}

		if !info.IsDir() && info.ModTime().After(newestPackfileModtime) {
			unpackedObjects++
		}

		return nil
	}); err != nil {
		return 0, err
	}

	return unpackedObjects, nil
}

func skipNonObjectDir(root, path string) error {
	rel, err := filepath.Rel(root, path)
	if err != nil {
		return err
	}

	if len(rel) != 2 {
		return filepath.SkipDir
	}

	if _, err := strconv.ParseUint(rel, 16, 8); err != nil {
		return filepath.SkipDir
	}

	return nil
}