aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-07-07 16:38:24 -0700
committerGitHub <noreply@github.com>2017-07-07 16:38:24 -0700
commit83c0e738df4b676e6c4aeed5fd1bc7c90fb341c5 (patch)
treecbb4d9fdbce36b28d951eb28b56fefe05624193f /plumbing
parentb0f7c5b0cc6b3e22a78988e44134ab65ae76eb5a (diff)
parent61884bbfb0690872cc4b9f0a2a6b1aeb83e7dd3a (diff)
downloadgo-git-83c0e738df4b676e6c4aeed5fd1bc7c90fb341c5.tar.gz
Merge pull request #465 from smola/fix-short-ref
fix reference shortening
Diffstat (limited to 'plumbing')
-rw-r--r--plumbing/reference.go36
-rw-r--r--plumbing/reference_test.go5
2 files changed, 23 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 (
diff --git a/plumbing/reference_test.go b/plumbing/reference_test.go
index 6a695f4..97c8772 100644
--- a/plumbing/reference_test.go
+++ b/plumbing/reference_test.go
@@ -23,6 +23,11 @@ func (s *ReferenceSuite) TestReferenceNameWithSlash(c *C) {
c.Assert(r.Short(), Equals, "origin/feature/AllowSlashes")
}
+func (s *ReferenceSuite) TestReferenceNameNote(c *C) {
+ r := ReferenceName("refs/notes/foo")
+ c.Assert(r.Short(), Equals, "notes/foo")
+}
+
func (s *ReferenceSuite) TestNewReferenceFromStrings(c *C) {
r := NewReferenceFromStrings("refs/heads/v4", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
c.Assert(r.Type(), Equals, HashReference)