# Cover returned fetch-pack reader interruptions

`c676521`→[main](/content/gh/entireio/git-sync/commits/main/index.html)·Soph·3mo ago·2 files·+125 added/-1 removed

## Sessions

15bb3382b7ebView transcript

## Changes

2

- docs
  - Mrewrite-issue-list.md+2/-1
- internal/gitproto
  - Mfetch_test.go+123

```
163 unmodified lines
164
165
166
167
167
168
169
170
171

163 unmodified lines

- `gitproto.PushPack` now also has direct closure coverage for cancellation, server-side receive-pack errors, and success.
- `gitproto` fetch tests now verify response-body closure symmetry for both v1 and v2 decode-failure paths.
- `fetchToStoreV2` now also has direct cancellation and decode-failure cleanup coverage.
- This still wants a fuller close-audit around lower-level transport interruption paths before it should be considered fully done.
- Returned `fetchPackV1` and `fetchPackV2` readers now also have direct interruption/close coverage after valid startup.
- The remaining gap is mostly real-network behavior that is not easily modeled in unit tests rather than an obvious ownership hole in current strategy/protocol code.

### 6. Protocol v2 tag fetches request `include-tag` without capability gating
```

Mdocs/rewrite-issue-list.md+2/-1

```
10 unmodified lines

11
12
13
14
15
16
17
174 unmodified lines

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
366 unmodified lines

585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
30 unmodified lines

666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727

10 unmodified lines

git "github.com/go-git/go-git/v6"
	"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/protocol/packp/sideband"
174 unmodified lines

return nil
}

type interruptedBody struct {
	data   []byte
	err    error
	offset int
	closed bool
}

func (b *interruptedBody) Read(p []byte) (int, error) {
	if b.offset < len(b.data) {
		n := copy(p, b.data[b.offset:])
		b.offset += n
		return n, nil
	}
	return 0, b.err
}

func (b *interruptedBody) Close() error {
	b.closed = true
	return nil
}

func TestDecodeV2LSRefs(t *testing.T) {
	// Build a valid ls-refs response:
	// Each line: "<hash> <refname>\n"
366 unmodified lines

}
}

func TestFetchPackV1ReturnedReaderClosesBodyOnInterruption(t *testing.T) {
	ep, err := transport.NewEndpoint("https://example.com/repo.git")
	if err != nil {
		t.Fatalf("parse endpoint: %v", err)
	}
	body := &interruptedBody{
		data: []byte("0008NAK\nPACK"),
		err:  io.ErrUnexpectedEOF,
	}
	conn := NewConn(ep, "source", nil, roundTripperFunc(func(req *http.Request) (*http.Response, error) {
		return &http.Response{
			StatusCode: http.StatusOK,
			Request:    req,
			Body:       body,
		}, nil
	}))

adv := packp.NewAdvRefs()
	desired := map[plumbing.ReferenceName]DesiredRef{
		plumbing.NewBranchReferenceName("main"): {
			SourceRef:  plumbing.NewBranchReferenceName("main"),
			TargetRef:  plumbing.NewBranchReferenceName("main"),
			SourceHash: plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
		},
	}

rc, err := fetchPackV1(context.Background(), conn, adv, desired, nil)
	if err != nil {
		t.Fatalf("fetchPackV1: %v", err)
	}
	data, err := io.ReadAll(rc)
	if len(data) == 0 {
		t.Fatal("expected partial pack data before interruption")
	}
	if err == nil {
		t.Fatal("expected interrupted read error")
	}
	if closeErr := rc.Close(); closeErr != nil {
		t.Fatalf("close returned reader: %v", closeErr)
	}
	if !body.closed {
		t.Fatal("expected returned reader close to close underlying body")
	}
}

func TestFetchPackV2ClosesBodyOnDecodeError(t *testing.T) {
	ep, err := transport.NewEndpoint("https://example.com/repo.git")
	if err != nil {
		t.Fatalf("parse endpoint: %v", err)
	}
	// Additional test implementation
	30 unmodified lines
	
	}

func TestFetchPackV2ReturnedReaderClosesBodyOnInterruption(t *testing.T) {
	ep, err := transport.NewEndpoint("https://example.com/repo.git")
	if err != nil {
		t.Fatalf("parse endpoint: %v", err)
	}
	var wire bytes.Buffer
	if _, err := pktline.WriteString(&wire, "packfile\n"); err != nil {
		t.Fatalf("write packfile header: %v", err)
	}
	if _, err := pktline.Write(&wire, append([]byte{1}, []byte("PACK")...)); err != nil {
		t.Fatalf("write sideband packet: %v", err)
	}
	body := &interruptedBody{
		data: wire.Bytes(),
		err:  io.ErrUnexpectedEOF,
	}
	conn := NewConn(ep, "source", nil, roundTripperFunc(func(req *http.Request) (*http.Response, error) {
		return &http.Response{
			StatusCode: http.StatusOK,
			Request:    req,
			Body:       body,
		}, nil
	}))

caps := &V2Capabilities{
		Caps: map[string]string{
			"fetch": "",
		},
	}
	desired := map[plumbing.ReferenceName]DesiredRef{
		plumbing.NewBranchReferenceName("main"): {
			SourceRef:  plumbing.NewBranchReferenceName("main"),
			TargetRef:  plumbing.NewBranchReferenceName("main"),
			SourceHash: plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
		},
	}

rc, err := fetchPackV2(context.Background(), conn, caps, desired, nil)
	if err != nil {
		t.Fatalf("fetchPackV2: %v", err)
	}
	data, err := io.ReadAll(rc)
	if len(data) == 0 {
		t.Fatal("expected partial pack data before interruption")
	}
	if err == nil {
		t.Fatal("expected interrupted read error")
	}
	if closeErr := rc.Close(); closeErr != nil {
		t.Fatalf("close returned reader: %v", closeErr)
	}
	if !body.closed {
		t.Fatal("expected returned reader close to close underlying body")
	}
}

func TestBuildV1UploadPackBodyEmptyWantSet(t *testing.T) {
	adv := packp.NewAdvRefs()
	_, _, err := buildV1UploadPackBody(adv, nil, nil, false)
