summary refs log tree commit diff
path: root/LibGit/BlobObject.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-05 03:25:53 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-05 03:25:53 +0200
commit51d820e22a4517dbb06d38a4f07f7c48522ef811 (patch)
tree4a7749cf77223dff2414fd4b73cb17df43d7449e /LibGit/BlobObject.cs
downloadGitTools-51d820e22a4517dbb06d38a4f07f7c48522ef811.tar.xz
Initial commit HEAD master
Diffstat (limited to 'LibGit/BlobObject.cs')
-rw-r--r--LibGit/BlobObject.cs71
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