diff options
-rw-r--r-- | .travis.yml | 1 | ||||
-rw-r--r-- | blame.go | 8 | ||||
-rw-r--r-- | blame_test.go | 1 | ||||
-rw-r--r-- | plumbing/format/diff/unified_encoder.go | 8 | ||||
-rw-r--r-- | plumbing/format/diff/unified_encoder_test.go | 37 | ||||
-rw-r--r-- | plumbing/format/packfile/delta_selector.go | 5 | ||||
-rw-r--r-- | plumbing/format/packfile/encoder_test.go | 4 | ||||
-rw-r--r-- | plumbing/format/packfile/object_pack.go | 17 | ||||
-rw-r--r-- | plumbing/transport/http/common.go | 34 | ||||
-rw-r--r-- | plumbing/transport/http/receive_pack.go | 2 | ||||
-rw-r--r-- | plumbing/transport/http/upload_pack.go | 2 | ||||
-rw-r--r-- | plumbing/transport/http/upload_pack_test.go | 28 | ||||
-rw-r--r-- | plumbing/transport/ssh/auth_method.go | 7 | ||||
-rw-r--r-- | plumbing/transport/ssh/auth_method_test.go | 6 | ||||
-rw-r--r-- | storage/filesystem/shallow.go | 2 |
15 files changed, 137 insertions, 25 deletions
diff --git a/.travis.yml b/.travis.yml index ee975e4..49d8608 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: go go: - 1.8.x - 1.9.x + - "1.10" go_import_path: gopkg.in/src-d/go-git.v4 @@ -6,6 +6,7 @@ import ( "fmt" "strconv" "strings" + "time" "unicode/utf8" "gopkg.in/src-d/go-git.v4/plumbing" @@ -106,12 +107,15 @@ type Line struct { Author string // Text is the original text of the line. Text string + // Date is when the original text of the line was introduced + Date time.Time } -func newLine(author, text string) *Line { +func newLine(author, text string, date time.Time) *Line { return &Line{ Author: author, Text: text, + Date: date, } } @@ -121,7 +125,7 @@ func newLines(contents []string, commits []*object.Commit) ([]*Line, error) { } result := make([]*Line, 0, len(contents)) for i := range contents { - l := newLine(commits[i].Author.Email, contents[i]) + l := newLine(commits[i].Author.Email, contents[i], commits[i].Author.When) result = append(result, l) } return result, nil diff --git a/blame_test.go b/blame_test.go index 5374610..51c546a 100644 --- a/blame_test.go +++ b/blame_test.go @@ -53,6 +53,7 @@ func (s *BlameSuite) mockBlame(c *C, t blameTest, r *Repository) (blame *BlameRe l := &Line{ Author: commit.Author.Email, Text: lines[i], + Date: commit.Author.When, } blamedLines = append(blamedLines, l) } diff --git a/plumbing/format/diff/unified_encoder.go b/plumbing/format/diff/unified_encoder.go index cf2a34b..58edd95 100644 --- a/plumbing/format/diff/unified_encoder.go +++ b/plumbing/format/diff/unified_encoder.go @@ -262,11 +262,15 @@ func (c *hunksGenerator) processEqualsLines(ls []string, i int) { c.current.AddOp(Equal, c.afterContext...) c.afterContext = nil } else { - c.current.AddOp(Equal, c.afterContext[:c.ctxLines]...) + ctxLines := c.ctxLines + if ctxLines > len(c.afterContext) { + ctxLines = len(c.afterContext) + } + c.current.AddOp(Equal, c.afterContext[:ctxLines]...) c.hunks = append(c.hunks, c.current) c.current = nil - c.beforeContext = c.afterContext[c.ctxLines:] + c.beforeContext = c.afterContext[ctxLines:] c.afterContext = nil } } diff --git a/plumbing/format/diff/unified_encoder_test.go b/plumbing/format/diff/unified_encoder_test.go index 6e12070..0e419ca 100644 --- a/plumbing/format/diff/unified_encoder_test.go +++ b/plumbing/format/diff/unified_encoder_test.go @@ -476,6 +476,43 @@ index ab5eed5d4a2c33aeef67e0188ee79bed666bde6f..0adddcde4fd38042c354518351820eb0 W `, }, { + patch: oneChunkPatch, + desc: "modified deleting lines file with context to 6", + context: 6, + diff: `diff --git a/onechunk.txt b/onechunk.txt +index ab5eed5d4a2c33aeef67e0188ee79bed666bde6f..0adddcde4fd38042c354518351820eb06c417c82 100644 +--- a/onechunk.txt ++++ b/onechunk.txt +@@ -1,27 +1,23 @@ +-A + B + C + D + E + F + G +-H + I + J + K + L + M + N +-Ñ + O + P + Q + R + S + T +-U + V + W + X + Y + Z +`, +}, { patch: oneChunkPatch, desc: "modified deleting lines file with context to 3", diff --git a/plumbing/format/packfile/delta_selector.go b/plumbing/format/packfile/delta_selector.go index 1d9fb5f..6710085 100644 --- a/plumbing/format/packfile/delta_selector.go +++ b/plumbing/format/packfile/delta_selector.go @@ -103,7 +103,7 @@ func (dw *deltaSelector) objectsToPack( otp := newObjectToPack(o) if _, ok := o.(plumbing.DeltaObject); ok { - otp.Original = nil + otp.CleanOriginal() } objectsToPack = append(objectsToPack, otp) @@ -231,7 +231,8 @@ func (dw *deltaSelector) walk( delete(indexMap, obj.Hash()) if obj.IsDelta() { - obj.Original = nil + obj.SaveOriginalMetadata() + obj.CleanOriginal() } } diff --git a/plumbing/format/packfile/encoder_test.go b/plumbing/format/packfile/encoder_test.go index 63dfafa..84d03fb 100644 --- a/plumbing/format/packfile/encoder_test.go +++ b/plumbing/format/packfile/encoder_test.go @@ -233,10 +233,10 @@ func (s *EncoderSuite) deltaOverDeltaCyclicTest(c *C) { // is nil. po1.SetOriginal(po1.Original) pd2.SetOriginal(pd2.Original) - pd2.SetOriginal(nil) + pd2.CleanOriginal() pd3.SetOriginal(pd3.Original) - pd3.SetOriginal(nil) + pd3.CleanOriginal() pd4.SetOriginal(pd4.Original) diff --git a/plumbing/format/packfile/object_pack.go b/plumbing/format/packfile/object_pack.go index 877581e..dfea571 100644 --- a/plumbing/format/packfile/object_pack.go +++ b/plumbing/format/packfile/object_pack.go @@ -81,15 +81,24 @@ func (o *ObjectToPack) WantWrite() bool { // is nil Original is set but previous resolved values are kept func (o *ObjectToPack) SetOriginal(obj plumbing.EncodedObject) { o.Original = obj + o.SaveOriginalMetadata() +} - if obj != nil { - o.originalSize = obj.Size() - o.originalType = obj.Type() - o.originalHash = obj.Hash() +// SaveOriginalMetadata saves size, type and hash of Original object +func (o *ObjectToPack) SaveOriginalMetadata() { + if o.Original != nil { + o.originalSize = o.Original.Size() + o.originalType = o.Original.Type() + o.originalHash = o.Original.Hash() o.resolvedOriginal = true } } +// CleanOriginal sets Original to nil +func (o *ObjectToPack) CleanOriginal() { + o.Original = nil +} + func (o *ObjectToPack) Type() plumbing.ObjectType { if o.Original != nil { return o.Original.Type() diff --git a/plumbing/transport/http/common.go b/plumbing/transport/http/common.go index edf1c6c..24e63a4 100644 --- a/plumbing/transport/http/common.go +++ b/plumbing/transport/http/common.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "strconv" + "strings" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp" @@ -28,10 +29,12 @@ func applyHeadersToRequest(req *http.Request, content *bytes.Buffer, host string req.Header.Add("Content-Length", strconv.Itoa(content.Len())) } +const infoRefsPath = "/info/refs" + func advertisedReferences(s *session, serviceName string) (*packp.AdvRefs, error) { url := fmt.Sprintf( - "%s/info/refs?service=%s", - s.endpoint.String(), serviceName, + "%s%s?service=%s", + s.endpoint.String(), infoRefsPath, serviceName, ) req, err := http.NewRequest(http.MethodGet, url, nil) @@ -39,13 +42,14 @@ func advertisedReferences(s *session, serviceName string) (*packp.AdvRefs, error return nil, err } - s.applyAuthToRequest(req) + s.ApplyAuthToRequest(req) applyHeadersToRequest(req, nil, s.endpoint.Host, serviceName) res, err := s.client.Do(req) if err != nil { return nil, err } + s.ModifyEndpointIfRedirect(res) defer ioutil.CheckClose(res.Body, &err) if err := NewErr(res); err != nil { @@ -129,11 +133,7 @@ func newSession(c *http.Client, ep *transport.Endpoint, auth transport.AuthMetho return s, nil } -func (*session) Close() error { - return nil -} - -func (s *session) applyAuthToRequest(req *http.Request) { +func (s *session) ApplyAuthToRequest(req *http.Request) { if s.auth == nil { return } @@ -141,6 +141,24 @@ func (s *session) applyAuthToRequest(req *http.Request) { s.auth.setAuth(req) } +func (s *session) ModifyEndpointIfRedirect(res *http.Response) { + if res.Request == nil { + return + } + + r := res.Request + if !strings.HasSuffix(r.URL.Path, infoRefsPath) { + return + } + + s.endpoint.Protocol = r.URL.Scheme + s.endpoint.Path = r.URL.Path[:len(r.URL.Path)-len(infoRefsPath)] +} + +func (*session) Close() error { + return nil +} + // AuthMethod is concrete implementation of common.AuthMethod for HTTP services type AuthMethod interface { transport.AuthMethod diff --git a/plumbing/transport/http/receive_pack.go b/plumbing/transport/http/receive_pack.go index e5cae28..72ba0ec 100644 --- a/plumbing/transport/http/receive_pack.go +++ b/plumbing/transport/http/receive_pack.go @@ -90,7 +90,7 @@ func (s *rpSession) doRequest( } applyHeadersToRequest(req, content, s.endpoint.Host, transport.ReceivePackServiceName) - s.applyAuthToRequest(req) + s.ApplyAuthToRequest(req) res, err := s.client.Do(req.WithContext(ctx)) if err != nil { diff --git a/plumbing/transport/http/upload_pack.go b/plumbing/transport/http/upload_pack.go index 85a57a5..fb5ac36 100644 --- a/plumbing/transport/http/upload_pack.go +++ b/plumbing/transport/http/upload_pack.go @@ -88,7 +88,7 @@ func (s *upSession) doRequest( } applyHeadersToRequest(req, content, s.endpoint.Host, transport.UploadPackServiceName) - s.applyAuthToRequest(req) + s.ApplyAuthToRequest(req) res, err := s.client.Do(req.WithContext(ctx)) if err != nil { diff --git a/plumbing/transport/http/upload_pack_test.go b/plumbing/transport/http/upload_pack_test.go index fbd28c7..3b85af5 100644 --- a/plumbing/transport/http/upload_pack_test.go +++ b/plumbing/transport/http/upload_pack_test.go @@ -75,3 +75,31 @@ func (s *UploadPackSuite) newEndpoint(c *C, name string) *transport.Endpoint { return ep } + +func (s *UploadPackSuite) TestAdvertisedReferencesRedirectPath(c *C) { + endpoint, _ := transport.NewEndpoint("https://gitlab.com/gitlab-org/gitter/webapp") + + session, err := s.Client.NewUploadPackSession(endpoint, s.EmptyAuth) + c.Assert(err, IsNil) + + info, err := session.AdvertisedReferences() + c.Assert(err, IsNil) + c.Assert(info, NotNil) + + url := session.(*upSession).endpoint.String() + c.Assert(url, Equals, "https://gitlab.com/gitlab-org/gitter/webapp.git") +} + +func (s *UploadPackSuite) TestAdvertisedReferencesRedirectSchema(c *C) { + endpoint, _ := transport.NewEndpoint("http://github.com/git-fixtures/basic") + + session, err := s.Client.NewUploadPackSession(endpoint, s.EmptyAuth) + c.Assert(err, IsNil) + + info, err := session.AdvertisedReferences() + c.Assert(err, IsNil) + c.Assert(info, NotNil) + + url := session.(*upSession).endpoint.String() + c.Assert(url, Equals, "https://github.com/git-fixtures/basic") +} diff --git a/plumbing/transport/ssh/auth_method.go b/plumbing/transport/ssh/auth_method.go index a092b29..84cfab2 100644 --- a/plumbing/transport/ssh/auth_method.go +++ b/plumbing/transport/ssh/auth_method.go @@ -124,6 +124,9 @@ type PublicKeys struct { // (PKCS#1), DSA (OpenSSL), and ECDSA private keys. func NewPublicKeys(user string, pemBytes []byte, password string) (*PublicKeys, error) { block, _ := pem.Decode(pemBytes) + if block == nil { + return nil, errors.New("invalid PEM data") + } if x509.IsEncryptedPEMBlock(block) { key, err := x509.DecryptPEMBlock(block, []byte(password)) if err != nil { @@ -231,7 +234,7 @@ func (a *PublicKeysCallback) ClientConfig() (*ssh.ClientConfig, error) { } // NewKnownHostsCallback returns ssh.HostKeyCallback based on a file based on a -// know_hosts file. http://man.openbsd.org/sshd#SSH_KNOWN_HOSTS_FILE_FORMAT +// known_hosts file. http://man.openbsd.org/sshd#SSH_KNOWN_HOSTS_FILE_FORMAT // // If files is empty, the list of files will be read from the SSH_KNOWN_HOSTS // environment variable, example: @@ -286,7 +289,7 @@ func filterKnownHostsFiles(files ...string) ([]string, error) { } if len(out) == 0 { - return nil, fmt.Errorf("unable to find any valid know_hosts file, set SSH_KNOWN_HOSTS env variable") + return nil, fmt.Errorf("unable to find any valid known_hosts file, set SSH_KNOWN_HOSTS env variable") } return out, nil diff --git a/plumbing/transport/ssh/auth_method_test.go b/plumbing/transport/ssh/auth_method_test.go index 1e77ca0..0025669 100644 --- a/plumbing/transport/ssh/auth_method_test.go +++ b/plumbing/transport/ssh/auth_method_test.go @@ -143,3 +143,9 @@ func (*SuiteCommon) TestNewPublicKeysFromFile(c *C) { c.Assert(err, IsNil) c.Assert(auth, NotNil) } + +func (*SuiteCommon) TestNewPublicKeysWithInvalidPEM(c *C) { + auth, err := NewPublicKeys("foo", []byte("bar"), "") + c.Assert(err, NotNil) + c.Assert(auth, IsNil) +} diff --git a/storage/filesystem/shallow.go b/storage/filesystem/shallow.go index 394e6ed..4b2e2dc 100644 --- a/storage/filesystem/shallow.go +++ b/storage/filesystem/shallow.go @@ -26,7 +26,7 @@ func (s *ShallowStorage) SetShallow(commits []plumbing.Hash) error { defer ioutil.CheckClose(f, &err) for _, h := range commits { - if _, err := fmt.Fprintf(f, "%s\n", h); err != err { + if _, err := fmt.Fprintf(f, "%s\n", h); err != nil { return err } } |