From d873056e03f21724f799824e02a17b2da7fd29af Mon Sep 17 00:00:00 2001 From: Alan Cabrera Date: Wed, 14 Mar 2018 07:58:13 -0700 Subject: [PATCH 1/2] Fix RefSpec.Src() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, the Src() function was assuming there are no “+” characters in the refspec src and erroneously used the strings.Index() function to compute the start index of src in the refspec. This change now uses the IsForceUpdate() method to decide how to elide the force update token. Signed-off-by: Alan Cabrera --- config/refspec.go | 8 +++++++- config/refspec_test.go | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/config/refspec.go b/config/refspec.go index af7e73205..c9b9d524f 100644 --- a/config/refspec.go +++ b/config/refspec.go @@ -62,7 +62,13 @@ func (s RefSpec) IsDelete() bool { // Src return the src side. func (s RefSpec) Src() string { spec := string(s) - start := strings.Index(spec, refSpecForce) + 1 + + var start int + if s.IsForceUpdate() { + start = 1 + } else { + start = 0 + } end := strings.Index(spec, refSpecSeparator) return spec[start:end] diff --git a/config/refspec_test.go b/config/refspec_test.go index 5ee610893..6daddb428 100644 --- a/config/refspec_test.go +++ b/config/refspec_test.go @@ -64,6 +64,9 @@ func (s *RefSpecSuite) TestRefSpecSrc(c *C) { spec = RefSpec(":refs/heads/master") c.Assert(spec.Src(), Equals, "") + + spec = RefSpec("refs/heads/love+hate:refs/heads/love+hate") + c.Assert(spec.Src(), Equals, "refs/heads/love+hate") } func (s *RefSpecSuite) TestRefSpecMatch(c *C) { @@ -74,6 +77,9 @@ func (s *RefSpecSuite) TestRefSpecMatch(c *C) { spec = RefSpec(":refs/heads/master") c.Assert(spec.Match(plumbing.ReferenceName("")), Equals, true) c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/master")), Equals, false) + + spec = RefSpec("refs/heads/love+hate:heads/love+hate") + c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/love+hate")), Equals, true) } func (s *RefSpecSuite) TestRefSpecMatchGlob(c *C) { From 87b70781ab9ff69811999396d76ca7dfdf6de24b Mon Sep 17 00:00:00 2001 From: Alan Cabrera Date: Wed, 14 Mar 2018 09:33:54 -0700 Subject: [PATCH 2/2] Add more unit tests for RefSpec Need this to get better code coverage of the bug fix. Signed-off-by: Alan Cabrera --- config/refspec_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/config/refspec_test.go b/config/refspec_test.go index 6daddb428..675e075cc 100644 --- a/config/refspec_test.go +++ b/config/refspec_test.go @@ -62,11 +62,17 @@ func (s *RefSpecSuite) TestRefSpecSrc(c *C) { spec := RefSpec("refs/heads/*:refs/remotes/origin/*") c.Assert(spec.Src(), Equals, "refs/heads/*") + spec = RefSpec("+refs/heads/*:refs/remotes/origin/*") + c.Assert(spec.Src(), Equals, "refs/heads/*") + spec = RefSpec(":refs/heads/master") c.Assert(spec.Src(), Equals, "") spec = RefSpec("refs/heads/love+hate:refs/heads/love+hate") c.Assert(spec.Src(), Equals, "refs/heads/love+hate") + + spec = RefSpec("+refs/heads/love+hate:refs/heads/love+hate") + c.Assert(spec.Src(), Equals, "refs/heads/love+hate") } func (s *RefSpecSuite) TestRefSpecMatch(c *C) { @@ -74,12 +80,19 @@ func (s *RefSpecSuite) TestRefSpecMatch(c *C) { c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/foo")), Equals, false) c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/master")), Equals, true) + spec = RefSpec("+refs/heads/master:refs/remotes/origin/master") + c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/foo")), Equals, false) + c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/master")), Equals, true) + spec = RefSpec(":refs/heads/master") c.Assert(spec.Match(plumbing.ReferenceName("")), Equals, true) c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/master")), Equals, false) spec = RefSpec("refs/heads/love+hate:heads/love+hate") c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/love+hate")), Equals, true) + + spec = RefSpec("+refs/heads/love+hate:heads/love+hate") + c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/love+hate")), Equals, true) } func (s *RefSpecSuite) TestRefSpecMatchGlob(c *C) {