forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_migrate_info.go
217 lines (172 loc) · 5.77 KB
/
command_migrate_info.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package commands
import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/git/githistory"
"github.com/git-lfs/git-lfs/git/githistory/log"
"github.com/git-lfs/git-lfs/git/odb"
"github.com/git-lfs/git-lfs/tools"
"github.com/git-lfs/git-lfs/tools/humanize"
"github.com/spf13/cobra"
)
var (
// migrateInfoTopN is a flag given to the git-lfs-migrate(1) subcommand
// 'info' which specifies how many info entries to show by default.
migrateInfoTopN int
// migrateInfoAboveFmt is a flag given to the git-lfs-migrate(1)
// subcommand 'info' specifying a human-readable string threshold of
// filesize before entries are counted.
migrateInfoAboveFmt string
// migrateInfoAbove is the number of bytes parsed from the above
// migrateInfoAboveFmt flag.
migrateInfoAbove uint64
// migrateInfoUnitFmt is a flag given to the git-lfs-migrate(1)
// subcommand 'info' specifying a human-readable string of units with
// which to display the number of bytes.
migrateInfoUnitFmt string
// migrateInfoUnit is the number of bytes in the unit given as
// migrateInfoUnitFmt.
migrateInfoUnit uint64
)
func migrateInfoCommand(cmd *cobra.Command, args []string) {
l := log.NewLogger(os.Stderr)
db, err := getObjectDatabase()
if err != nil {
ExitWithError(err)
}
defer db.Close()
rewriter := getHistoryRewriter(cmd, db, l)
exts := make(map[string]*MigrateInfoEntry)
above, err := humanize.ParseBytes(migrateInfoAboveFmt)
if err != nil {
ExitWithError(errors.Wrap(err, "cannot parse --above=<n>"))
}
if u := cmd.Flag("unit"); u.Changed {
unit, err := humanize.ParseByteUnit(u.Value.String())
if err != nil {
ExitWithError(errors.Wrap(err, "cannot parse --unit=<unit>"))
}
migrateInfoUnit = unit
}
migrateInfoAbove = above
migrate(args, rewriter, l, &githistory.RewriteOptions{
BlobFn: func(path string, b *odb.Blob) (*odb.Blob, error) {
ext := fmt.Sprintf("*%s", filepath.Ext(path))
if len(ext) > 1 {
entry := exts[ext]
if entry == nil {
entry = &MigrateInfoEntry{Qualifier: ext}
}
entry.Total++
entry.BytesTotal += b.Size
if b.Size > int64(migrateInfoAbove) {
entry.TotalAbove++
entry.BytesAbove += b.Size
}
exts[ext] = entry
}
return b, nil
},
})
l.Close()
entries := EntriesBySize(MapToEntries(exts))
entries = removeEmptyEntries(entries)
sort.Sort(sort.Reverse(entries))
migrateInfoTopN = tools.ClampInt(migrateInfoTopN, len(entries), 0)
entries = entries[:tools.MaxInt(0, migrateInfoTopN)]
entries.Print(os.Stdout)
}
// MigrateInfoEntry represents a tuple of filetype to bytes and entry count
// above and below a threshold.
type MigrateInfoEntry struct {
// Qualifier is the filepath's extension.
Qualifier string
// BytesAbove is total size of all files above a given threshold.
BytesAbove int64
// TotalAbove is the count of all files above a given size threshold.
TotalAbove int64
// BytesTotal is the number of bytes of all files
BytesTotal int64
// Total is the count of all files.
Total int64
}
// MapToEntries creates a set of `*MigrateInfoEntry`'s for a given map of
// filepath extensions to file size in bytes.
func MapToEntries(exts map[string]*MigrateInfoEntry) []*MigrateInfoEntry {
entries := make([]*MigrateInfoEntry, 0, len(exts))
for _, entry := range exts {
entries = append(entries, entry)
}
return entries
}
// removeEmptyEntries removes `*MigrateInfoEntry`'s for which no matching file
// is above the given threshold "--above".
func removeEmptyEntries(entries []*MigrateInfoEntry) []*MigrateInfoEntry {
nz := make([]*MigrateInfoEntry, 0, len(entries))
for _, e := range entries {
if e.TotalAbove > 0 {
nz = append(nz, e)
}
}
return nz
}
// EntriesBySize is an implementation of sort.Interface that sorts a set of
// `*MigrateInfoEntry`'s
type EntriesBySize []*MigrateInfoEntry
// Len returns the total length of the set of `*MigrateInfoEntry`'s.
func (e EntriesBySize) Len() int { return len(e) }
// Less returns the whether or not the MigrateInfoEntry given at `i` takes up
// less total size than the MigrateInfoEntry given at `j`.
func (e EntriesBySize) Less(i, j int) bool { return e[i].BytesAbove < e[j].BytesAbove }
// Swap swaps the entries given at i, j.
func (e EntriesBySize) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
// Print formats the `*MigrateInfoEntry`'s in the set and prints them to the
// given io.Writer, "to", returning "n" the number of bytes written, and any
// error, if one occurred.
func (e EntriesBySize) Print(to io.Writer) (int, error) {
if len(e) == 0 {
return 0, nil
}
extensions := make([]string, 0, len(e))
sizes := make([]string, 0, len(e))
stats := make([]string, 0, len(e))
percentages := make([]string, 0, len(e))
for _, entry := range e {
bytesAbove := uint64(entry.BytesAbove)
above := entry.TotalAbove
total := entry.Total
percentAbove := 100 * (float64(above) / float64(total))
var size string
if migrateInfoUnit > 0 {
size = humanize.FormatBytesUnit(bytesAbove, migrateInfoUnit)
} else {
size = humanize.FormatBytes(bytesAbove)
}
stat := fmt.Sprintf("%d/%d files(s)",
above, total)
percentage := fmt.Sprintf("%.0f%%", percentAbove)
extensions = append(extensions, entry.Qualifier)
sizes = append(sizes, size)
stats = append(stats, stat)
percentages = append(percentages, percentage)
}
extensions = tools.Ljust(extensions)
sizes = tools.Ljust(sizes)
stats = tools.Rjust(stats)
percentages = tools.Rjust(percentages)
output := make([]string, 0, len(e))
for i := 0; i < len(e); i++ {
extension := extensions[i]
size := sizes[i]
stat := stats[i]
percentage := percentages[i]
line := strings.Join([]string{extension, size, stat, percentage}, "\t")
output = append(output, line)
}
return fmt.Fprintln(to, strings.Join(output, "\n"))
}