Skip to content

Commit

Permalink
[ui] Reshorten refs for output (#413)
Browse files Browse the repository at this point in the history
* Add shortener for SHA1 refs
* Print shortened refs
  • Loading branch information
angrycub committed Sep 25, 2023
1 parent cb94dcc commit b804429
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 14 additions & 0 deletions internal/cli/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package cli

import (
"strings"
"time"

"github.com/ryanuber/columnize"
Expand Down Expand Up @@ -34,3 +35,16 @@ func formatTime(t time.Time) string {
func formatTimeDifference(first, second time.Time, d time.Duration) string {
return second.Truncate(d).Sub(first.Truncate(d)).String()
}

func formatSHA1Reference(in string) string {
// a SHA1 hash is 20 bytes written as a hexadecimal string (40 chars)
if len(in) != 40 && len(strings.Trim(strings.ToLower(in), "0123456789abcdef")) != 0 {
// if it can't be a sha1, return it unchanged
return in
}
l := 8
if len(in) < l {
l = len(in)
}
return in[:l]
}
8 changes: 4 additions & 4 deletions internal/cli/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func registryTableRow(cachedRegistry *cache.Registry) []terminal.TableEntry {
Value: cachedRegistry.Name,
},
{
Value: cachedRegistry.Ref,
Value: formatSHA1Reference(cachedRegistry.Ref),
},
{
Value: cachedRegistry.LocalRef,
Value: formatSHA1Reference(cachedRegistry.LocalRef),
},
{
Value: cachedRegistry.Source,
Expand All @@ -85,11 +85,11 @@ func registryPackRow(cachedRegistry *cache.Registry, cachedPack *cache.Pack) []t
},
// The revision from where the registryPack was cloned
{
Value: cachedPack.Ref,
Value: formatSHA1Reference(cachedPack.Ref),
},
// The canonical revision from where the registryPack was cloned
{
Value: cachedRegistry.LocalRef,
Value: formatSHA1Reference(cachedRegistry.LocalRef),
},
// The metadata version
{
Expand Down

0 comments on commit b804429

Please sign in to comment.