Skip to content

Commit 2875bdf

Browse files
committed
Pkt: handle final LF and capabilities string
Remove the final LF in each pkt line so ref names are stored correctly and split out the capabilities string explicitly, as C# doesn't stop at the first NUL. References libgit2#210
1 parent e8873e1 commit 2875bdf

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

LibGit2Sharp/Pkt.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class PktRef : Pkt
2323
{
2424
public readonly ObjectId Id;
2525
public readonly string Name;
26+
public readonly string CapString;
2627

2728
public PktRef(BinaryReader reader, int len)
2829
{
@@ -32,7 +33,25 @@ public PktRef(BinaryReader reader, int len)
3233
ObjectId.TryParse(hash, out Id);
3334
reader.ReadByte(); /* Skip the SP */
3435
len -= 1; /* The SP */
35-
Name = Encoding.UTF8.GetString(reader.ReadBytes(len));
36+
37+
/* Each line SHOULD end with LF, but unfortunately we can't rely on it being there */
38+
var tmp = Encoding.UTF8.GetString(reader.ReadBytes(len));
39+
if (tmp.EndsWith("\n"))
40+
tmp = tmp.Remove(tmp.Length - 1);
41+
42+
/*
43+
* The server's capabilities are sent as "HEAD" NUL "cap1 cap2". In
44+
* C it works just fine, but C# knows that the string doesn't stop
45+
* at the first NUL, so we need to split it out explicitly.
46+
*/
47+
var nul = tmp.IndexOf('\0');
48+
if (nul == -1) {
49+
Name = tmp;
50+
} else {
51+
var parts = tmp.Split('\0');
52+
Name = parts[0];
53+
CapString = parts[1];
54+
}
3655
}
3756
}
3857

0 commit comments

Comments
 (0)