From 030324a34cdb88aacae568304c19875a0fece7e2 Mon Sep 17 00:00:00 2001 From: Eduardo Lezcano Date: Fri, 20 Apr 2018 14:44:52 +0200 Subject: [PATCH] Improved Describe.String() Signed-off-by: Eduardo Lezcano --- repository.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/repository.go b/repository.go index 0f179b664..213f9f19a 100644 --- a/repository.go +++ b/repository.go @@ -1233,14 +1233,25 @@ type Describe struct { Distance int // Dirty string to append Dirty string + // Use digits to display SHA-ls + Abbrev int } func (d *Describe) String() string { - return fmt.Sprintf("%v-%v-%v-%v", - d.Tag.Name().Short(), - d.Distance, - d.Reference.Hash().String()[0:8], - d.Dirty) + var s []string + + if d.Tag != nil{ + s = append(s, d.Tag.Name().Short()) + } + if d.Distance > 0 { + s = append(s, fmt.Sprint(d.Distance)) + } + s = append(s, d.Reference.Hash().String()[0:d.Abbrev]) + if d.Dirty != "" { + s = append(s, d.Dirty) + } + + return strings.Join(s, "-") } // Describe just like the `git describe` command will return a Describe struct for the hash passed.