A bit of professionalisation (#1)

This commit is contained in:
Patrick Stevens
2022-09-02 22:56:06 +01:00
committed by GitHub
parent e533ab1bc5
commit 7a593dd97a
29 changed files with 662 additions and 385 deletions

View File

@@ -1,6 +1,7 @@
namespace Git
open System
open System.Collections.Generic
open System.IO
open System.Text
open Git.Internals
@@ -11,6 +12,7 @@ type TreeEntry =
Name : string
Hash : Hash
}
override this.ToString () =
sprintf "%i %s %O" this.Mode this.Name this.Hash
@@ -18,36 +20,40 @@ type TreeEntry =
module Tree =
/// emits a byte array because the header needs to know a length
let encode (tree : TreeEntry list) : byte [] =
let encode (tree : TreeEntry list) : byte array =
// This is a bit odd, we should probably emit the stream in a streamy way
// rather than constructing the whole thing
let b = StringBuilder ()
for t in tree do
b.Append (sprintf "%i %s%c" t.Mode t.Name (char 0))
|> ignore
let (Hash h) = t.Hash
let hashStr = String(h |> List.toArray |> Array.map char)
b.Append (hashStr)
|> ignore
let b = ResizeArray ()
b.ToString().ToCharArray ()
|> Array.map byte
for t in tree do
b.AddRange (Encoding.ASCII.GetBytes (sprintf "%i %s" t.Mode t.Name))
b.Add 0uy
let (Hash h) = t.Hash
b.AddRange h
b.ToArray ()
/// Given a stream seeked to the point where we should start consuming,
/// decode as a tree object.
let decode (b : byte array) : TreeEntry list =
use b = new MemoryStream(b)
use b = new MemoryStream (b)
let stripRow () : TreeEntry option =
let mode = Stream.consumeTo b 32uy
match mode with
| None -> None
| Some mode ->
let name = Stream.consumeTo b 0uy
match name with
| None -> failwith "Stream ended before we could consume a name"
| Some name ->
let hash = Stream.consume b 20
{
Mode = mode |> Array.map char |> String |> Int32.Parse
Name = name |> Array.map char |> String
@@ -58,13 +64,12 @@ module Tree =
let rec allRows () : TreeEntry seq =
seq {
let r = stripRow ()
match r with
| Some r ->
yield r
yield! allRows ()
| None ->
()
| None -> ()
}
allRows ()
|> Seq.toList
allRows () |> Seq.toList