Skip to content

Commit d6c74d2

Browse files
committed
Deploy ObjectType to OdbBackend
Sadly, this is a breaking change as there's no way to allow a migration path through the use of an [Obsolete] attribute.
1 parent 82a8d05 commit d6c74d2

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

LibGit2Sharp.Tests/OdbBackendFixture.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ protected override OdbBackendOperations SupportedOperations
5252
}
5353
}
5454

55-
public override int Read(byte[] oid, out Stream data, out GitObjectType objectType)
55+
public override int Read(byte[] oid, out Stream data, out ObjectType objectType)
5656
{
5757
data = null;
58-
objectType = GitObjectType.Bad;
58+
objectType = default(ObjectType);
5959

6060
MockGitObject gitObject;
6161

@@ -72,11 +72,11 @@ public override int Read(byte[] oid, out Stream data, out GitObjectType objectTy
7272
return GIT_ENOTFOUND;
7373
}
7474

75-
public override int ReadPrefix(byte[] shortOid, out byte[] oid, out Stream data, out GitObjectType objectType)
75+
public override int ReadPrefix(byte[] shortOid, out byte[] oid, out Stream data, out ObjectType objectType)
7676
{
7777
oid = null;
7878
data = null;
79-
objectType = GitObjectType.Bad;
79+
objectType = default(ObjectType);
8080

8181
MockGitObject gitObjectAlreadyFound = null;
8282

@@ -120,7 +120,7 @@ public override int ReadPrefix(byte[] shortOid, out byte[] oid, out Stream data,
120120
return GIT_ENOTFOUND;
121121
}
122122

123-
public override int Write(byte[] oid, Stream dataStream, long length, GitObjectType objectType, out byte[] finalOid)
123+
public override int Write(byte[] oid, Stream dataStream, long length, ObjectType objectType, out byte[] finalOid)
124124
{
125125
using (var sha1 = new SHA1CryptoServiceProvider())
126126
{
@@ -152,7 +152,7 @@ public override int Write(byte[] oid, Stream dataStream, long length, GitObjectT
152152
return GIT_OK;
153153
}
154154

155-
public override int WriteStream(long length, GitObjectType objectType, out OdbBackendStream stream)
155+
public override int WriteStream(long length, ObjectType objectType, out OdbBackendStream stream)
156156
{
157157
stream = new MockOdbBackendStream(this, objectType, length);
158158

@@ -174,7 +174,7 @@ public override bool Exists(byte[] oid)
174174

175175
#region Unimplemented
176176

177-
public override int ReadHeader(byte[] oid, out int length, out GitObjectType objectType)
177+
public override int ReadHeader(byte[] oid, out int length, out ObjectType objectType)
178178
{
179179
throw new NotImplementedException();
180180
}
@@ -195,7 +195,7 @@ public override int ForEach(ForEachCallback callback)
195195

196196
private class MockOdbBackendStream : OdbBackendStream
197197
{
198-
public MockOdbBackendStream(MockOdbBackend backend, GitObjectType objectType, long length)
198+
public MockOdbBackendStream(MockOdbBackend backend, ObjectType objectType, long length)
199199
: base(backend)
200200
{
201201
m_type = objectType;
@@ -287,7 +287,7 @@ public override int FinalizeWrite(out byte[] oid)
287287

288288
private byte[] m_buffer;
289289

290-
private readonly GitObjectType m_type;
290+
private readonly ObjectType m_type;
291291
private readonly long m_length;
292292
private readonly HashAlgorithm m_hash;
293293

@@ -307,7 +307,7 @@ public override int Read(Stream dataStream, long length)
307307

308308
private class MockGitObject
309309
{
310-
public MockGitObject(byte[] objectId, GitObjectType objectType, byte[] data)
310+
public MockGitObject(byte[] objectId, ObjectType objectType, byte[] data)
311311
{
312312
if (objectId.Length != 20)
313313
{
@@ -320,7 +320,7 @@ public MockGitObject(byte[] objectId, GitObjectType objectType, byte[] data)
320320
}
321321

322322
public byte[] ObjectId;
323-
public GitObjectType ObjectType;
323+
public ObjectType ObjectType;
324324
public byte[] Data;
325325

326326
public int Length

LibGit2Sharp/GitObjectType.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,26 @@ public static TreeEntryTargetType ToTreeEntryTargetType(this GitObjectType type)
7777
throw new InvalidOperationException(string.Format("Cannot map {0} to a TreeEntryTargetType.", type));
7878
}
7979
}
80+
81+
public static ObjectType ToObjectType(this GitObjectType type)
82+
{
83+
switch (type)
84+
{
85+
case GitObjectType.Commit:
86+
return ObjectType.Commit;
87+
88+
case GitObjectType.Tree:
89+
return ObjectType.Tree;
90+
91+
case GitObjectType.Blob:
92+
return ObjectType.Blob;
93+
94+
case GitObjectType.Tag:
95+
return ObjectType.Tag;
96+
97+
default:
98+
throw new InvalidOperationException(string.Format("Cannot map {0} to a ObjectType.", type));
99+
}
100+
}
80101
}
81102
}

LibGit2Sharp/OdbBackend.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,22 @@ protected unsafe Stream Allocate(long bytes)
5555
/// </summary>
5656
public abstract int Read(byte[] oid,
5757
out Stream data,
58-
out GitObjectType objectType);
58+
out ObjectType objectType);
5959

6060
/// <summary>
6161
/// Requests that this backend read an object. The object ID may not be complete (may be a prefix).
6262
/// </summary>
6363
public abstract int ReadPrefix(byte[] shortOid,
6464
out byte[] oid,
6565
out Stream data,
66-
out GitObjectType objectType);
66+
out ObjectType objectType);
6767

6868
/// <summary>
6969
/// Requests that this backend read an object's header (length and object type) but not its contents.
7070
/// </summary>
7171
public abstract int ReadHeader(byte[] oid,
7272
out int length,
73-
out GitObjectType objectType);
73+
out ObjectType objectType);
7474

7575
/// <summary>
7676
/// Requests that this backend write an object to the backing store. The backend may need to compute the object ID
@@ -79,7 +79,7 @@ public abstract int ReadHeader(byte[] oid,
7979
public abstract int Write(byte[] oid,
8080
Stream dataStream,
8181
long length,
82-
GitObjectType objectType,
82+
ObjectType objectType,
8383
out byte[] finalOid);
8484

8585
/// <summary>
@@ -93,7 +93,7 @@ public abstract int ReadStream(byte[] oid,
9393
/// the data in chunks.
9494
/// </summary>
9595
public abstract int WriteStream(long length,
96-
GitObjectType objectType,
96+
ObjectType objectType,
9797
out OdbBackendStream stream);
9898

9999
/// <summary>
@@ -210,7 +210,7 @@ private unsafe static int Read(
210210
if (odbBackend != null)
211211
{
212212
Stream dataStream = null;
213-
GitObjectType objectType;
213+
ObjectType objectType;
214214

215215
try
216216
{
@@ -227,7 +227,7 @@ private unsafe static int Read(
227227
}
228228

229229
len_p = new UIntPtr((ulong)memoryStream.Capacity);
230-
type_p = objectType;
230+
type_p = objectType.ToGitObjectType();
231231

232232
memoryStream.Seek(0, SeekOrigin.Begin);
233233
buffer_p = new IntPtr(memoryStream.PositionPointer);
@@ -271,7 +271,7 @@ private unsafe static int ReadPrefix(
271271
{
272272
byte[] oid;
273273
Stream dataStream = null;
274-
GitObjectType objectType;
274+
ObjectType objectType;
275275

276276
try
277277
{
@@ -294,7 +294,7 @@ private unsafe static int ReadPrefix(
294294

295295
out_oid.Id = oid;
296296
len_p = new UIntPtr((ulong)memoryStream.Capacity);
297-
type_p = objectType;
297+
type_p = objectType.ToGitObjectType();
298298

299299
memoryStream.Seek(0, SeekOrigin.Begin);
300300
buffer_p = new IntPtr(memoryStream.PositionPointer);
@@ -332,7 +332,7 @@ private static int ReadHeader(
332332
if (odbBackend != null)
333333
{
334334
int length;
335-
GitObjectType objectType;
335+
ObjectType objectType;
336336

337337
try
338338
{
@@ -341,7 +341,7 @@ private static int ReadHeader(
341341
if (0 == toReturn)
342342
{
343343
len_p = new UIntPtr((uint)length);
344-
type_p = objectType;
344+
type_p = objectType.ToGitObjectType();
345345
}
346346

347347
return toReturn;
@@ -364,6 +364,8 @@ private static unsafe int Write(
364364
{
365365
OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;
366366

367+
ObjectType objectType = type.ToObjectType();
368+
367369
if (odbBackend != null &&
368370
len.ToUInt64() < long.MaxValue)
369371
{
@@ -373,7 +375,7 @@ private static unsafe int Write(
373375
{
374376
byte[] finalOid;
375377

376-
int toReturn = odbBackend.Write(oid.Id, stream, (long)len.ToUInt64(), type, out finalOid);
378+
int toReturn = odbBackend.Write(oid.Id, stream, (long)len.ToUInt64(), objectType, out finalOid);
377379

378380
if (0 == toReturn)
379381
{
@@ -402,14 +404,16 @@ private static int WriteStream(
402404

403405
OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;
404406

407+
ObjectType objectType = type.ToObjectType();
408+
405409
if (odbBackend != null &&
406410
length.ToUInt64() < long.MaxValue)
407411
{
408412
OdbBackendStream stream;
409413

410414
try
411415
{
412-
int toReturn = odbBackend.WriteStream((long)length.ToUInt64(), type, out stream);
416+
int toReturn = odbBackend.WriteStream((long)length.ToUInt64(), objectType, out stream);
413417

414418
if (0 == toReturn)
415419
{

0 commit comments

Comments
 (0)