Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 8cd772a

Browse files
alcortesmmcuadros
authored andcommitted
Fix pktline api (#89)
* Change pktline API so you can add payloads The old pktline API only had one method: New, that receives the payloads and return the new PktLine, that was an io.Reader. This means you have to prepare the contents beforehand, in a [][]byte and then call the ctor to build the pktlines. Now, the construction of the pktlines and the method to add payloads are separated: New() // creates an empty PktLines AddFlush() Add(pp ...[]byte) AddString(pp ...string) and a PktLines has a public member R, which is the io.Reader of the pktlines added. * metalinter * change package name from pktlines to pktline * change package name from pktlines to pktline for true * make pktlines a reader instead of have a reader
1 parent 176cdac commit 8cd772a

File tree

6 files changed

+408
-349
lines changed

6 files changed

+408
-349
lines changed

clients/common/common.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -293,27 +293,26 @@ func (r *GitUploadPackInfo) String() string {
293293
}
294294

295295
func (r *GitUploadPackInfo) Bytes() []byte {
296-
payloads := []string{}
297-
payloads = append(payloads, "# service=git-upload-pack\n")
296+
p := pktline.New()
297+
_ = p.AddString("# service=git-upload-pack\n")
298298
// inserting a flush-pkt here violates the protocol spec, but some
299299
// servers do it, like Github.com
300-
payloads = append(payloads, "")
300+
p.AddFlush()
301301

302302
firstLine := fmt.Sprintf("%s HEAD\x00%s\n", r.Head().Hash(), r.Capabilities.String())
303-
payloads = append(payloads, firstLine)
303+
_ = p.AddString(firstLine)
304304

305305
for _, ref := range r.Refs {
306306
if ref.Type() != core.HashReference {
307307
continue
308308
}
309309

310310
ref := fmt.Sprintf("%s %s\n", ref.Hash(), ref.Name())
311-
payloads = append(payloads, ref)
311+
_ = p.AddString(ref)
312312
}
313313

314-
payloads = append(payloads, "")
315-
pktlines, _ := pktline.NewFromStrings(payloads...)
316-
b, _ := ioutil.ReadAll(pktlines)
314+
p.AddFlush()
315+
b, _ := ioutil.ReadAll(p)
317316

318317
return b
319318
}
@@ -338,25 +337,24 @@ func (r *GitUploadPackRequest) String() string {
338337
}
339338

340339
func (r *GitUploadPackRequest) Reader() *strings.Reader {
341-
payloads := []string{}
340+
p := pktline.New()
342341

343342
for _, want := range r.Wants {
344-
payloads = append(payloads, fmt.Sprintf("want %s\n", want))
343+
_ = p.AddString(fmt.Sprintf("want %s\n", want))
345344
}
346345

347346
for _, have := range r.Haves {
348-
payloads = append(payloads, fmt.Sprintf("have %s\n", have))
347+
_ = p.AddString(fmt.Sprintf("have %s\n", have))
349348
}
350349

351350
if r.Depth != 0 {
352-
payloads = append(payloads, fmt.Sprintf("deepen %d\n", r.Depth))
351+
_ = p.AddString(fmt.Sprintf("deepen %d\n", r.Depth))
353352
}
354353

355-
payloads = append(payloads, "")
356-
payloads = append(payloads, "done\n")
354+
p.AddFlush()
355+
_ = p.AddString("done\n")
357356

358-
pktlines, _ := pktline.NewFromStrings(payloads...)
359-
b, _ := ioutil.ReadAll(pktlines)
357+
b, _ := ioutil.ReadAll(p)
360358

361359
return strings.NewReader(string(b))
362360
}

formats/packp/pktline/pktline.go

Lines changed: 0 additions & 114 deletions
This file was deleted.

formats/packp/pktline/pktline_test.go

Lines changed: 0 additions & 199 deletions
This file was deleted.

0 commit comments

Comments
 (0)