Skip to content

Commit 0625fce

Browse files
committed
Make OdbBackend expose valid backend return codes
1 parent 43d7bed commit 0625fce

File tree

2 files changed

+49
-30
lines changed

2 files changed

+49
-30
lines changed

LibGit2Sharp.Tests/OdbBackendFixture.cs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ public override int Read(ObjectId oid, out Stream data, out ObjectType objectTyp
182182

183183
objectType = gitObject.ObjectType;
184184

185-
return GIT_OK;
185+
return (int)ReturnCode.GIT_OK;
186186
}
187187

188-
return GIT_ENOTFOUND;
188+
return (int)ReturnCode.GIT_ENOTFOUND;
189189
}
190190

191191
public override int ReadPrefix(byte[] shortOid, int len, out byte[] oid, out Stream data, out ObjectType objectType)
@@ -228,7 +228,7 @@ public override int ReadPrefix(byte[] shortOid, int len, out byte[] oid, out Str
228228

229229
if (null != gitObjectAlreadyFound)
230230
{
231-
return GIT_EAMBIGUOUS;
231+
return (int)ReturnCode.GIT_EAMBIGUOUS;
232232
}
233233

234234
gitObjectAlreadyFound = gitObject;
@@ -246,37 +246,47 @@ public override int ReadPrefix(byte[] shortOid, int len, out byte[] oid, out Str
246246
data.Write(chunk, 0, chunk.Length);
247247
}
248248

249-
return GIT_OK;
249+
return (int)ReturnCode.GIT_OK;
250250
}
251251

252-
return GIT_ENOTFOUND;
252+
return (int)ReturnCode.GIT_ENOTFOUND;
253253
}
254254

255255
public override int Write(ObjectId oid, Stream dataStream, long length, ObjectType objectType)
256+
{
257+
var buffer = ReadBuffer(dataStream, length);
258+
259+
m_objectIdToContent.Add(oid,
260+
new MockGitObject(oid, objectType, length, new List<byte[]> { buffer }));
261+
262+
return (int)ReturnCode.GIT_OK;
263+
}
264+
265+
private static byte[] ReadBuffer(Stream dataStream, long length)
256266
{
257267
if (length > int.MaxValue)
258268
{
259-
return GIT_ERROR;
269+
throw new InvalidOperationException(
270+
string.Format("Provided length ({0}) exceeds int.MaxValue ({1}).", length, int.MaxValue));
260271
}
261272

262273
var buffer = new byte[length];
263274
int bytesRead = dataStream.Read(buffer, 0, (int)length);
264275

265276
if (bytesRead != (int)length)
266277
{
267-
return GIT_ERROR;
278+
throw new InvalidOperationException(
279+
string.Format("Too short buffer. {0} bytes were expected. {1} have been successfully read.", length,
280+
bytesRead));
268281
}
269-
270-
m_objectIdToContent.Add(oid, new MockGitObject(oid, objectType, length, new List<byte[]> { buffer }));
271-
272-
return GIT_OK;
282+
return buffer;
273283
}
274284

275285
public override int WriteStream(long length, ObjectType objectType, out OdbBackendStream stream)
276286
{
277287
stream = new MockOdbBackendStream(this, objectType, length);
278288

279-
return GIT_OK;
289+
return (int)ReturnCode.GIT_OK;
280290
}
281291

282292
public override bool Exists(ObjectId oid)
@@ -287,11 +297,6 @@ public override bool Exists(ObjectId oid)
287297
private readonly Dictionary<ObjectId, MockGitObject> m_objectIdToContent =
288298
new Dictionary<ObjectId, MockGitObject>();
289299

290-
private const int GIT_OK = 0;
291-
private const int GIT_ERROR = -1;
292-
private const int GIT_ENOTFOUND = -3;
293-
private const int GIT_EAMBIGUOUS = -5;
294-
295300
#region Unimplemented
296301

297302
public override int ReadHeader(ObjectId oid, out int length, out ObjectType objectType)
@@ -311,7 +316,7 @@ public override int ForEach(ForEachCallback callback)
311316
callback(mockGitObject.Key);
312317
}
313318

314-
return GIT_OK;
319+
return (int)ReturnCode.GIT_OK;
315320
}
316321

317322
#endregion
@@ -345,19 +350,11 @@ public override bool CanWrite
345350

346351
public override int Write(Stream dataStream, long length)
347352
{
348-
if (length > Int32.MaxValue)
349-
return GIT_ERROR;
350-
351-
var buffer = new byte[length];
352-
353-
int bytesRead = dataStream.Read(buffer, 0, (int)length);
354-
355-
if (bytesRead != (int)length)
356-
return GIT_ERROR;
353+
var buffer = ReadBuffer(dataStream, length);
357354

358355
m_chunks.Add(buffer);
359356

360-
return GIT_OK;
357+
return (int)ReturnCode.GIT_OK;
361358
}
362359

363360
public override int FinalizeWrite(ObjectId oid)
@@ -366,15 +363,16 @@ public override int FinalizeWrite(ObjectId oid)
366363

367364
if (totalLength != m_length)
368365
{
369-
return GIT_ERROR;
366+
throw new InvalidOperationException(
367+
string.Format("Invalid final length. {0} was expected. The total size of the received chunks is {1}.", m_length, totalLength));
370368
}
371369

372370
var backend = (MockOdbBackend)Backend;
373371

374372
backend.m_objectIdToContent.Add(oid,
375373
new MockGitObject(oid, m_type, m_length, m_chunks));
376374

377-
return GIT_OK;
375+
return (int)ReturnCode.GIT_OK;
378376
}
379377

380378
private readonly List<byte[]> m_chunks = new List<byte[]>();

LibGit2Sharp/OdbBackend.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,5 +624,26 @@ protected enum OdbBackendOperations
624624
/// </summary>
625625
ForEach = 128,
626626
}
627+
628+
/// <summary>
629+
/// Libgit2 expected backend return codes.
630+
/// </summary>
631+
protected enum ReturnCode
632+
{
633+
/// <summary>
634+
/// No error has occured.
635+
/// </summary>
636+
GIT_OK = 0,
637+
638+
/// <summary>
639+
/// No object matching the oid, or short oid, can be found in the backend.
640+
/// </summary>
641+
GIT_ENOTFOUND = -3,
642+
643+
/// <summary>
644+
/// The given short oid is ambiguous.
645+
/// </summary>
646+
GIT_EAMBIGUOUS = -5,
647+
}
627648
}
628649
}

0 commit comments

Comments
 (0)