fixed a bug with initial ref parsing · Entire

fixed a bug with initial ref parsing

e37fb72→main·

Soph·3mo ago·2 files·+82 added/-3 removed

Sessions

e25750d5a737View transcript

[?
this should work right, or did the syntax change:Codex·GPT-5.4·1 step](/content/gh/entireio/git-sync/session/019d85eb-8e2e-7e83-8d90-640dce44d5f9#timeline-e25750d5a737/index.html)

Changes

2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
153 unmodified lines

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
170
185
186
187
188
174
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

package gitproto

import (
    "bufio"
    "bytes"
    "context"
    "errors"
    "fmt"
    "io"
    "strings"

"github.com/go-git/go-git/v6/plumbing"
    "github.com/go-git/go-git/v6/plumbing/format/pktline"
    "github.com/go-git/go-git/v6/plumbing/protocol/packp"
    "github.com/go-git/go-git/v6/plumbing/transport"
)
153 unmodified lines

}

func decodeV1AdvRefs(data []byte) (*packp.AdvRefs, error) {
    rd := bufio.NewReader(bytes.NewReader(data))
    consumedSmartHeader, err := consumeSmartInfoRefsHeader(rd)
    if err != nil {
        return nil, fmt.Errorf("%w; body-prefix=%q", err, bodyPreview(data))
    }
    if consumedSmartHeader {
        if _, err := rd.Peek(1); errors.Is(err, io.EOF) {
            return nil, transport.ErrEmptyRemoteRepository
        }
    }

ar := packp.NewAdvRefs()
    if err := ar.Decode(bytes.NewReader(data)); err != nil {
        if err := ar.Decode(rd); err != nil {
            if err == packp.ErrEmptyAdvRefs {
                return nil, transport.ErrEmptyRemoteRepository
            }
            return nil, err
        }
        return nil, fmt.Errorf("%w; body-prefix=%q", err, bodyPreview(data))
    }
    return ar, nil
}

func consumeSmartInfoRefsHeader(rd *bufio.Reader) (bool, error) {
    _, prefix, err := pktline.PeekLine(rd)
    if err != nil {
        return false, err
    }
    if !bytes.HasPrefix(prefix, []byte("# service=")) {
        return false, nil
    }

var reply packp.SmartReply
    if err := reply.Decode(rd); err != nil {
        return true, err
    }
    if reply.Service == "" {
        return true, errors.New("missing smart HTTP service name")
    }
    return true, nil
}

func bodyPreview(data []byte) string {
    if len(data) == 0 {
        return ""
    }
    limit := 200
    if len(data) < limit {
        limit = len(data)
    }
    preview := string(data[:limit])
    preview = strings.ReplaceAll(preview, "\n", `\n`)
    preview = strings.ReplaceAll(preview, "\r", `\r`)
    if len(data) > limit {
        preview += "..."
    }
    return preview
}

// RefHashMap converts a reference slice to a map of name→hash.
func RefHashMap(refs []*plumbing.Reference) map[plumbing.ReferenceName]plumbing.Hash {
    out := make(map[plumbing.ReferenceName]plumbing.Hash, len(refs))
}

Minternal/gitproto/refs.go+53/-2

1 unmodified line

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 108 unmodified lines

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 130

import ( "context" "errors" "strings" "testing"

"github.com/go-git/go-git/v6/plumbing" "github.com/go-git/go-git/v6/plumbing/format/pktline" "github.com/go-git/go-git/v6/plumbing/protocol/packp" "github.com/go-git/go-git/v6/plumbing/protocol/packp/capability" "github.com/go-git/go-git/v6/plumbing/transport" )

func TestRefHashMap(t *testing.T) { 108 unmodified lines

} }

func TestDecodeV1AdvRefsSmartEmptyAdvertisement(t *testing.T) { var body strings.Builder if _, err := pktline.Writef(&body, "# service=%s\n", transport.ReceivePackService); err != nil { t.Fatalf("write smart service line: %%v", err) } if err := pktline.WriteFlush(&body); err != nil { t.Fatalf("write smart flush: %%v", err) }

_, err := decodeV1AdvRefs([]byte(body.String())) if !errors.Is(err, transport.ErrEmptyRemoteRepository) { t.Fatalf("expected empty remote repository, got %%v", err) } }

func TestDecodeV1AdvRefsMalformedIncludesPreview(t *testing.T) { _, err := decodeV1AdvRefs([]byte("# service=git-receive-pack")) if err == nil { t.Fatal("expected malformed decode error") } if !strings.Contains(err.Error(), body-prefix="# service=git-receive-pack") { t.Fatalf("expected body preview in error, got %%v", err) } }

func TestListSourceRefsUnsupportedProtocol(t *testing.T) { _, _, err := ListSourceRefs(context.Background(), nil, "v99", nil) if err == nil { t.Fatal("expected error for unsupported protocol mode") } }


Minternal/gitproto/refs\_test.go+29/-1