Skip to content

Commit b804429

Browse files
committed
[ui] Reshorten refs for output (#413)
* Add shortener for SHA1 refs * Print shortened refs
1 parent cb94dcc commit b804429

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

internal/cli/formatters.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package cli
55

66
import (
7+
"strings"
78
"time"
89

910
"github.com/ryanuber/columnize"
@@ -34,3 +35,16 @@ func formatTime(t time.Time) string {
3435
func formatTimeDifference(first, second time.Time, d time.Duration) string {
3536
return second.Truncate(d).Sub(first.Truncate(d)).String()
3637
}
38+
39+
func formatSHA1Reference(in string) string {
40+
// a SHA1 hash is 20 bytes written as a hexadecimal string (40 chars)
41+
if len(in) != 40 && len(strings.Trim(strings.ToLower(in), "0123456789abcdef")) != 0 {
42+
// if it can't be a sha1, return it unchanged
43+
return in
44+
}
45+
l := 8
46+
if len(in) < l {
47+
l = len(in)
48+
}
49+
return in[:l]
50+
}

internal/cli/helpers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ func registryTableRow(cachedRegistry *cache.Registry) []terminal.TableEntry {
6666
Value: cachedRegistry.Name,
6767
},
6868
{
69-
Value: cachedRegistry.Ref,
69+
Value: formatSHA1Reference(cachedRegistry.Ref),
7070
},
7171
{
72-
Value: cachedRegistry.LocalRef,
72+
Value: formatSHA1Reference(cachedRegistry.LocalRef),
7373
},
7474
{
7575
Value: cachedRegistry.Source,
@@ -85,11 +85,11 @@ func registryPackRow(cachedRegistry *cache.Registry, cachedPack *cache.Pack) []t
8585
},
8686
// The revision from where the registryPack was cloned
8787
{
88-
Value: cachedPack.Ref,
88+
Value: formatSHA1Reference(cachedPack.Ref),
8989
},
9090
// The canonical revision from where the registryPack was cloned
9191
{
92-
Value: cachedRegistry.LocalRef,
92+
Value: formatSHA1Reference(cachedRegistry.LocalRef),
9393
},
9494
// The metadata version
9595
{

0 commit comments

Comments
 (0)