1 files changed, 71 insertions, 0 deletions
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<byte> 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<byte>(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
|