From 51d820e22a4517dbb06d38a4f07f7c48522ef811 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 5 Jun 2023 03:25:53 +0200 Subject: Initial commit --- LibGit/BlobObject.cs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 LibGit/BlobObject.cs (limited to 'LibGit/BlobObject.cs') diff --git a/LibGit/BlobObject.cs b/LibGit/BlobObject.cs new file mode 100644 index 0000000..93fd5f3 --- /dev/null +++ b/LibGit/BlobObject.cs @@ -0,0 +1,71 @@ +using System.IO.Compression; +using System.Text.Json.Serialization; +using LibGit.Extensions; +using LibGit.Interfaces; + +namespace LibGit; + +public class BlobObject +{ + [JsonIgnore] + public IRepoSource RepoSource { get; } + public string ObjectId { get; } + + public BlobObject(IRepoSource repoSource, string objectId) + { + RepoSource = repoSource; + ObjectId = objectId; + } + + private const bool _debug = false; + + public string Length { get; set; } + public IEnumerable Data { get; set; } + + public BlobObject ReadFromZlibCompressedObjFile(Stream bytes) + { + if(_debug) Console.WriteLine($"Decompressing {this.GetType().Name}"); + using ZLibStream stream = new ZLibStream(bytes, CompressionMode.Decompress); + using var result = new MemoryStream(); + stream.CopyTo(result); + stream.Flush(); + stream.Close(); + return ReadFromDecompressedObjFile(result); + } + + public BlobObject ReadFromDecompressedObjFile(Stream data) + { + if(_debug) Console.WriteLine("Parsing commit object"); + // var data = new Queue(bytes.ToArray()); + int iters = 0; + data.Seek(0, SeekOrigin.Begin); + if(_debug)Console.WriteLine($"Iteration {iters}: starting pos: {data.Position}/+{data.Remaining()}/{data.Length}"); + while (data.Position < data.Length) + { + if(_debug) Console.WriteLine($"Iteration {iters} ({data.Position}/+{data.Remaining()}/{data.Length})"); + if (data.StartsWith("tree ")) + Length = data.ReadNullTerminatedField(asciiPrefix: "tree ").AsString(); + + Data = data.ReadBytes(data.Remaining()); + + if (data.Remaining() > 0) + { + Console.WriteLine($"--Unparsed data after {++iters} iteration(s) of parsing CommitObject--"); + Console.WriteLine(this.ToJson()); + Console.WriteLine("--HexDump of remaining data--"); + data.Peek(data.Remaining()).HexDump(); + //Console.WriteLine($"Unparsed data: {Encoding.UTF8.GetString(data.ToArray())}"); + } + if(iters > 100) throw new Exception("Too many iterations"); + } + + data.Close(); + return this; + } + + public class TreeObjectEntry + { + public string Mode { get; set; } + public string Hash { get; set; } + } +} \ No newline at end of file -- cgit 1.4.1