Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fetch_pack.go « stats « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e6a77a181e2bdd516350faf5d1bb24066c22d310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package stats

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"time"

	"gitlab.com/gitlab-org/gitaly/v16/internal/git/pktline"
)

// FetchPack is used to parse the response body of a git-fetch-pack(1) request.
type FetchPack struct {
	// ReportProgress is an optional callback set by the caller. If set, this function
	// will be called for all received progress packets.
	ReportProgress func([]byte)

	packets           int
	largestPacketSize int
	multiband         map[string]*bandInfo
	nak               time.Time
	responseBody      time.Time
}

// Parse parses the server response from a git-fetch-pack(1) request.
func (f *FetchPack) Parse(body io.Reader) error {
	// Expected response:
	// - "NAK\n"
	// - "<side band byte><pack or progress or error data>
	// - ...
	// - FLUSH

	f.multiband = make(map[string]*bandInfo)
	for _, band := range Bands() {
		f.multiband[band] = &bandInfo{}
	}

	seenFlush := false

	scanner := pktline.NewScanner(body)
	for ; scanner.Scan(); f.packets++ {
		if seenFlush {
			return errors.New("received extra packet after flush")
		}

		if n := len(scanner.Bytes()); n > f.largestPacketSize {
			f.largestPacketSize = n
		}

		data := pktline.Data(scanner.Bytes())

		if f.packets == 0 {
			// We're now looking at the first git packet sent by the server. The
			// server must conclude the ref negotiation. Because we have not sent any
			// "have" messages there is nothing to negotiate and the server should
			// send a single NAK.
			if !bytes.Equal([]byte("NAK\n"), data) {
				return fmt.Errorf("expected NAK, got %q", data)
			}
			f.nak = time.Now()
			continue
		}

		if pktline.IsFlush(scanner.Bytes()) {
			seenFlush = true
			continue
		}

		if len(data) == 0 {
			return errors.New("empty packet in PACK data")
		}

		band, err := bandToHuman(data[0])
		if err != nil {
			return err
		}

		f.multiband[band].consume(data[1:])

		// Print progress data as-is.
		if f.ReportProgress != nil && band == bandProgress {
			f.ReportProgress(data[1:])
		}
	}

	if err := scanner.Err(); err != nil {
		return err
	}
	if !seenFlush {
		return errors.New("POST response did not end in flush")
	}

	f.responseBody = time.Now()
	return nil
}

type bandInfo struct {
	firstPacket time.Time
	size        int64
	packets     int
}

func (bi *bandInfo) consume(data []byte) {
	if bi.packets == 0 {
		bi.firstPacket = time.Now()
	}
	bi.size += int64(len(data))
	bi.packets++
}

const (
	bandPack     = "pack"
	bandProgress = "progress"
	bandError    = "error"
)

// Bands returns the slice of bands which git uses to transport different kinds
// of data in a multiplexed way. See
// https://git-scm.com/docs/protocol-capabilities/2.24.0#_side_band_side_band_64k
// for more information about the different bands.
func Bands() []string { return []string{bandPack, bandProgress, bandError} }

func bandToHuman(b byte) (string, error) {
	bands := Bands()

	// Band index bytes are 1-indexed.
	if b < 1 || int(b) > len(bands) {
		return "", fmt.Errorf("invalid band index: %d", b)
	}

	return bands[b-1], nil
}