Skip to content

Commit 1198d2a

Browse files
committed
Expose and use giterr_set_str for ODB backends
1 parent 1498ed4 commit 1198d2a

File tree

4 files changed

+83
-26
lines changed

4 files changed

+83
-26
lines changed

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ private static bool IsRunningOnLinux()
6868
[DllImport(libgit2)]
6969
internal static extern GitErrorSafeHandle giterr_last();
7070

71+
[DllImport(libgit2)]
72+
internal static extern void giterr_set_str(
73+
GitErrorCategory error_class,
74+
string errorString);
75+
76+
[DllImport(libgit2)]
77+
internal static extern void giterr_set_oom();
78+
7179
[DllImport(libgit2)]
7280
internal static extern int git_blob_create_fromdisk(
7381
ref GitOid oid,

LibGit2Sharp/Core/Proxy.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,27 @@ namespace LibGit2Sharp.Core
1111
{
1212
internal class Proxy
1313
{
14+
#region giterr_
15+
16+
public static void giterr_set_str(GitErrorCategory error_class, Exception exception)
17+
{
18+
if (exception is OutOfMemoryException)
19+
{
20+
NativeMethods.giterr_set_oom();
21+
}
22+
else
23+
{
24+
NativeMethods.giterr_set_str(error_class, exception.Message);
25+
}
26+
}
27+
28+
public static void giterr_set_str(GitErrorCategory error_class, String errorString)
29+
{
30+
NativeMethods.giterr_set_str(error_class, errorString);
31+
}
32+
33+
#endregion
34+
1435
#region git_blob_
1536

1637
public static ObjectId git_blob_create_fromchunks(RepositorySafeHandle repo, FilePath hintpath, NativeMethods.source_callback fileCallback)

LibGit2Sharp/OdbBackend.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ private unsafe static int Read(
228228

229229
return toReturn;
230230
}
231-
catch
231+
catch (Exception ex)
232232
{
233-
// TODO: Set a rich error message for libgit2
233+
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
234234
}
235235
finally
236236
{
@@ -295,9 +295,9 @@ private unsafe static int ReadPrefix(
295295

296296
return toReturn;
297297
}
298-
catch
298+
catch (Exception ex)
299299
{
300-
// TODO: Set a rich error message for libgit2
300+
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
301301
}
302302
finally
303303
{
@@ -339,9 +339,9 @@ private static int ReadHeader(
339339

340340
return toReturn;
341341
}
342-
catch
342+
catch (Exception ex)
343343
{
344-
// TODO: Set a rich error message for libgit2
344+
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
345345
}
346346
}
347347

@@ -376,9 +376,9 @@ private static unsafe int Write(
376376
return toReturn;
377377
}
378378
}
379-
catch
379+
catch (Exception ex)
380380
{
381-
// TODO: Set a rich error message for libgit2
381+
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
382382
}
383383
}
384384

@@ -411,9 +411,9 @@ private static int WriteStream(
411411

412412
return toReturn;
413413
}
414-
catch
414+
catch (Exception ex)
415415
{
416-
// TODO: Set a rich error message for libgit2
416+
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
417417
}
418418
}
419419

@@ -444,9 +444,9 @@ private static int ReadStream(
444444

445445
return toReturn;
446446
}
447-
catch
447+
catch (Exception ex)
448448
{
449-
// TODO: Set a rich error message for libgit2
449+
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
450450
}
451451
}
452452

@@ -465,9 +465,9 @@ private static bool Exists(
465465
{
466466
return odbBackend.Exists(oid.Id);
467467
}
468-
catch
468+
catch (Exception ex)
469469
{
470-
// TODO: Set a rich error message for libgit2
470+
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
471471
}
472472
}
473473

@@ -487,9 +487,9 @@ private static int Foreach(
487487
{
488488
return odbBackend.Foreach(new ForeachState(cb, data).ManagedCallback);
489489
}
490-
catch
490+
catch (Exception ex)
491491
{
492-
// TODO: Set a rich error message for libgit2
492+
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
493493
}
494494
}
495495

@@ -508,9 +508,9 @@ private static void Free(
508508
{
509509
odbBackend.Dispose();
510510
}
511-
catch
511+
catch (Exception ex)
512512
{
513-
// TODO: Set a rich error message for libgit2
513+
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
514514
}
515515
}
516516
}

LibGit2Sharp/OdbBackendStream.cs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ private unsafe static int Read(
150150
{
151151
using (UnmanagedMemoryStream memoryStream = new UnmanagedMemoryStream((byte*)buffer, 0, (long)len.ToUInt64(), FileAccess.ReadWrite))
152152
{
153-
return odbBackendStream.Read(memoryStream, (long)len.ToUInt64());
153+
try
154+
{
155+
return odbBackendStream.Read(memoryStream, (long)len.ToUInt64());
156+
}
157+
catch (Exception ex)
158+
{
159+
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
160+
}
154161
}
155162
}
156163

@@ -171,7 +178,14 @@ private static unsafe int Write(
171178

172179
using (UnmanagedMemoryStream dataStream = new UnmanagedMemoryStream((byte*)buffer, length))
173180
{
174-
return odbBackendStream.Write(dataStream, length);
181+
try
182+
{
183+
return odbBackendStream.Write(dataStream, length);
184+
}
185+
catch (Exception ex)
186+
{
187+
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
188+
}
175189
}
176190
}
177191

@@ -190,14 +204,21 @@ private static int FinalizeWrite(
190204
{
191205
byte[] computedObjectId;
192206

193-
int toReturn = odbBackendStream.FinalizeWrite(out computedObjectId);
207+
try
208+
{
209+
int toReturn = odbBackendStream.FinalizeWrite(out computedObjectId);
210+
211+
if (0 == toReturn)
212+
{
213+
oid_p.Id = computedObjectId;
214+
}
194215

195-
if (0 == toReturn)
216+
return toReturn;
217+
}
218+
catch (Exception ex)
196219
{
197-
oid_p.Id = computedObjectId;
220+
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
198221
}
199-
200-
return toReturn;
201222
}
202223

203224
return (int)GitErrorCode.Error;
@@ -210,7 +231,14 @@ private static void Free(
210231

211232
if (odbBackendStream != null)
212233
{
213-
odbBackendStream.Dispose();
234+
try
235+
{
236+
odbBackendStream.Dispose();
237+
}
238+
catch (Exception ex)
239+
{
240+
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
241+
}
214242
}
215243
}
216244
}

0 commit comments

Comments
 (0)