summary refs log tree commit diff
path: root/LibGit/GitPack.cs
blob: 9e574ee8b6896bb36ec05151247978ba97325bd2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO.Compression;
using System.Runtime.CompilerServices;
using LibGit.Extensions;

namespace LibGit;

// https://shafiul.github.io//gitbook/7_the_packfile.html - easier to digest than the git documentation
public class GitPack
{
    private const bool _log = false;
    public string PackId { get; set; }
    public GitRepo Repo { get; set; }

    public int Version { get; set; }
    public int ObjectCount { get; set; }
    public GitPackIndex Index { get; set; }
    public List<GitPackObject> Objects { get; set; } = new();

    public GitPack Read(Stream packStream, Stream? idxStream = null, GitPackIndex? index = null)
    {
        if (_log)
        {
            Console.Write(" Header: ");
            packStream.Peek(04).ToArray()[0..].HexDump(4);
            Console.Write("Version: ");
            packStream.Peek(08).ToArray()[4..].HexDump(4);
            Console.Write(" ObjCnt: ");
            packStream.Peek(12).ToArray()[8..].HexDump(4);
        }

        if (!packStream.StartsWith("PACK"))
            throw new Exception("Invalid pack file header");

        packStream.Skip(4);

        Version = packStream.ReadInt32BE();
        ObjectCount = packStream.ReadInt32BE();
        if (_log)
        {
            Console.WriteLine($"Got git v{Version} pack with {ObjectCount} objects");
            Console.WriteLine("Reading index...");
        }

        Index = new GitPackIndex().Read(idxStream);
        if (_log) Console.WriteLine("Reading pack objects...");

        var ordered = Index.Entries.OrderBy(x => x.Offset).ToArray();
        // prevent spamming the console
        var sw = Stopwatch.StartNew();
        var tsw = Stopwatch.StartNew();
        for (int i = 0; i < ObjectCount; i++)
        {
            var obj = ordered[i];
            if (sw.ElapsedMilliseconds >= 50 || i == ObjectCount - 1)
            {
                Console.Write($"\r[{tsw.Elapsed}] Reading object {i + 1}/{ObjectCount} ({ordered[i].Sha}) @ {obj.Offset}");
                sw.Restart();
            }

            Objects.Add(new GitPackObject().ReadHeader(packStream, obj.Offset));
        }

        Console.WriteLine();

        foreach (var group in Objects.GroupBy(x => x.ObjType))
        {
            Console.WriteLine($"  - {group.Key}: {group.Count()} objects");
        }

        return this;
    }

    public GitPack(string packId, GitRepo repo)
    {
        PackId = packId;
        Repo = repo;
    }
}

public class GitPackIndex
{
    private const bool _log = false;
    public int Version { get; set; }
    public int[] fanOutTable = new int[256];
    public List<IndexEntry> Entries { get; set; } = [];
    public Sha1Value PackSHA { get; set; }
    public Sha1Value IndexSHA { get; set; }

    [InlineArray(20)]
    public struct Sha1Value
    {
        private byte _e0;

        public override string ToString()
        {
            ReadOnlySpan<byte> bytes = this;
            return Convert.ToHexStringLower(bytes);
        }
    }

    public struct IndexEntry
    {
        public Sha1Value Sha;
        public uint Crc32;
        public ulong Offset;
    }

    public GitPackIndex Read(Stream stream)
    {
        if (!stream.StartsWith([0xff, 0x74, 0x4f, 0x63]))
            throw new Exception("Invalid pack index file header or pack is v1");

        stream.Skip(4);
        Version = stream.ReadInt32BE();
        if (_log) Console.WriteLine($"Got git v{Version} pack index");

        fanOutTable = stream.MultiReadInt32BE(255);

        var size = stream.ReadInt32BE(); // aka "fanout[255]"
        if (_log) Console.WriteLine($"Index contains {size} objects");
        Entries = new List<IndexEntry>(new IndexEntry[size]);

        if (_log) Console.Write("Reading SHA1...");
        var sha1Values = stream.MultiReadInlineArray<Sha1Value>(size);

        if (_log) Console.Write(" CRC...");
        var crcValues = stream.MultiReadUInt32BE(size);

        if (_log) Console.Write(" OFS...");
        var raw32 = stream.MultiReadUInt32BE(size); // uint[]
        var raw64 = stream.MultiReadUInt64BE((int)((stream.Remaining() - 40) / sizeof(ulong)));

        if (_log) Console.WriteLine(" Hashes...");
        var h = new Sha1Value();
        stream.ReadBlock(20).CopyTo(h);
        PackSHA = h;
        if (_log) Console.WriteLine($"Pack SHA: {PackSHA}");

        h = new Sha1Value();
        stream.ReadBlock(20).CopyTo(h);
        IndexSHA = h;
        if (_log) Console.WriteLine($"Index SHA: {IndexSHA}");

        if (_log) Console.WriteLine("Constructing entries...");
        for (var i = 0; i < size; i++)
        {
            Entries[i] = new IndexEntry
            {
                Sha = sha1Values[i],
                Crc32 = crcValues[i],
                Offset = (raw32[i] & 0x80000000) == 0
                    ? raw32[i]
                    : raw64[raw32[i] & 0x7FFFFFFF]
            };
        }

        return this;
    }
}

public class GitPackObject
{
    private const bool _debug = false;

    public GitPackObject ReadHeader(Stream stream, ulong offset)
    {
        Offset = offset;
        stream.Seek((long)offset, SeekOrigin.Begin);
        if (_debug) Console.WriteLine($"Reading pack object at offset {offset}, stream position {stream.Position}");
        var headerPos = stream.Position;
        var data = stream.ReadByte();
        if (_debug) Console.WriteLine($"data: {data:X8} ({data}/{data:b8})");

        //format: 1 bit continue, 3 bits type, 4 bits size (A), continued by up to 3 more bytes of size (B, C and D), A is least significant
        ObjType = (GitObjectType)((data >> 4) & 0b0000_0111);
        var sizeBits = data & 0b0000_1111; // Lower 4 bits are the initial size
        var restOfSize = (data & 0b1000_0000) != 0 ? stream.ReadVLQ() : 0;
        UncompressedSize = (restOfSize << 4) | sizeBits;

        // handle delta objects
        if (ObjType == GitObjectType.RefDelta)
        {
            RefDeltaBaseObjectId = stream.ReadBlock(20);
            if (_debug) Console.WriteLine($"Ref delta base object id: {RefDeltaBaseObjectId.AsHexString()}");
        }
        else if (ObjType == GitObjectType.OffsDelta)
        {
            OffsDeltaBaseOffset = stream.ReadGitPackOffsetModifiedVLQ();
            if (_debug) Console.WriteLine($"Offset delta base offset: {OffsDeltaBaseOffset}");
        }

        // var dataPos = stream.Position;
        // if (_debug) Console.WriteLine($"pack objType: {ObjType} ({(int)ObjType}), uncompressed size: {UncompressedSize}, position: HDR={headerPos}, DATA={dataPos}");

        DataOffset = (ulong) stream.Position;

        return this;
    }

    public GitPackObject Read(Stream stream, ulong offset)
    {
        ReadHeader(stream, offset);

        try
        {
            using var zlibStream = new ZLibStream(stream, CompressionMode.Decompress, true);
            var decompressedData = new Span<byte>(new byte[UncompressedSize]);
            int totalRead = 0;
            while (totalRead < UncompressedSize)
            {
                int bytesRead = zlibStream.Read(decompressedData); //, totalRead, UncompressedSize - totalRead);
                if (bytesRead == 0)
                    throw new Exception("Unexpected end of zlib stream");
                totalRead += bytesRead;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error during zlib decompression: {ex.Message}");
            stream.Seek((long)Offset, SeekOrigin.Begin);
            stream.Peek(32).HexDump();
            throw;
        }

        // var endPos = stream.Position;
        // if (_debug) Console.WriteLine($"Decompressed data ({UncompressedSize} bytes/{endPos - dataPos} compressed, stream @ {endPos}):");
        // if (_debug) decompressedData.ToArray().HexDump(32);

        // Environment.Exit(1);
        return this;
    }

    public ulong Offset { get; set; }
    public ulong DataOffset { get; set; }

    public GitObjectType ObjType { get; set; }

    public int UncompressedSize { get; set; }

    public byte[]? RefDeltaBaseObjectId { get; set; }

    public int? OffsDeltaBaseOffset { get; set; }
}

public enum GitObjectType
{
    Commit = 1,
    Tree,
    Blob,
    Tag,
    Invalid, // Reserved for future expansion, see https://git-scm.com/docs/pack-format#_object_types
    OffsDelta,
    RefDelta
}