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

Commit 3431da0

Browse files
author
Eduardo Lezcano
committed
Describe method initial implementation
Signed-off-by: Eduardo Lezcano <eduardo.lezcano@be.atlascopco.com>
1 parent 87cc819 commit 3431da0

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

repository.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,3 +1223,102 @@ func (r *Repository) createNewObjectPack(cfg *RepackConfig) (h plumbing.Hash, er
12231223

12241224
return h, err
12251225
}
1226+
1227+
type DescribeOptions struct {
1228+
// Contains find the tag that comes after the commit
1229+
//Contains bool
1230+
// Debug search strategy on stderr
1231+
//Debug bool
1232+
// All Use any reference
1233+
//All bool
1234+
// Tags use any tag, even unannotated
1235+
//Tags bool
1236+
// FirstParent only follow first parent
1237+
//FirstParent bool
1238+
// Use <Abbrev> digits to display SHA-1s
1239+
// By default is 8
1240+
//Abbrev int
1241+
// Only output exact matches
1242+
//ExactMatch bool
1243+
// Consider <Candidates> most recent tags
1244+
// By default is 10
1245+
//Candidates int
1246+
// Only consider tags matching <Match> pattern
1247+
//Match string
1248+
// Show abbreviated commit object as fallback
1249+
//Always bool
1250+
// Append <mark> on dirty working tree (default: "-dirty")
1251+
Dirty string
1252+
}
1253+
1254+
type Describe struct {
1255+
// Reference being described
1256+
Hash *plumbing.Hash
1257+
// Tag of the describe object
1258+
Tag *plumbing.Reference
1259+
// Distance to the tag object in commits
1260+
Distance int
1261+
// Dirty string to append
1262+
Dirty string
1263+
}
1264+
1265+
func (d *Describe) String() string {
1266+
return fmt.Sprintf("%v-%v-%v-%v",
1267+
d.Tag.Name().Short(),
1268+
d.Distance,
1269+
d.Hash.String()[0:8],
1270+
d.Dirty)
1271+
}
1272+
1273+
func (r *Repository) Describe(options *DescribeOptions, hashes ...*plumbing.Hash) ([]Describe, error) {
1274+
1275+
var describes []Describe
1276+
1277+
for _, hash := range hashes {
1278+
1279+
// Fetch the reference log
1280+
commitIterator, err := r.Log(&LogOptions{
1281+
From: *hash,
1282+
Order: LogOrderCommitterTime,
1283+
})
1284+
if err != nil {
1285+
return nil, err
1286+
}
1287+
1288+
// Tag map for rapid (?) query
1289+
tagIterator, err := r.Tags()
1290+
if err != nil {
1291+
return nil, err
1292+
}
1293+
tags := make(map[plumbing.Hash]*plumbing.Reference)
1294+
tagIterator.ForEach(func(t *plumbing.Reference) error {
1295+
tags[t.Hash()] = t
1296+
return nil
1297+
})
1298+
1299+
1300+
// Search the tag
1301+
var tag *plumbing.Reference
1302+
var count int
1303+
err = commitIterator.ForEach(func(c *object.Commit) error {
1304+
if t, ok := tags[c.Hash]; ok {
1305+
tag = t
1306+
}
1307+
if tag != nil {
1308+
return storer.ErrStop
1309+
}
1310+
count++
1311+
return nil
1312+
})
1313+
1314+
describes = append(describes, Describe{
1315+
hash,
1316+
tag,
1317+
count,
1318+
options.Dirty,
1319+
})
1320+
1321+
}
1322+
1323+
return describes, nil
1324+
}

0 commit comments

Comments
 (0)