From 18e6f9daa3899318d36b525b068837f49bc2c1cf Mon Sep 17 00:00:00 2001 From: ferhat elmas Date: Wed, 29 Nov 2017 02:24:31 +0100 Subject: all: simplification - no length for map initialization - don't check for boolean/error return - don't format string - use string method of bytes buffer instead of converting bytes to string - use `strings.Contains` instead of `strings.Index` - use `bytes.Equal` instead of `bytes.Compare` --- plumbing/format/config/encoder.go | 8 ++------ plumbing/format/index/decoder.go | 11 ++++------- plumbing/format/objfile/reader.go | 6 +----- plumbing/format/packfile/decoder.go | 2 +- plumbing/format/packfile/delta_index.go | 4 +--- plumbing/format/packfile/object_pack.go | 6 +----- plumbing/format/packfile/patch_delta.go | 7 ++----- plumbing/format/pktline/encoder.go | 12 +++--------- plumbing/protocol/packp/advrefs_encode.go | 2 +- plumbing/protocol/packp/shallowupd.go | 2 +- plumbing/protocol/packp/srvresp.go | 6 +++--- plumbing/protocol/packp/ulreq_encode.go | 4 ++-- plumbing/protocol/packp/uppackreq.go | 2 +- plumbing/protocol/packp/uppackresp_test.go | 4 ++-- plumbing/storer/reference_test.go | 24 ++++-------------------- 15 files changed, 29 insertions(+), 71 deletions(-) (limited to 'plumbing') diff --git a/plumbing/format/config/encoder.go b/plumbing/format/config/encoder.go index 6d17a5a..4eac896 100644 --- a/plumbing/format/config/encoder.go +++ b/plumbing/format/config/encoder.go @@ -53,17 +53,13 @@ func (e *Encoder) encodeSubsection(sectionName string, s *Subsection) error { return err } - if err := e.encodeOptions(s.Options); err != nil { - return err - } - - return nil + return e.encodeOptions(s.Options) } func (e *Encoder) encodeOptions(opts Options) error { for _, o := range opts { pattern := "\t%s = %s\n" - if strings.Index(o.Value, "\\") != -1 { + if strings.Contains(o.Value, "\\") { pattern = "\t%s = %q\n" } diff --git a/plumbing/format/index/decoder.go b/plumbing/format/index/decoder.go index 5bf6a52..1a58128 100644 --- a/plumbing/format/index/decoder.go +++ b/plumbing/format/index/decoder.go @@ -200,11 +200,8 @@ func (d *Decoder) padEntry(idx *Index, e *Entry, read int) error { entrySize := read + len(e.Name) padLen := 8 - entrySize%8 - if _, err := io.CopyN(ioutil.Discard, d.r, int64(padLen)); err != nil { - return err - } - - return nil + _, err := io.CopyN(ioutil.Discard, d.r, int64(padLen)) + return err } func (d *Decoder) readExtensions(idx *Index) error { @@ -288,7 +285,7 @@ func (d *Decoder) readChecksum(expected []byte, alreadyRead [4]byte) error { return err } - if bytes.Compare(h[:], expected) != 0 { + if !bytes.Equal(h[:], expected) { return ErrInvalidChecksum } @@ -407,7 +404,7 @@ func (d *resolveUndoDecoder) Decode(ru *ResolveUndo) error { func (d *resolveUndoDecoder) readEntry() (*ResolveUndoEntry, error) { e := &ResolveUndoEntry{ - Stages: make(map[Stage]plumbing.Hash, 0), + Stages: make(map[Stage]plumbing.Hash), } path, err := binary.ReadUntil(d.r, '\x00') diff --git a/plumbing/format/objfile/reader.go b/plumbing/format/objfile/reader.go index e7e119c..c4467e4 100644 --- a/plumbing/format/objfile/reader.go +++ b/plumbing/format/objfile/reader.go @@ -110,9 +110,5 @@ func (r *Reader) Hash() plumbing.Hash { // Close releases any resources consumed by the Reader. Calling Close does not // close the wrapped io.Reader originally passed to NewReader. func (r *Reader) Close() error { - if err := r.zlib.Close(); err != nil { - return err - } - - return nil + return r.zlib.Close() } diff --git a/plumbing/format/packfile/decoder.go b/plumbing/format/packfile/decoder.go index 26a2a01..ad72ea0 100644 --- a/plumbing/format/packfile/decoder.go +++ b/plumbing/format/packfile/decoder.go @@ -105,7 +105,7 @@ func NewDecoderForType(s *Scanner, o storer.EncodedObjectStorer, o: o, idx: NewIndex(0), - offsetToType: make(map[int64]plumbing.ObjectType, 0), + offsetToType: make(map[int64]plumbing.ObjectType), decoderType: t, }, nil } diff --git a/plumbing/format/packfile/delta_index.go b/plumbing/format/packfile/delta_index.go index 349bedf..07a6112 100644 --- a/plumbing/format/packfile/delta_index.go +++ b/plumbing/format/packfile/delta_index.go @@ -215,12 +215,10 @@ var len8tab = [256]uint8{ } func hashBlock(raw []byte, ptr int) int { - var hash uint32 - // The first 4 steps collapse out into a 4 byte big-endian decode, // with a larger right shift as we combined shift lefts together. // - hash = ((uint32(raw[ptr]) & 0xff) << 24) | + hash := ((uint32(raw[ptr]) & 0xff) << 24) | ((uint32(raw[ptr+1]) & 0xff) << 16) | ((uint32(raw[ptr+2]) & 0xff) << 8) | (uint32(raw[ptr+3]) & 0xff) diff --git a/plumbing/format/packfile/object_pack.go b/plumbing/format/packfile/object_pack.go index 14337d1..e22e783 100644 --- a/plumbing/format/packfile/object_pack.go +++ b/plumbing/format/packfile/object_pack.go @@ -84,11 +84,7 @@ func (o *ObjectToPack) Size() int64 { } func (o *ObjectToPack) IsDelta() bool { - if o.Base != nil { - return true - } - - return false + return o.Base != nil } func (o *ObjectToPack) SetDelta(base *ObjectToPack, delta plumbing.EncodedObject) { diff --git a/plumbing/format/packfile/patch_delta.go b/plumbing/format/packfile/patch_delta.go index 976cabc..c604851 100644 --- a/plumbing/format/packfile/patch_delta.go +++ b/plumbing/format/packfile/patch_delta.go @@ -38,11 +38,8 @@ func ApplyDelta(target, base plumbing.EncodedObject, delta []byte) error { target.SetSize(int64(len(dst))) - if _, err := w.Write(dst); err != nil { - return err - } - - return nil + _, err = w.Write(dst) + return err } var ( diff --git a/plumbing/format/pktline/encoder.go b/plumbing/format/pktline/encoder.go index 797b813..eae85cc 100644 --- a/plumbing/format/pktline/encoder.go +++ b/plumbing/format/pktline/encoder.go @@ -63,21 +63,15 @@ func (e *Encoder) encodeLine(p []byte) error { } if bytes.Equal(p, Flush) { - if err := e.Flush(); err != nil { - return err - } - return nil + return e.Flush() } n := len(p) + 4 if _, err := e.w.Write(asciiHex16(n)); err != nil { return err } - if _, err := e.w.Write(p); err != nil { - return err - } - - return nil + _, err := e.w.Write(p) + return err } // Returns the hexadecimal ascii representation of the 16 less diff --git a/plumbing/protocol/packp/advrefs_encode.go b/plumbing/protocol/packp/advrefs_encode.go index cb93d46..c23e3fe 100644 --- a/plumbing/protocol/packp/advrefs_encode.go +++ b/plumbing/protocol/packp/advrefs_encode.go @@ -133,7 +133,7 @@ func encodeRefs(e *advRefsEncoder) encoderStateFn { continue } - hash, _ := e.data.References[r] + hash := e.data.References[r] if e.err = e.pe.Encodef("%s %s\n", hash.String(), r); e.err != nil { return nil } diff --git a/plumbing/protocol/packp/shallowupd.go b/plumbing/protocol/packp/shallowupd.go index 40f58e8..fce4e3b 100644 --- a/plumbing/protocol/packp/shallowupd.go +++ b/plumbing/protocol/packp/shallowupd.go @@ -32,7 +32,7 @@ func (r *ShallowUpdate) Decode(reader io.Reader) error { err = r.decodeShallowLine(line) case bytes.HasPrefix(line, unshallow): err = r.decodeUnshallowLine(line) - case bytes.Compare(line, pktline.Flush) == 0: + case bytes.Equal(line, pktline.Flush): return nil } diff --git a/plumbing/protocol/packp/srvresp.go b/plumbing/protocol/packp/srvresp.go index 434e8aa..6a91991 100644 --- a/plumbing/protocol/packp/srvresp.go +++ b/plumbing/protocol/packp/srvresp.go @@ -77,7 +77,7 @@ func (r *ServerResponse) stopReading(reader *bufio.Reader) (bool, error) { func (r *ServerResponse) isValidCommand(b []byte) bool { commands := [][]byte{ack, nak} for _, c := range commands { - if bytes.Compare(b, c) == 0 { + if bytes.Equal(b, c) { return true } } @@ -90,11 +90,11 @@ func (r *ServerResponse) decodeLine(line []byte) error { return fmt.Errorf("unexpected flush") } - if bytes.Compare(line[0:3], ack) == 0 { + if bytes.Equal(line[0:3], ack) { return r.decodeACKLine(line) } - if bytes.Compare(line[0:3], nak) == 0 { + if bytes.Equal(line[0:3], nak) { return nil } diff --git a/plumbing/protocol/packp/ulreq_encode.go b/plumbing/protocol/packp/ulreq_encode.go index 4a26e74..89a5986 100644 --- a/plumbing/protocol/packp/ulreq_encode.go +++ b/plumbing/protocol/packp/ulreq_encode.go @@ -70,7 +70,7 @@ func (e *ulReqEncoder) encodeFirstWant() stateFn { func (e *ulReqEncoder) encodeAditionalWants() stateFn { last := e.data.Wants[0] for _, w := range e.data.Wants[1:] { - if bytes.Compare(last[:], w[:]) == 0 { + if bytes.Equal(last[:], w[:]) { continue } @@ -90,7 +90,7 @@ func (e *ulReqEncoder) encodeShallows() stateFn { var last plumbing.Hash for _, s := range e.data.Shallows { - if bytes.Compare(last[:], s[:]) == 0 { + if bytes.Equal(last[:], s[:]) { continue } diff --git a/plumbing/protocol/packp/uppackreq.go b/plumbing/protocol/packp/uppackreq.go index 4bb22d0..1144139 100644 --- a/plumbing/protocol/packp/uppackreq.go +++ b/plumbing/protocol/packp/uppackreq.go @@ -77,7 +77,7 @@ func (u *UploadHaves) Encode(w io.Writer, flush bool) error { var last plumbing.Hash for _, have := range u.Haves { - if bytes.Compare(last[:], have[:]) == 0 { + if bytes.Equal(last[:], have[:]) { continue } diff --git a/plumbing/protocol/packp/uppackresp_test.go b/plumbing/protocol/packp/uppackresp_test.go index 789444d..0d96ce7 100644 --- a/plumbing/protocol/packp/uppackresp_test.go +++ b/plumbing/protocol/packp/uppackresp_test.go @@ -92,7 +92,7 @@ func (s *UploadPackResponseSuite) TestEncodeNAK(c *C) { c.Assert(res.Encode(b), IsNil) expected := "0008NAK\n[PACK]" - c.Assert(string(b.Bytes()), Equals, expected) + c.Assert(b.String(), Equals, expected) } func (s *UploadPackResponseSuite) TestEncodeDepth(c *C) { @@ -107,7 +107,7 @@ func (s *UploadPackResponseSuite) TestEncodeDepth(c *C) { c.Assert(res.Encode(b), IsNil) expected := "00000008NAK\nPACK" - c.Assert(string(b.Bytes()), Equals, expected) + c.Assert(b.String(), Equals, expected) } func (s *UploadPackResponseSuite) TestEncodeMultiACK(c *C) { diff --git a/plumbing/storer/reference_test.go b/plumbing/storer/reference_test.go index 5738eef..490ec95 100644 --- a/plumbing/storer/reference_test.go +++ b/plumbing/storer/reference_test.go @@ -97,11 +97,7 @@ func (s *ReferenceSuite) TestReferenceFilteredIterNext(c *C) { } i := NewReferenceFilteredIter(func(r *plumbing.Reference) bool { - if r.Name() == "bar" { - return true - } - - return false + return r.Name() == "bar" }, NewReferenceSliceIter(slice)) foo, err := i.Next() c.Assert(err, IsNil) @@ -120,11 +116,7 @@ func (s *ReferenceSuite) TestReferenceFilteredIterForEach(c *C) { } i := NewReferenceFilteredIter(func(r *plumbing.Reference) bool { - if r.Name() == "bar" { - return true - } - - return false + return r.Name() == "bar" }, NewReferenceSliceIter(slice)) var count int i.ForEach(func(r *plumbing.Reference) error { @@ -143,11 +135,7 @@ func (s *ReferenceSuite) TestReferenceFilteredIterError(c *C) { } i := NewReferenceFilteredIter(func(r *plumbing.Reference) bool { - if r.Name() == "bar" { - return true - } - - return false + return r.Name() == "bar" }, NewReferenceSliceIter(slice)) var count int exampleErr := errors.New("SOME ERROR") @@ -172,11 +160,7 @@ func (s *ReferenceSuite) TestReferenceFilteredIterForEachStop(c *C) { } i := NewReferenceFilteredIter(func(r *plumbing.Reference) bool { - if r.Name() == "bar" { - return true - } - - return false + return r.Name() == "bar" }, NewReferenceSliceIter(slice)) var count int -- cgit