Skip to content

Commit a78d06f

Browse files
committed
Remote: implement initial clone
As we have no commit source yet, Download() can only clone and store the resulting pack somewhere. The directory is given as the argument to the method. There is no way to interact with the indexer yet.
1 parent 50ae7e9 commit a78d06f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

LibGit2Sharp/Pkt.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ namespace LibGit2Sharp
66
{
77
public class Pkt
88
{
9+
static public char[] Line(string msg)
10+
{
11+
return string.Format("{0:x4}{1}\n", 4 + msg.Length + 1, msg).ToCharArray();
12+
}
13+
914
static public Pkt Parse(BinaryReader reader)
1015
{
1116
var buf = reader.ReadBytes(4);

LibGit2Sharp/Remote.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34
using System.Collections.Generic;
45
using LibGit2Sharp.Core;
@@ -73,6 +74,43 @@ public void Connect()
7374
}
7475
}
7576

77+
public void Download(string path)
78+
{
79+
Ensure.ArgumentNotNullOrEmptyString(path, "path");
80+
if (!Directory.Exists(path))
81+
Directory.CreateDirectory(path);
82+
83+
/* First, let's see what we want. At this point we don't send any haves */
84+
Stream stream = new MemoryStream();
85+
var writer = new StreamWriter(stream);
86+
foreach (PktRef p in Refs) {
87+
if (!Refspec.Matches(p.Name))
88+
continue;
89+
90+
writer.Write(Pkt.Line("want " + p.Id.Sha));
91+
}
92+
/* Flush after the want lines */
93+
writer.Write("0000");
94+
/* We don't have anything locally, so just download the packfile */
95+
writer.Write(Pkt.Line("done"));
96+
writer.Flush();
97+
stream.Seek(0, SeekOrigin.Begin);
98+
transport.NegotiationStep(stream);
99+
100+
stream = transport.GetStream();
101+
var parser = new PktParser(stream);
102+
var pkt = parser.Next();
103+
/*
104+
* For now, simply check that it's one or the other. After this,
105+
* the stream contains the pack data, so feed it to the indexer.
106+
*/
107+
if (!(pkt is PktAck || pkt is PktNak))
108+
throw new GitProtocolException("Expected ACK or NAK, got neither");
109+
110+
var indexer = new Indexer(path);
111+
indexer.IndexStream(stream);
112+
}
113+
76114
private static readonly LambdaEqualityHelper<Remote> equalityHelper =
77115
new LambdaEqualityHelper<Remote>(new Func<Remote, object>[] { x => x.Name, x => x.Url });
78116

0 commit comments

Comments
 (0)