aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/reference.go
diff options
context:
space:
mode:
authorSantiago M. Mola <santi@mola.io>2017-07-07 12:25:18 +0200
committerSantiago M. Mola <santi@mola.io>2017-07-07 12:25:18 +0200
commit61884bbfb0690872cc4b9f0a2a6b1aeb83e7dd3a (patch)
treef67c8ad271a4c9d85fff6dfb7439f062aded9f00 /plumbing/reference.go
parent102d4b5aeb9b3cbd544c59706a1b0dd9300ddcc8 (diff)
downloadgo-git-61884bbfb0690872cc4b9f0a2a6b1aeb83e7dd3a.tar.gz
fix reference shortening
Implemented according to git shorten_unambiguous_ref. See: https://github.com/git/git/blob/e0aaa1b6532cfce93d87af9bc813fb2e7a7ce9d7/refs.c#L1030
Diffstat (limited to 'plumbing/reference.go')
-rw-r--r--plumbing/reference.go36
1 files changed, 18 insertions, 18 deletions
diff --git a/plumbing/reference.go b/plumbing/reference.go
index 8fa103e..5d477b9 100644
--- a/plumbing/reference.go
+++ b/plumbing/reference.go
@@ -15,15 +15,16 @@ const (
symrefPrefix = "ref: "
)
-var (
- refPrefixes = []string{
- refHeadPrefix,
- refTagPrefix,
- refRemotePrefix,
- refNotePrefix,
- refPrefix,
- }
-)
+// refRevParseRules are a set of rules to parse references into short names.
+// These are the same rules as used by git in shorten_unambiguous_ref.
+// See: https://github.com/git/git/blob/e0aaa1b6532cfce93d87af9bc813fb2e7a7ce9d7/refs.c#L417
+var refRevParseRules = []string{
+ "refs/%s",
+ "refs/tags/%s",
+ "refs/heads/%s",
+ "refs/remotes/%s",
+ "refs/remotes/%s/HEAD",
+}
var (
ErrReferenceNotFound = errors.New("reference not found")
@@ -60,17 +61,16 @@ func (r ReferenceName) String() string {
// Short returns the short name of a ReferenceName
func (r ReferenceName) Short() string {
- return r.removeRefPrefix()
-}
-
-// Instead of hardcoding a number of components, we should remove the prefixes
-// refHeadPrefix, refTagPrefix, refRemotePrefix, refNotePrefix and refPrefix
-func (r ReferenceName) removeRefPrefix() string {
s := string(r)
- for _, prefix := range refPrefixes {
- s = strings.TrimPrefix(s, prefix)
+ res := s
+ for _, format := range refRevParseRules {
+ _, err := fmt.Sscanf(s, format, &res)
+ if err == nil {
+ continue
+ }
}
- return s
+
+ return res
}
const (