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
|
using LibGit.Extensions;
namespace LibGit;
public class GitPack
{
public string PackId { get; set; }
public GitRepo Repo { get; set; }
public int Version { get; set; }
public int ObjectCount { get; set; }
public List<GitPackObject> Objects { get; set; } = new List<GitPackObject>();
public GitPack Read(Stream stream)
{
stream.Peek(12).HexDump(16);
Console.Write(" Header: "); stream.Peek(04).ToArray()[0..].HexDump(4);
Console.Write("Version: "); stream.Peek(08).ToArray()[4..].HexDump(4);
Console.Write(" ObjCnt: "); stream.Peek(12).ToArray()[8..].HexDump(4);
if(!stream.StartsWith("PACK"))
throw new Exception("Invalid pack file header");
stream.Skip(4);
Version = stream.ReadInt32BE();
ObjectCount = stream.ReadInt32BE();
Console.WriteLine($"Got git v{Version} pack with {ObjectCount} objects");
for (int i = 0; i < ObjectCount; i++)
{
Objects.Add(new GitPackObject().Read(stream));
}
return this;
}
public GitPack(string packId, GitRepo repo)
{
PackId = packId;
Repo = repo;
}
}
public class GitPackIndex
{
public int Version { get; set; }
public int[] fanOutTable = new int[256];
public GitPackIndex Read(Stream stream)
{
if(!stream.StartsWith(new byte[]{0xff,0x74,0x4f,0x63}))
throw new Exception("Invalid pack index file header or pack is v1");
stream.Skip(4);
Version = stream.ReadInt32BE();
Console.WriteLine($"Got git v{Version} pack index");
//fan-out table
for (int i = 0; i < 256; i++)
{
fanOutTable[i] = stream.ReadInt32BE();
}
return this;
}
}
public class GitPackObject
{
private const bool _debug = true;
public GitPackObject Read(Stream stream)
{
stream.Peek(64).HexDump(32);
var header = stream.ReadBytes(4).ToArray();
ObjType = (GitObjectType)((header[0] & 0b0111_0000) >> 4);
if(ObjType == 0 || (int)ObjType == 5 || (int)ObjType > 7)
throw new Exception($"Invalid object type: {(int)ObjType}");
Size = header[0] & 0b0000_1111;
Offset = 0;
for (int i = 1; i < 4; i++)
{
Offset <<= 8;
Offset |= header[i];
}
if ((Size & 0b0000_1000) != 0)
{
Size <<= 4;
Size |= stream.ReadVLQ();
}
// ObjType = Type switch
// {
// 1 => GitObjectType.Commit,
// 2 => GitObjectType.Tree,
// 3 => GitObjectType.Blob,
// 4 => GitObjectType.Tag,
// 5 => GitObjectType.Invalid,
// 6 => GitObjectType.OffsDelta,
// 7 => GitObjectType.RefDelta,
// _ => throw new Exception($"Invalid object type {Type}")
// };
if(_debug) Console.WriteLine($"pack obj type: {ObjType} ({(int)ObjType}), size: {Size}, offset: {Offset}, sizeBytes: {SizeBytes}");
Console.WriteLine("Data: ");
stream.Peek(Size).Take(16).ToArray().HexDump(16);
stream.ReadBytes(Size).ZlibDecompress().Take(16).HexDump(16);
return this;
}
public GitObjectType ObjType { get; set; }
public int SizeBytes { get; set; }
public int Size { get; set; }
public int Offset { 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
}
|