Skip to content

Expose and use giterr_set_str for ODB backends #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ private static bool IsRunningOnLinux()
[DllImport(libgit2)]
internal static extern GitErrorSafeHandle giterr_last();

[DllImport(libgit2)]
internal static extern void giterr_set_str(
GitErrorCategory error_class,
string errorString);

[DllImport(libgit2)]
internal static extern void giterr_set_oom();

[DllImport(libgit2)]
internal static extern int git_blob_create_fromdisk(
ref GitOid oid,
Expand Down
21 changes: 21 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ namespace LibGit2Sharp.Core
{
internal class Proxy
{
#region giterr_

public static void giterr_set_str(GitErrorCategory error_class, Exception exception)
{
if (exception is OutOfMemoryException)
{
NativeMethods.giterr_set_oom();
}
else
{
NativeMethods.giterr_set_str(error_class, exception.Message);
}
}

public static void giterr_set_str(GitErrorCategory error_class, String errorString)
{
NativeMethods.giterr_set_str(error_class, errorString);
}

#endregion

#region git_blob_

public static ObjectId git_blob_create_fromchunks(RepositorySafeHandle repo, FilePath hintpath, NativeMethods.source_callback fileCallback)
Expand Down
36 changes: 18 additions & 18 deletions LibGit2Sharp/OdbBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ private unsafe static int Read(

return toReturn;
}
catch
catch (Exception ex)
{
// TODO: Set a rich error message for libgit2
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
finally
{
Expand Down Expand Up @@ -295,9 +295,9 @@ private unsafe static int ReadPrefix(

return toReturn;
}
catch
catch (Exception ex)
{
// TODO: Set a rich error message for libgit2
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
finally
{
Expand Down Expand Up @@ -339,9 +339,9 @@ private static int ReadHeader(

return toReturn;
}
catch
catch (Exception ex)
{
// TODO: Set a rich error message for libgit2
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}

Expand Down Expand Up @@ -376,9 +376,9 @@ private static unsafe int Write(
return toReturn;
}
}
catch
catch (Exception ex)
{
// TODO: Set a rich error message for libgit2
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}

Expand Down Expand Up @@ -411,9 +411,9 @@ private static int WriteStream(

return toReturn;
}
catch
catch (Exception ex)
{
// TODO: Set a rich error message for libgit2
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}

Expand Down Expand Up @@ -444,9 +444,9 @@ private static int ReadStream(

return toReturn;
}
catch
catch (Exception ex)
{
// TODO: Set a rich error message for libgit2
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}

Expand All @@ -465,9 +465,9 @@ private static bool Exists(
{
return odbBackend.Exists(oid.Id);
}
catch
catch (Exception ex)
{
// TODO: Set a rich error message for libgit2
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}

Expand All @@ -487,9 +487,9 @@ private static int Foreach(
{
return odbBackend.Foreach(new ForeachState(cb, data).ManagedCallback);
}
catch
catch (Exception ex)
{
// TODO: Set a rich error message for libgit2
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}

Expand All @@ -508,9 +508,9 @@ private static void Free(
{
odbBackend.Dispose();
}
catch
catch (Exception ex)
{
// TODO: Set a rich error message for libgit2
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}
}
Expand Down
44 changes: 36 additions & 8 deletions LibGit2Sharp/OdbBackendStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,14 @@ private unsafe static int Read(
{
using (UnmanagedMemoryStream memoryStream = new UnmanagedMemoryStream((byte*)buffer, 0, (long)len.ToUInt64(), FileAccess.ReadWrite))
{
return odbBackendStream.Read(memoryStream, (long)len.ToUInt64());
try
{
return odbBackendStream.Read(memoryStream, (long)len.ToUInt64());
}
catch (Exception ex)
{
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}
}

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

using (UnmanagedMemoryStream dataStream = new UnmanagedMemoryStream((byte*)buffer, length))
{
return odbBackendStream.Write(dataStream, length);
try
{
return odbBackendStream.Write(dataStream, length);
}
catch (Exception ex)
{
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}
}

Expand All @@ -190,14 +204,21 @@ private static int FinalizeWrite(
{
byte[] computedObjectId;

int toReturn = odbBackendStream.FinalizeWrite(out computedObjectId);
try
{
int toReturn = odbBackendStream.FinalizeWrite(out computedObjectId);

if (0 == toReturn)
{
oid_p.Id = computedObjectId;
}

if (0 == toReturn)
return toReturn;
}
catch (Exception ex)
{
oid_p.Id = computedObjectId;
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}

return toReturn;
}

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

if (odbBackendStream != null)
{
odbBackendStream.Dispose();
try
{
odbBackendStream.Dispose();
}
catch (Exception ex)
{
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}
}
}
Expand Down