diff options
19 files changed, 692 insertions, 62 deletions
diff --git a/plumbing/protocol/packp/capability/capability.go b/plumbing/protocol/packp/capability/capability.go index 06fbfca..351ba0b 100644 --- a/plumbing/protocol/packp/capability/capability.go +++ b/plumbing/protocol/packp/capability/capability.go @@ -231,6 +231,8 @@ const ( SymRef Capability = "symref" ) +const DefaultAgent = "go-git/4.x" + var valid = map[Capability]bool{ MultiACK: true, MultiACKDetailed: true, NoDone: true, ThinPack: true, Sideband: true, Sideband64k: true, OFSDelta: true, Agent: true, diff --git a/plumbing/protocol/packp/report_status.go b/plumbing/protocol/packp/report_status.go index f480b34..ead4bb6 100644 --- a/plumbing/protocol/packp/report_status.go +++ b/plumbing/protocol/packp/report_status.go @@ -1,12 +1,13 @@ package packp import ( + "bytes" + "fmt" "io" + "strings" - "fmt" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/format/pktline" - "strings" ) const ( @@ -33,7 +34,7 @@ func (s *ReportStatus) Ok() bool { // Encode writes the report status to a writer. func (s *ReportStatus) Encode(w io.Writer) error { e := pktline.NewEncoder(w) - if err := e.Encodef("unpack %s", s.UnpackStatus); err != nil { + if err := e.Encodef("unpack %s\n", s.UnpackStatus); err != nil { return err } @@ -95,6 +96,8 @@ func (s *ReportStatus) decodeReportStatus(b []byte) error { return fmt.Errorf("premature flush") } + b = bytes.TrimSuffix(b, eol) + line := string(b) fields := strings.SplitN(line, " ", 2) if len(fields) != 2 || fields[0] != "unpack" { @@ -106,6 +109,8 @@ func (s *ReportStatus) decodeReportStatus(b []byte) error { } func (s *ReportStatus) decodeCommandStatus(b []byte) error { + b = bytes.TrimSuffix(b, eol) + line := string(b) fields := strings.SplitN(line, " ", 3) status := ok @@ -138,8 +143,8 @@ func (s *CommandStatus) Ok() bool { func (s *CommandStatus) encode(w io.Writer) error { e := pktline.NewEncoder(w) if s.Ok() { - return e.Encodef("ok %s", s.ReferenceName.String()) + return e.Encodef("ok %s\n", s.ReferenceName.String()) } - return e.Encodef("ng %s %s", s.ReferenceName.String(), s.Status) + return e.Encodef("ng %s %s\n", s.ReferenceName.String(), s.Status) } diff --git a/plumbing/protocol/packp/report_status_test.go b/plumbing/protocol/packp/report_status_test.go index 064e514..168d25b 100644 --- a/plumbing/protocol/packp/report_status_test.go +++ b/plumbing/protocol/packp/report_status_test.go @@ -69,8 +69,8 @@ func (s *ReportStatusSuite) TestEncodeDecodeOkOneReference(c *C) { }} s.testEncodeDecodeOk(c, rs, - "unpack ok", - "ok refs/heads/master", + "unpack ok\n", + "ok refs/heads/master\n", pktline.FlushString, ) } @@ -84,8 +84,8 @@ func (s *ReportStatusSuite) TestEncodeDecodeOkOneReferenceFailed(c *C) { }} s.testEncodeDecodeOk(c, rs, - "unpack my error", - "ng refs/heads/master command error", + "unpack my error\n", + "ng refs/heads/master command error\n", pktline.FlushString, ) } @@ -105,10 +105,10 @@ func (s *ReportStatusSuite) TestEncodeDecodeOkMoreReferences(c *C) { }} s.testEncodeDecodeOk(c, rs, - "unpack ok", - "ok refs/heads/master", - "ok refs/heads/a", - "ok refs/heads/b", + "unpack ok\n", + "ok refs/heads/master\n", + "ok refs/heads/a\n", + "ok refs/heads/b\n", pktline.FlushString, ) } @@ -128,10 +128,10 @@ func (s *ReportStatusSuite) TestEncodeDecodeOkMoreReferencesFailed(c *C) { }} s.testEncodeDecodeOk(c, rs, - "unpack my error", - "ok refs/heads/master", - "ng refs/heads/a command error", - "ok refs/heads/b", + "unpack my error\n", + "ok refs/heads/master\n", + "ng refs/heads/a command error\n", + "ok refs/heads/b\n", pktline.FlushString, ) } @@ -141,7 +141,7 @@ func (s *ReportStatusSuite) TestEncodeDecodeOkNoReferences(c *C) { expected.UnpackStatus = "ok" s.testEncodeDecodeOk(c, expected, - "unpack ok", + "unpack ok\n", pktline.FlushString, ) } @@ -151,7 +151,7 @@ func (s *ReportStatusSuite) TestEncodeDecodeOkNoReferencesFailed(c *C) { rs.UnpackStatus = "my error" s.testEncodeDecodeOk(c, rs, - "unpack my error", + "unpack my error\n", pktline.FlushString, ) } @@ -165,8 +165,8 @@ func (s *ReportStatusSuite) TestDecodeErrorOneReferenceNoFlush(c *C) { }} s.testDecodeError(c, "missing flush", - "unpack ok", - "ok refs/heads/master", + "unpack ok\n", + "ok refs/heads/master\n", ) } @@ -190,7 +190,7 @@ func (s *ReportStatusSuite) TestDecodeErrorMalformed(c *C) { }} s.testDecodeError(c, "malformed unpack status: unpackok", - "unpackok", + "unpackok\n", pktline.FlushString, ) } @@ -204,7 +204,7 @@ func (s *ReportStatusSuite) TestDecodeErrorMalformed2(c *C) { }} s.testDecodeError(c, "malformed unpack status: UNPACK OK", - "UNPACK OK", + "UNPACK OK\n", pktline.FlushString, ) } @@ -218,8 +218,8 @@ func (s *ReportStatusSuite) TestDecodeErrorMalformedCommandStatus(c *C) { }} s.testDecodeError(c, "malformed command status: ko refs/heads/master", - "unpack ok", - "ko refs/heads/master", + "unpack ok\n", + "ko refs/heads/master\n", pktline.FlushString, ) } @@ -233,8 +233,8 @@ func (s *ReportStatusSuite) TestDecodeErrorMalformedCommandStatus2(c *C) { }} s.testDecodeError(c, "malformed command status: ng refs/heads/master", - "unpack ok", - "ng refs/heads/master", + "unpack ok\n", + "ng refs/heads/master\n", pktline.FlushString, ) } diff --git a/plumbing/protocol/packp/ulreq.go b/plumbing/protocol/packp/ulreq.go index faf0885..7832007 100644 --- a/plumbing/protocol/packp/ulreq.go +++ b/plumbing/protocol/packp/ulreq.go @@ -8,8 +8,6 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability" ) -const DefaultAgent = "go-git/4.x" - // UploadRequest values represent the information transmitted on a // upload-request message. Values from this type are not zero-value // safe, use the New function instead. @@ -97,7 +95,7 @@ func NewUploadRequestFromCapabilities(adv *capability.List) *UploadRequest { } if adv.Supports(capability.Agent) { - r.Capabilities.Set(capability.Agent, DefaultAgent) + r.Capabilities.Set(capability.Agent, capability.DefaultAgent) } return r diff --git a/plumbing/protocol/packp/updreq.go b/plumbing/protocol/packp/updreq.go index 90d6e09..a2deb89 100644 --- a/plumbing/protocol/packp/updreq.go +++ b/plumbing/protocol/packp/updreq.go @@ -2,6 +2,7 @@ package packp import ( "errors" + "io" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability" @@ -20,6 +21,8 @@ type ReferenceUpdateRequest struct { Capabilities *capability.List Commands []*Command Shallow *plumbing.Hash + // Packfile contains an optional packfile reader. + Packfile io.ReadCloser } // New returns a pointer to a new ReferenceUpdateRequest value. @@ -30,6 +33,37 @@ func NewReferenceUpdateRequest() *ReferenceUpdateRequest { } } +// NewReferenceUpdateRequestFromCapabilities returns a pointer to a new +// ReferenceUpdateRequest value, the request capabilities are filled with the +// most optimal ones, based on the adv value (advertised capabilities), the +// ReferenceUpdateRequest contains no commands +// +// It does set the following capabilities: +// - agent +// - report-status +// - ofs-delta +// - ref-delta +// It leaves up to the user to add the following capabilities later: +// - atomic +// - ofs-delta +// - side-band +// - side-band-64k +// - quiet +// - push-cert +func NewReferenceUpdateRequestFromCapabilities(adv *capability.List) *ReferenceUpdateRequest { + r := NewReferenceUpdateRequest() + + if adv.Supports(capability.Agent) { + r.Capabilities.Set(capability.Agent, capability.DefaultAgent) + } + + if adv.Supports(capability.ReportStatus) { + r.Capabilities.Set(capability.ReportStatus) + } + + return r +} + func (r *ReferenceUpdateRequest) validate() error { if len(r.Commands) == 0 { return ErrEmptyCommands diff --git a/plumbing/protocol/packp/updreq_decode.go b/plumbing/protocol/packp/updreq_decode.go index 0740871..2ebe2a3 100644 --- a/plumbing/protocol/packp/updreq_decode.go +++ b/plumbing/protocol/packp/updreq_decode.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/format/pktline" @@ -76,23 +77,32 @@ func errMalformedCommand(err error) error { // Decode reads the next update-request message form the reader and wr func (req *ReferenceUpdateRequest) Decode(r io.Reader) error { - d := &updReqDecoder{s: pktline.NewScanner(r)} + var rc io.ReadCloser + var ok bool + rc, ok = r.(io.ReadCloser) + if !ok { + rc = ioutil.NopCloser(r) + } + + d := &updReqDecoder{r: rc, s: pktline.NewScanner(r)} return d.Decode(req) } type updReqDecoder struct { - s *pktline.Scanner - r *ReferenceUpdateRequest + r io.ReadCloser + s *pktline.Scanner + req *ReferenceUpdateRequest } -func (d *updReqDecoder) Decode(r *ReferenceUpdateRequest) error { - d.r = r +func (d *updReqDecoder) Decode(req *ReferenceUpdateRequest) error { + d.req = req funcs := []func() error{ d.scanLine, d.decodeShallow, d.decodeCommandAndCapabilities, d.decodeCommands, - r.validate, + d.setPackfile, + req.validate, } for _, f := range funcs { @@ -132,7 +142,7 @@ func (d *updReqDecoder) decodeShallow() error { return d.scanErrorOr(errNoCommands) } - d.r.Shallow = &h + d.req.Shallow = &h return nil } @@ -149,7 +159,7 @@ func (d *updReqDecoder) decodeCommands() error { return err } - d.r.Commands = append(d.r.Commands, c) + d.req.Commands = append(d.req.Commands, c) if ok := d.s.Scan(); !ok { return d.s.Err() @@ -173,9 +183,9 @@ func (d *updReqDecoder) decodeCommandAndCapabilities() error { return err } - d.r.Commands = append(d.r.Commands, cmd) + d.req.Commands = append(d.req.Commands, cmd) - if err := d.r.Capabilities.Decode(b[i+1:]); err != nil { + if err := d.req.Capabilities.Decode(b[i+1:]); err != nil { return err } @@ -186,6 +196,12 @@ func (d *updReqDecoder) decodeCommandAndCapabilities() error { return nil } +func (d *updReqDecoder) setPackfile() error { + d.req.Packfile = d.r + + return nil +} + func parseCommand(b []byte) (*Command, error) { if len(b) < minCommandLength { return nil, errInvalidCommandLineLength(len(b)) diff --git a/plumbing/protocol/packp/updreq_decode_test.go b/plumbing/protocol/packp/updreq_decode_test.go index 66d9180..6cbab2b 100644 --- a/plumbing/protocol/packp/updreq_decode_test.go +++ b/plumbing/protocol/packp/updreq_decode_test.go @@ -3,6 +3,7 @@ package packp import ( "bytes" "io" + "io/ioutil" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/format/pktline" @@ -156,13 +157,14 @@ func (s *UpdReqDecodeSuite) TestOneUpdateCommand(c *C) { expected.Commands = []*Command{ {Name: name, Old: hash1, New: hash2}, } + expected.Packfile = ioutil.NopCloser(bytes.NewReader([]byte{})) payloads := []string{ "1ecf0ef2c2dffb796033e5a02219af86ec6584e5 2ecf0ef2c2dffb796033e5a02219af86ec6584e5 myref\x00", pktline.FlushString, } - c.Assert(s.testDecodeOK(c, payloads), DeepEquals, expected) + s.testDecodeOkExpected(c, expected, payloads) } func (s *UpdReqDecodeSuite) TestMultipleCommands(c *C) { @@ -175,6 +177,7 @@ func (s *UpdReqDecodeSuite) TestMultipleCommands(c *C) { {Name: "myref2", Old: plumbing.ZeroHash, New: hash2}, {Name: "myref3", Old: hash1, New: plumbing.ZeroHash}, } + expected.Packfile = ioutil.NopCloser(bytes.NewReader([]byte{})) payloads := []string{ "1ecf0ef2c2dffb796033e5a02219af86ec6584e5 2ecf0ef2c2dffb796033e5a02219af86ec6584e5 myref1\x00", @@ -183,10 +186,7 @@ func (s *UpdReqDecodeSuite) TestMultipleCommands(c *C) { pktline.FlushString, } - c.Assert(s.testDecodeOK(c, payloads).Commands, DeepEquals, expected.Commands) - c.Assert(s.testDecodeOK(c, payloads).Shallow, DeepEquals, expected.Shallow) - c.Assert(s.testDecodeOK(c, payloads).Capabilities, DeepEquals, expected.Capabilities) - c.Assert(s.testDecodeOK(c, payloads), DeepEquals, expected) + s.testDecodeOkExpected(c, expected, payloads) } func (s *UpdReqDecodeSuite) TestMultipleCommandsAndCapabilities(c *C) { @@ -200,6 +200,7 @@ func (s *UpdReqDecodeSuite) TestMultipleCommandsAndCapabilities(c *C) { {Name: "myref3", Old: hash1, New: plumbing.ZeroHash}, } expected.Capabilities.Add("shallow") + expected.Packfile = ioutil.NopCloser(bytes.NewReader([]byte{})) payloads := []string{ "1ecf0ef2c2dffb796033e5a02219af86ec6584e5 2ecf0ef2c2dffb796033e5a02219af86ec6584e5 myref1\x00shallow", @@ -208,7 +209,7 @@ func (s *UpdReqDecodeSuite) TestMultipleCommandsAndCapabilities(c *C) { pktline.FlushString, } - c.Assert(s.testDecodeOK(c, payloads), DeepEquals, expected) + s.testDecodeOkExpected(c, expected, payloads) } func (s *UpdReqDecodeSuite) TestMultipleCommandsAndCapabilitiesShallow(c *C) { @@ -223,6 +224,7 @@ func (s *UpdReqDecodeSuite) TestMultipleCommandsAndCapabilitiesShallow(c *C) { } expected.Capabilities.Add("shallow") expected.Shallow = &hash1 + expected.Packfile = ioutil.NopCloser(bytes.NewReader([]byte{})) payloads := []string{ "shallow 1ecf0ef2c2dffb796033e5a02219af86ec6584e5", @@ -232,7 +234,31 @@ func (s *UpdReqDecodeSuite) TestMultipleCommandsAndCapabilitiesShallow(c *C) { pktline.FlushString, } - c.Assert(s.testDecodeOK(c, payloads), DeepEquals, expected) + s.testDecodeOkExpected(c, expected, payloads) +} + +func (s *UpdReqDecodeSuite) TestWithPackfile(c *C) { + hash1 := plumbing.NewHash("1ecf0ef2c2dffb796033e5a02219af86ec6584e5") + hash2 := plumbing.NewHash("2ecf0ef2c2dffb796033e5a02219af86ec6584e5") + name := "myref" + + expected := NewReferenceUpdateRequest() + expected.Commands = []*Command{ + {Name: name, Old: hash1, New: hash2}, + } + packfileContent := []byte("PACKabc") + expected.Packfile = ioutil.NopCloser(bytes.NewReader(packfileContent)) + + payloads := []string{ + "1ecf0ef2c2dffb796033e5a02219af86ec6584e5 2ecf0ef2c2dffb796033e5a02219af86ec6584e5 myref\x00", + pktline.FlushString, + } + var buf bytes.Buffer + e := pktline.NewEncoder(&buf) + c.Assert(e.EncodeString(payloads...), IsNil) + buf.Write(packfileContent) + + s.testDecodeOkRaw(c, expected, buf.Bytes()) } func (s *UpdReqDecodeSuite) testDecoderErrorMatches(c *C, input io.Reader, pattern string) { @@ -251,3 +277,32 @@ func (s *UpdReqDecodeSuite) testDecodeOK(c *C, payloads []string) *ReferenceUpda return r } + +func (s *UpdReqDecodeSuite) testDecodeOkRaw(c *C, expected *ReferenceUpdateRequest, raw []byte) { + req := NewReferenceUpdateRequest() + c.Assert(req.Decode(bytes.NewBuffer(raw)), IsNil) + c.Assert(req.Packfile, NotNil) + s.compareReaders(c, req.Packfile, expected.Packfile) + req.Packfile = nil + expected.Packfile = nil + c.Assert(req, DeepEquals, expected) +} + +func (s *UpdReqDecodeSuite) testDecodeOkExpected(c *C, expected *ReferenceUpdateRequest, payloads []string) { + req := s.testDecodeOK(c, payloads) + c.Assert(req.Packfile, NotNil) + s.compareReaders(c, req.Packfile, expected.Packfile) + req.Packfile = nil + expected.Packfile = nil + c.Assert(req, DeepEquals, expected) +} + +func (s *UpdReqDecodeSuite) compareReaders(c *C, a io.ReadCloser, b io.ReadCloser) { + pba, err := ioutil.ReadAll(a) + c.Assert(err, IsNil) + c.Assert(a.Close(), IsNil) + pbb, err := ioutil.ReadAll(b) + c.Assert(err, IsNil) + c.Assert(b.Close(), IsNil) + c.Assert(pba, DeepEquals, pbb) +} diff --git a/plumbing/protocol/packp/updreq_encode.go b/plumbing/protocol/packp/updreq_encode.go index b2b7944..44c0573 100644 --- a/plumbing/protocol/packp/updreq_encode.go +++ b/plumbing/protocol/packp/updreq_encode.go @@ -29,6 +29,14 @@ func (r *ReferenceUpdateRequest) Encode(w io.Writer) error { return err } + if r.Packfile != nil { + if _, err := io.Copy(w, r.Packfile); err != nil { + return err + } + + return r.Packfile.Close() + } + return nil } diff --git a/plumbing/protocol/packp/updreq_encode_test.go b/plumbing/protocol/packp/updreq_encode_test.go index 47958fd..14f9975 100644 --- a/plumbing/protocol/packp/updreq_encode_test.go +++ b/plumbing/protocol/packp/updreq_encode_test.go @@ -7,6 +7,7 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing/format/pktline" . "gopkg.in/check.v1" + "io/ioutil" ) type UpdReqEncodeSuite struct{} @@ -117,3 +118,27 @@ func (s *UpdReqEncodeSuite) TestMultipleCommandsAndCapabilitiesShallow(c *C) { s.testEncode(c, r, expected) } + +func (s *UpdReqEncodeSuite) TestWithPackfile(c *C) { + hash1 := plumbing.NewHash("1ecf0ef2c2dffb796033e5a02219af86ec6584e5") + hash2 := plumbing.NewHash("2ecf0ef2c2dffb796033e5a02219af86ec6584e5") + name := "myref" + + packfileContent := []byte("PACKabc") + packfileReader := bytes.NewReader(packfileContent) + packfileReadCloser := ioutil.NopCloser(packfileReader) + + r := NewReferenceUpdateRequest() + r.Commands = []*Command{ + {Name: name, Old: hash1, New: hash2}, + } + r.Packfile = packfileReadCloser + + expected := pktlines(c, + "1ecf0ef2c2dffb796033e5a02219af86ec6584e5 2ecf0ef2c2dffb796033e5a02219af86ec6584e5 myref\x00", + pktline.FlushString, + ) + expected = append(expected, packfileContent...) + + s.testEncode(c, r, expected) +} diff --git a/plumbing/protocol/packp/updreq_test.go b/plumbing/protocol/packp/updreq_test.go new file mode 100644 index 0000000..2412fbf --- /dev/null +++ b/plumbing/protocol/packp/updreq_test.go @@ -0,0 +1,39 @@ +package packp + +import ( + "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability" + + . "gopkg.in/check.v1" +) + +type UpdReqSuite struct{} + +var _ = Suite(&UpdReqSuite{}) + +func (s *UpdReqSuite) TestNewReferenceUpdateRequestFromCapabilities(c *C) { + cap := capability.NewList() + cap.Set(capability.Sideband) + cap.Set(capability.Sideband64k) + cap.Set(capability.Quiet) + cap.Set(capability.ReportStatus) + cap.Set(capability.DeleteRefs) + cap.Set(capability.PushCert, "foo") + cap.Set(capability.Atomic) + cap.Set(capability.Agent, "foo") + + r := NewReferenceUpdateRequestFromCapabilities(cap) + c.Assert(r.Capabilities.String(), Equals, + "agent=go-git/4.x report-status", + ) + + cap = capability.NewList() + cap.Set(capability.Agent, "foo") + + r = NewReferenceUpdateRequestFromCapabilities(cap) + c.Assert(r.Capabilities.String(), Equals, "agent=go-git/4.x") + + cap = capability.NewList() + + r = NewReferenceUpdateRequestFromCapabilities(cap) + c.Assert(r.Capabilities.String(), Equals, "") +} diff --git a/plumbing/transport/common.go b/plumbing/transport/common.go index 41813bf..83fd5a3 100644 --- a/plumbing/transport/common.go +++ b/plumbing/transport/common.go @@ -49,6 +49,8 @@ type Session interface { SetAuth(auth AuthMethod) error // AdvertisedReferences retrieves the advertised references for a // repository. + // If the repository does not exist, returns ErrRepositoryNotFound. + // If the repository exists, but is empty, returns ErrEmptyRemoteRepository. AdvertisedReferences() (*packp.AdvRefs, error) io.Closer } @@ -75,10 +77,9 @@ type FetchPackSession interface { // In that order. type SendPackSession interface { Session - // UpdateReferences sends an update references request and returns a - // writer to be used for packfile writing. - //TODO: Complete signature. - SendPack() (io.WriteCloser, error) + // UpdateReferences sends an update references request and a packfile + // reader and returns a ReportStatus and error. + SendPack(*packp.ReferenceUpdateRequest) (*packp.ReportStatus, error) } type Endpoint url.URL diff --git a/plumbing/transport/file/common.go b/plumbing/transport/file/common.go index 8697121..e7d18b2 100644 --- a/plumbing/transport/file/common.go +++ b/plumbing/transport/file/common.go @@ -69,6 +69,10 @@ func (c *command) StdoutPipe() (io.Reader, error) { // Close waits for the command to exit. func (c *command) Close() error { + if c.closed { + return nil + } + return c.cmd.Process.Kill() } diff --git a/plumbing/transport/file/common_test.go b/plumbing/transport/file/common_test.go index 94ca4c9..220df3d 100644 --- a/plumbing/transport/file/common_test.go +++ b/plumbing/transport/file/common_test.go @@ -1,9 +1,40 @@ package file import ( + "fmt" + "io" + "os" + "strings" "testing" + "gopkg.in/src-d/go-git.v4/plumbing/transport" + . "gopkg.in/check.v1" ) func Test(t *testing.T) { TestingT(t) } + +const bareConfig = `[core] +repositoryformatversion = 0 +filemode = true +bare = true` + +func prepareRepo(c *C, path string) transport.Endpoint { + url := fmt.Sprintf("file://%s", path) + ep, err := transport.NewEndpoint(url) + c.Assert(err, IsNil) + + // git-receive-pack refuses to update refs/heads/master on non-bare repo + // so we ensure bare repo config. + config := fmt.Sprintf("%s/config", path) + if _, err := os.Stat(config); err == nil { + f, err := os.OpenFile(config, os.O_TRUNC|os.O_WRONLY, 0) + c.Assert(err, IsNil) + content := strings.NewReader(bareConfig) + _, err = io.Copy(f, content) + c.Assert(err, IsNil) + c.Assert(f.Close(), IsNil) + } + + return ep +} diff --git a/plumbing/transport/file/fetch_pack_test.go b/plumbing/transport/file/fetch_pack_test.go index 7a23285..25e3fef 100644 --- a/plumbing/transport/file/fetch_pack_test.go +++ b/plumbing/transport/file/fetch_pack_test.go @@ -76,3 +76,11 @@ func (s *FetchPackSuite) TestMalformedInputNoErrors(c *C) { c.Assert(err, NotNil) c.Assert(ar, IsNil) } + +func (s *FetchPackSuite) TestNonExistentCommand(c *C) { + cmd := "/non-existent-git" + client := NewClient(cmd, cmd) + session, err := client.NewFetchPackSession(s.Endpoint) + c.Assert(err, ErrorMatches, ".*no such file or directory.*") + c.Assert(session, IsNil) +} diff --git a/plumbing/transport/file/send_pack_test.go b/plumbing/transport/file/send_pack_test.go new file mode 100644 index 0000000..fc7ea35 --- /dev/null +++ b/plumbing/transport/file/send_pack_test.go @@ -0,0 +1,81 @@ +package file + +import ( + "os" + "os/exec" + + "gopkg.in/src-d/go-git.v4/fixtures" + "gopkg.in/src-d/go-git.v4/plumbing/transport/test" + + . "gopkg.in/check.v1" +) + +type SendPackSuite struct { + fixtures.Suite + test.SendPackSuite +} + +var _ = Suite(&SendPackSuite{}) + +func (s *SendPackSuite) SetUpSuite(c *C) { + s.Suite.SetUpSuite(c) + + if err := exec.Command("git", "--version").Run(); err != nil { + c.Skip("git command not found") + } + + s.SendPackSuite.Client = DefaultClient +} + +func (s *SendPackSuite) SetUpTest(c *C) { + fixture := fixtures.Basic().One() + path := fixture.DotGit().Base() + s.Endpoint = prepareRepo(c, path) + + fixture = fixtures.ByTag("empty").One() + path = fixture.DotGit().Base() + s.EmptyEndpoint = prepareRepo(c, path) + + s.NonExistentEndpoint = prepareRepo(c, "/non-existent") +} + +func (s *SendPackSuite) TearDownTest(c *C) { + s.Suite.TearDownSuite(c) +} + +// TODO: fix test +func (s *SendPackSuite) TestCommandNoOutput(c *C) { + c.Skip("failing test") + + if _, err := os.Stat("/bin/true"); os.IsNotExist(err) { + c.Skip("/bin/true not found") + } + + client := NewClient("true", "true") + session, err := client.NewSendPackSession(s.Endpoint) + c.Assert(err, IsNil) + ar, err := session.AdvertisedReferences() + c.Assert(err, IsNil) + c.Assert(ar, IsNil) +} + +func (s *SendPackSuite) TestMalformedInputNoErrors(c *C) { + if _, err := os.Stat("/usr/bin/yes"); os.IsNotExist(err) { + c.Skip("/usr/bin/yes not found") + } + + client := NewClient("yes", "yes") + session, err := client.NewSendPackSession(s.Endpoint) + c.Assert(err, IsNil) + ar, err := session.AdvertisedReferences() + c.Assert(err, NotNil) + c.Assert(ar, IsNil) +} + +func (s *SendPackSuite) TestNonExistentCommand(c *C) { + cmd := "/non-existent-git" + client := NewClient(cmd, cmd) + session, err := client.NewSendPackSession(s.Endpoint) + c.Assert(err, ErrorMatches, ".*no such file or directory.*") + c.Assert(session, IsNil) +} diff --git a/plumbing/transport/http/send_pack.go b/plumbing/transport/http/send_pack.go index 5e3b2bb..43464ae 100644 --- a/plumbing/transport/http/send_pack.go +++ b/plumbing/transport/http/send_pack.go @@ -2,7 +2,6 @@ package http import ( "errors" - "io" "net/http" "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp" @@ -24,6 +23,8 @@ func (s *sendPackSession) AdvertisedReferences() (*packp.AdvRefs, error) { return nil, errSendPackNotSupported } -func (s *sendPackSession) SendPack() (io.WriteCloser, error) { +func (s *sendPackSession) SendPack(*packp.ReferenceUpdateRequest) ( + *packp.ReportStatus, error) { + return nil, errSendPackNotSupported } diff --git a/plumbing/transport/internal/common/common.go b/plumbing/transport/internal/common/common.go index 12f4995..f0e1691 100644 --- a/plumbing/transport/internal/common/common.go +++ b/plumbing/transport/internal/common/common.go @@ -15,6 +15,7 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing/format/pktline" "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp" + "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability" "gopkg.in/src-d/go-git.v4/plumbing/transport" "gopkg.in/src-d/go-git.v4/utils/ioutil" ) @@ -91,7 +92,7 @@ func (c *client) NewFetchPackSession(ep transport.Endpoint) ( func (c *client) NewSendPackSession(ep transport.Endpoint) ( transport.SendPackSession, error) { - return nil, errors.New("git send-pack not supported") + return c.newSession(transport.ReceivePackServiceName, ep) } type session struct { @@ -99,10 +100,11 @@ type session struct { Stdout io.Reader Command Command - advRefs *packp.AdvRefs - packRun bool - finished bool - errLines chan string + isReceivePack bool + advRefs *packp.AdvRefs + packRun bool + finished bool + errLines chan string } func (c *client) newSession(s string, ep transport.Endpoint) (*session, error) { @@ -140,10 +142,11 @@ func (c *client) newSession(s string, ep transport.Endpoint) (*session, error) { }() return &session{ - Stdin: stdin, - Stdout: stdout, - Command: cmd, - errLines: errLines, + Stdin: stdin, + Stdout: stdout, + Command: cmd, + errLines: errLines, + isReceivePack: s == transport.ReceivePackServiceName, }, nil } @@ -174,6 +177,11 @@ func (s *session) AdvertisedReferences() (*packp.AdvRefs, error) { // advertised-references message. But valid. That is, it // includes at least a flush. if err == packp.ErrEmptyAdvRefs { + // Empty repositories are valid for git-receive-pack. + if s.isReceivePack { + return ar, nil + } + if err := s.finish(); err != nil { return nil, err } @@ -229,6 +237,35 @@ func (s *session) FetchPack(req *packp.UploadPackRequest) (*packp.UploadPackResp return DecodeUploadPackResponse(rc, req) } +func (s *session) SendPack(req *packp.ReferenceUpdateRequest) (*packp.ReportStatus, error) { + if _, err := s.AdvertisedReferences(); err != nil { + return nil, err + } + + s.packRun = true + + if err := req.Encode(s.Stdin); err != nil { + return nil, err + } + + if !req.Capabilities.Supports(capability.ReportStatus) { + // If we have neither report-status or sideband, we can only + // check return value error. + return nil, s.Command.Wait() + } + + report := packp.NewReportStatus() + if err := report.Decode(s.Stdout); err != nil { + return nil, err + } + + if !report.Ok() { + return report, fmt.Errorf("report status: %s", report.UnpackStatus) + } + + return report, s.Command.Wait() +} + func (s *session) finish() error { if s.finished { return nil diff --git a/plumbing/transport/test/common.go b/plumbing/transport/test/fetch_pack.go index 2984154..2984154 100644 --- a/plumbing/transport/test/common.go +++ b/plumbing/transport/test/fetch_pack.go diff --git a/plumbing/transport/test/send_pack.go b/plumbing/transport/test/send_pack.go new file mode 100644 index 0000000..94a1150 --- /dev/null +++ b/plumbing/transport/test/send_pack.go @@ -0,0 +1,285 @@ +// Package test implements common test suite for different transport +// implementations. +// +package test + +import ( + "bytes" + "io" + "io/ioutil" + + "gopkg.in/src-d/go-git.v4/fixtures" + "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/format/packfile" + "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp" + "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability" + "gopkg.in/src-d/go-git.v4/plumbing/transport" + "gopkg.in/src-d/go-git.v4/storage/memory" + + . "gopkg.in/check.v1" +) + +type SendPackSuite struct { + Endpoint transport.Endpoint + EmptyEndpoint transport.Endpoint + NonExistentEndpoint transport.Endpoint + Client transport.Client +} + +func (s *SendPackSuite) TestInfoEmpty(c *C) { + r, err := s.Client.NewSendPackSession(s.EmptyEndpoint) + c.Assert(err, IsNil) + defer func() { c.Assert(r.Close(), IsNil) }() + info, err := r.AdvertisedReferences() + c.Assert(err, IsNil) + c.Assert(info.Head, IsNil) +} + +func (s *SendPackSuite) TestInfoNotExists(c *C) { + r, err := s.Client.NewSendPackSession(s.NonExistentEndpoint) + c.Assert(err, IsNil) + defer func() { c.Assert(r.Close(), IsNil) }() + info, err := r.AdvertisedReferences() + c.Assert(err, Equals, transport.ErrRepositoryNotFound) + c.Assert(info, IsNil) + + r, err = s.Client.NewSendPackSession(s.NonExistentEndpoint) + c.Assert(err, IsNil) + req := packp.NewReferenceUpdateRequest() + req.Commands = []*packp.Command{ + {"master", plumbing.ZeroHash, plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")}, + } + + writer, err := r.SendPack(req) + c.Assert(err, Equals, transport.ErrRepositoryNotFound) + c.Assert(writer, IsNil) +} + +func (s *SendPackSuite) TestCallAdvertisedReferenceTwice(c *C) { + r, err := s.Client.NewSendPackSession(s.Endpoint) + c.Assert(err, IsNil) + ar1, err := r.AdvertisedReferences() + c.Assert(err, IsNil) + c.Assert(ar1, NotNil) + ar2, err := r.AdvertisedReferences() + c.Assert(err, IsNil) + c.Assert(ar2, DeepEquals, ar1) +} + +func (s *SendPackSuite) TestDefaultBranch(c *C) { + r, err := s.Client.NewSendPackSession(s.Endpoint) + c.Assert(err, IsNil) + defer func() { c.Assert(r.Close(), IsNil) }() + + info, err := r.AdvertisedReferences() + c.Assert(err, IsNil) + ref, ok := info.References["refs/heads/master"] + c.Assert(ok, Equals, true) + c.Assert(ref, Equals, fixtures.Basic().One().Head) +} + +func (s *SendPackSuite) TestCapabilities(c *C) { + r, err := s.Client.NewSendPackSession(s.Endpoint) + c.Assert(err, IsNil) + defer func() { c.Assert(r.Close(), IsNil) }() + + info, err := r.AdvertisedReferences() + c.Assert(err, IsNil) + c.Assert(info.Capabilities.Get("agent"), HasLen, 1) +} + +func (s *SendPackSuite) TestFullSendPackOnEmpty(c *C) { + endpoint := s.EmptyEndpoint + full := true + fixture := fixtures.Basic().ByTag("packfile").One() + req := packp.NewReferenceUpdateRequest() + req.Commands = []*packp.Command{ + {"refs/heads/master", plumbing.ZeroHash, fixture.Head}, + } + s.sendPack(c, endpoint, req, fixture, full) + s.checkRemoteHead(c, endpoint, fixture.Head) +} + +func (s *SendPackSuite) TestSendPackOnEmpty(c *C) { + endpoint := s.EmptyEndpoint + full := false + fixture := fixtures.Basic().ByTag("packfile").One() + req := packp.NewReferenceUpdateRequest() + req.Commands = []*packp.Command{ + {"refs/heads/master", plumbing.ZeroHash, fixture.Head}, + } + s.sendPack(c, endpoint, req, fixture, full) + s.checkRemoteHead(c, endpoint, fixture.Head) +} + +func (s *SendPackSuite) TestSendPackOnEmptyWithReportStatus(c *C) { + endpoint := s.EmptyEndpoint + full := false + fixture := fixtures.Basic().ByTag("packfile").One() + req := packp.NewReferenceUpdateRequest() + req.Commands = []*packp.Command{ + {"refs/heads/master", plumbing.ZeroHash, fixture.Head}, + } + req.Capabilities.Set(capability.ReportStatus) + s.sendPack(c, endpoint, req, fixture, full) + s.checkRemoteHead(c, endpoint, fixture.Head) +} + +func (s *SendPackSuite) TestFullSendPackOnNonEmpty(c *C) { + endpoint := s.Endpoint + full := true + fixture := fixtures.Basic().ByTag("packfile").One() + req := packp.NewReferenceUpdateRequest() + req.Commands = []*packp.Command{ + {"refs/heads/master", plumbing.ZeroHash, fixture.Head}, + } + s.sendPack(c, endpoint, req, fixture, full) + s.checkRemoteHead(c, endpoint, fixture.Head) +} + +func (s *SendPackSuite) TestSendPackOnNonEmpty(c *C) { + endpoint := s.Endpoint + full := false + fixture := fixtures.Basic().ByTag("packfile").One() + req := packp.NewReferenceUpdateRequest() + req.Commands = []*packp.Command{ + {"refs/heads/master", plumbing.ZeroHash, fixture.Head}, + } + s.sendPack(c, endpoint, req, fixture, full) + s.checkRemoteHead(c, endpoint, fixture.Head) +} + +func (s *SendPackSuite) TestSendPackOnNonEmptyWithReportStatus(c *C) { + endpoint := s.Endpoint + full := false + fixture := fixtures.Basic().ByTag("packfile").One() + req := packp.NewReferenceUpdateRequest() + req.Commands = []*packp.Command{ + {"refs/heads/master", plumbing.ZeroHash, fixture.Head}, + } + req.Capabilities.Set(capability.ReportStatus) + + s.sendPack(c, endpoint, req, fixture, full) + s.checkRemoteHead(c, endpoint, fixture.Head) +} + +func (s *SendPackSuite) sendPack(c *C, ep transport.Endpoint, + req *packp.ReferenceUpdateRequest, fixture *fixtures.Fixture, + callAdvertisedReferences bool) { + + url := "" + if fixture != nil { + url = fixture.URL + } + comment := Commentf( + "failed with ep=%s fixture=%s callAdvertisedReferences=%s", + ep.String(), url, callAdvertisedReferences, + ) + + r, err := s.Client.NewSendPackSession(ep) + c.Assert(err, IsNil, comment) + defer func() { c.Assert(r.Close(), IsNil, comment) }() + + if callAdvertisedReferences { + info, err := r.AdvertisedReferences() + c.Assert(err, IsNil, comment) + c.Assert(info, NotNil, comment) + } + + if fixture != nil { + c.Assert(fixture.Packfile(), NotNil) + req.Packfile = fixture.Packfile() + } else { + req.Packfile = s.emptyPackfile() + } + + report, err := r.SendPack(req) + c.Assert(err, IsNil, comment) + if req.Capabilities.Supports(capability.ReportStatus) { + c.Assert(report, NotNil, comment) + c.Assert(report.Ok(), Equals, true, comment) + } else { + c.Assert(report, IsNil, comment) + } +} + +func (s *SendPackSuite) checkRemoteHead(c *C, ep transport.Endpoint, head plumbing.Hash) { + s.checkRemoteReference(c, ep, "refs/heads/master", head) +} + +func (s *SendPackSuite) checkRemoteReference(c *C, ep transport.Endpoint, + refName string, head plumbing.Hash) { + + r, err := s.Client.NewFetchPackSession(ep) + c.Assert(err, IsNil) + defer func() { c.Assert(r.Close(), IsNil) }() + ar, err := r.AdvertisedReferences() + c.Assert(err, IsNil, Commentf("endpoint: %s", ep.String())) + ref, ok := ar.References[refName] + if head == plumbing.ZeroHash { + c.Assert(ok, Equals, false) + } else { + c.Assert(ok, Equals, true) + c.Assert(ref, DeepEquals, head) + } +} + +func (s *SendPackSuite) TestSendPackAddDeleteReference(c *C) { + s.testSendPackAddReference(c) + s.testSendPackDeleteReference(c) +} + +func (s *SendPackSuite) testSendPackAddReference(c *C) { + r, err := s.Client.NewSendPackSession(s.Endpoint) + c.Assert(err, IsNil) + defer func() { c.Assert(r.Close(), IsNil) }() + + fixture := fixtures.Basic().ByTag("packfile").One() + + ar, err := r.AdvertisedReferences() + c.Assert(err, IsNil) + + req := packp.NewReferenceUpdateRequest() + req.Commands = []*packp.Command{ + {"refs/heads/newbranch", plumbing.ZeroHash, fixture.Head}, + } + if ar.Capabilities.Supports(capability.ReportStatus) { + req.Capabilities.Set(capability.ReportStatus) + } + + s.sendPack(c, s.Endpoint, req, nil, false) + s.checkRemoteReference(c, s.Endpoint, "refs/heads/newbranch", fixture.Head) +} + +func (s *SendPackSuite) testSendPackDeleteReference(c *C) { + r, err := s.Client.NewSendPackSession(s.Endpoint) + c.Assert(err, IsNil) + defer func() { c.Assert(r.Close(), IsNil) }() + + fixture := fixtures.Basic().ByTag("packfile").One() + + ar, err := r.AdvertisedReferences() + c.Assert(err, IsNil) + + req := packp.NewReferenceUpdateRequest() + req.Commands = []*packp.Command{ + {"refs/heads/newbranch", fixture.Head, plumbing.ZeroHash}, + } + if ar.Capabilities.Supports(capability.ReportStatus) { + req.Capabilities.Set(capability.ReportStatus) + } + + s.sendPack(c, s.Endpoint, req, nil, false) + s.checkRemoteReference(c, s.Endpoint, "refs/heads/newbranch", plumbing.ZeroHash) +} + +func (s *SendPackSuite) emptyPackfile() io.ReadCloser { + var buf bytes.Buffer + e := packfile.NewEncoder(&buf, memory.NewStorage()) + _, err := e.Encode(nil) + if err != nil { + panic(err) + } + + return ioutil.NopCloser(&buf) +} |