Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 0b1ee9f

Browse files
author
Eduardo Lezcano
committed
Improved Describe.String()
Signed-off-by: Eduardo Lezcano <[email protected]>
1 parent 0605454 commit 0b1ee9f

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

options.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ type DescribeOptions struct {
446446
//FirstParent bool
447447
// Use <Abbrev> digits to display SHA-1s
448448
// By default is 8
449-
//Abbrev int
449+
Abbrev int
450450
// Only output exact matches
451451
//ExactMatch bool
452452
// Consider <Candidates> most recent tags
@@ -460,4 +460,6 @@ type DescribeOptions struct {
460460
Dirty string
461461
}
462462

463-
func (o *DescribeOptions) Validate() error { return nil }
463+
func (o *DescribeOptions) Validate() error {
464+
if o.Abbrev == 0 { o.Abbrev = 8 }
465+
return nil }

repository.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -1233,14 +1233,25 @@ type Describe struct {
12331233
Distance int
12341234
// Dirty string to append
12351235
Dirty string
1236+
// Use <Abbrev> digits to display SHA-ls
1237+
Abbrev int
12361238
}
12371239

12381240
func (d *Describe) String() string {
1239-
return fmt.Sprintf("%v-%v-%v-%v",
1240-
d.Tag.Name().Short(),
1241-
d.Distance,
1242-
d.Reference.Hash().String()[0:8],
1243-
d.Dirty)
1241+
var s []string
1242+
1243+
if d.Tag != nil{
1244+
s = append(s, d.Tag.Name().Short())
1245+
}
1246+
if d.Distance > 0 {
1247+
s = append(s, fmt.Sprint(d.Distance))
1248+
}
1249+
s = append(s, d.Reference.Hash().String()[0:d.Abbrev])
1250+
if d.Dirty != "" {
1251+
s = append(s, d.Dirty)
1252+
}
1253+
1254+
return strings.Join(s, "-")
12441255
}
12451256

12461257
// Describe just like the `git describe` command will return a Describe struct for the hash passed.
@@ -1290,6 +1301,7 @@ func (r *Repository) Describe(ref *plumbing.Reference, opts *DescribeOptions) (*
12901301
tag,
12911302
count,
12921303
opts.Dirty,
1304+
opts.Abbrev,
12931305
}, nil
12941306

12951307
}

0 commit comments

Comments
 (0)