commands/migrate: teach --unit flag

This commit is contained in:
Taylor Blau 2017-06-15 15:08:15 -06:00
parent 8d09c7ecff
commit d1037fce8c
4 changed files with 54 additions and 1 deletions

@ -209,6 +209,7 @@ func init() {
info := NewCommand("info", migrateInfoCommand)
info.Flags().IntVar(&migrateInfoTopN, "top", 5, "--top=<n>")
info.Flags().StringVar(&migrateInfoAboveFmt, "above", "1mb", "--above=<n>")
info.Flags().StringVar(&migrateInfoUnitFmt, "unit", "", "--unit=<unit>")
RegisterCommand("migrate", nil, func(cmd *cobra.Command) {
// Adding flags directly to cmd.Flags() doesn't apply those

@ -27,6 +27,14 @@ var (
// 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) {
@ -37,6 +45,15 @@ func migrateInfoCommand(cmd *cobra.Command, args []string) {
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(cmd, args, func(path string, b *odb.Blob) (*odb.Blob, error) {
@ -123,17 +140,25 @@ func (e EntriesBySize) Print(to io.Writer) (int, error) {
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, humanize.FormatBytes(uint64(entry.BytesAbove)))
sizes = append(sizes, size)
stats = append(stats, stat)
percentages = append(percentages, percentage)
}

@ -44,6 +44,15 @@ The 'info' mode has these additional options:
Only include the top 'n' entries, ordered by how many total files match the
given pathspec.
* `--unit=<unit>`
Format the number of bytes in each entry as a quantity of the storage unit
provided. Valid units include:
* b, kib, mib, gib, tib, pib - for IEC storage units
* b, kb, mb, gb, tb, pb - for SI storage units
If a --unit is not specified, the largest unit that can fit the number of
counted bytes as a whole number quantity is chosen.
## INCLUDE AND EXCLUDE
You can configure Git LFS to only migrate tree entries whose pathspec matches

@ -219,3 +219,21 @@ begin_test "migrate info (above threshold, top)"
)
end_test
begin_test "migrate info (given unit)"
(
set -e
setup_multiple_local_branches
original_head="$(git rev-parse HEAD)"
diff -u <(git lfs migrate info --above=0b --unit=kb 2>&1) <(cat <<-EOF
*.md 0.1 1/1 files(s) 100%
*.txt 0.1 1/1 files(s) 100%
EOF)
migrated_head="$(git rev-parse HEAD)"
assert_ref_unmoved "HEAD" "$original_head" "$migrated_head"
)
end_test