Permalink
Browse files

Use csharp friendly names for error classes/codes

This also introduces a new marshaler for GitError along with a distinction
between exception (errors) in libgit2 vs. exceptions in libgit2sharp.

See LibGit2SharpException and LibGit2Exception

This has been done in a way to not break the public API.
  • Loading branch information...
tclem committed Jun 1, 2012
1 parent adf32bc commit 637fee10913a1a4ee9636c510b896a85c2603a95
@@ -125,9 +125,9 @@ public void CreatingABranchFromANonCommitObjectThrows()
using (var repo = new Repository(BareTestRepoPath))
{
const string name = "sorry-dude-i-do-not-do-blobs-nor-trees";
- Assert.Throws<LibGit2Exception>(() => repo.CreateBranch(name, "refs/tags/point_to_blob"));
- Assert.Throws<LibGit2Exception>(() => repo.CreateBranch(name, "53fc32d"));
- Assert.Throws<LibGit2Exception>(() => repo.CreateBranch(name, "0266163"));
+ Assert.Throws<LibGit2SharpException>(() => repo.CreateBranch(name, "refs/tags/point_to_blob"));
+ Assert.Throws<LibGit2SharpException>(() => repo.CreateBranch(name, "53fc32d"));
+ Assert.Throws<LibGit2SharpException>(() => repo.CreateBranch(name, "0266163"));
}
}
@@ -136,7 +136,7 @@ public void CreatingBranchWithUnknownNamedTargetThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.Branches.Create("my_new_branch", "my_old_branch"));
+ Assert.Throws<LibGit2SharpException>(() => repo.Branches.Create("my_new_branch", "my_old_branch"));
}
}
@@ -145,8 +145,8 @@ public void CreatingBranchWithUnknownShaTargetThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.Branches.Create("my_new_branch", Constants.UnknownSha));
- Assert.Throws<LibGit2Exception>(() => repo.Branches.Create("my_new_branch", Constants.UnknownSha.Substring(0, 7)));
+ Assert.Throws<LibGit2SharpException>(() => repo.Branches.Create("my_new_branch", Constants.UnknownSha));
+ Assert.Throws<LibGit2SharpException>(() => repo.Branches.Create("my_new_branch", Constants.UnknownSha.Substring(0, 7)));
}
}
@@ -155,7 +155,7 @@ public void CreatingABranchPointingAtANonCanonicalReferenceThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.Branches.Create("nocanonicaltarget", "br2"));
+ Assert.Throws<LibGit2SharpException>(() => repo.Branches.Create("nocanonicaltarget", "br2"));
}
}
@@ -414,7 +414,7 @@ public void CheckingOutANonExistingBranchThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.Checkout("i-do-not-exist"));
+ Assert.Throws<LibGit2SharpException>(() => repo.Checkout("i-do-not-exist"));
}
}
@@ -96,8 +96,8 @@ public void QueryingTheCommitHistoryWithUnknownShaOrInvalidEntryPointThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.Commits.QueryBy(new Filter { Since = Constants.UnknownSha }).Count());
- Assert.Throws<LibGit2Exception>(() => repo.Commits.QueryBy(new Filter { Since = "refs/heads/deadbeef" }).Count());
+ Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new Filter { Since = Constants.UnknownSha }).Count());
+ Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new Filter { Since = "refs/heads/deadbeef" }).Count());
Assert.Throws<ArgumentNullException>(() => repo.Commits.QueryBy(new Filter { Since = null }).Count());
}
}
@@ -110,8 +110,8 @@ public void QueryingTheCommitHistoryFromACorruptedReferenceThrows()
{
CreateCorruptedDeadBeefHead(repo.Info.Path);
- Assert.Throws<LibGit2Exception>(() => repo.Commits.QueryBy(new Filter { Since = repo.Branches["deadbeef"] }).Count());
- Assert.Throws<LibGit2Exception>(() => repo.Commits.QueryBy(new Filter { Since = repo.Refs["refs/heads/deadbeef"] }).Count());
+ Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new Filter { Since = repo.Branches["deadbeef"] }).Count());
+ Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new Filter { Since = repo.Refs["refs/heads/deadbeef"] }).Count());
}
}
@@ -647,7 +647,7 @@ public void CanNotAmendAnEmptyRepository()
using (Repository repo = Repository.Init(scd.DirectoryPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.Commit("I can not amend anything !:(", DummySignature, DummySignature, true));
+ Assert.Throws<LibGit2SharpException>(() => repo.Commit("I can not amend anything !:(", DummySignature, DummySignature, true));
}
}
@@ -220,7 +220,7 @@ public void SettingLocalConfigurationOutsideAReposThrows()
{
using (var config = new Configuration())
{
- Assert.Throws<LibGit2Exception>(() => config.Set("unittests.intsetting", 3));
+ Assert.Throws<LibGit2SharpException>(() => config.Set("unittests.intsetting", 3));
}
}
@@ -132,14 +132,26 @@ public void CanStageTheUpdationOfAStagedFile()
[Theory]
[InlineData("1/I-do-not-exist.txt", FileStatus.Nonexistent)]
- [InlineData("deleted_staged_file.txt", FileStatus.Removed)]
public void StagingAnUnknownFileThrows(string relativePath, FileStatus status)
{
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.Null(repo.Index[relativePath]);
Assert.Equal(status, repo.Index.RetrieveStatus(relativePath));
+ Assert.Throws<LibGit2SharpException>(() => repo.Index.Stage(relativePath));
+ }
+ }
+
+ [Theory]
+ [InlineData("deleted_staged_file.txt", FileStatus.Removed)]
+ public void StagingARemovedFileThrows(string relativePath, FileStatus status)
+ {
+ using (var repo = new Repository(StandardTestRepoPath))
+ {
+ Assert.Null(repo.Index[relativePath]);
+ Assert.Equal(repo.Index.RetrieveStatus(relativePath), status);
+
Assert.Throws<LibGit2Exception>(() => repo.Index.Stage(relativePath));
}
}
@@ -457,7 +469,7 @@ private static void InvalidMoveUseCases(string sourcePath, FileStatus sourceStat
foreach (var destPath in destPaths)
{
string path = destPath;
- Assert.Throws<LibGit2Exception>(() => repo.Index.Move(sourcePath, path));
+ Assert.Throws<LibGit2SharpException>(() => repo.Index.Move(sourcePath, path));
}
}
}
@@ -493,7 +505,7 @@ public void RemovingAInvalidFileThrows(string filepath)
{
using (var repo = new Repository(StandardTestRepoPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.Index.Remove(filepath));
+ Assert.Throws<LibGit2SharpException>(() => repo.Index.Remove(filepath));
}
}
@@ -34,7 +34,7 @@ public void AccessingTheIndexInABareRepoThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.Index);
+ Assert.Throws<LibGit2SharpException>(() => repo.Index);
}
}
@@ -17,7 +17,7 @@ public void ResetANewlyInitializedRepositoryThrows(bool isBare)
using (var repo = Repository.Init(scd.DirectoryPath, isBare))
{
- Assert.Throws<LibGit2Exception>(() => repo.Reset(ResetOptions.Soft, repo.Head.CanonicalName));
+ Assert.Throws<LibGit2SharpException>(() => repo.Reset(ResetOptions.Soft, repo.Head.CanonicalName));
}
}
@@ -54,7 +54,7 @@ public void ResettingWithBadParamsThrows()
{
Assert.Throws<ArgumentNullException>(() => repo.Reset(ResetOptions.Soft, null));
Assert.Throws<ArgumentException>(() => repo.Reset(ResetOptions.Soft, ""));
- Assert.Throws<LibGit2Exception>(() => repo.Reset(ResetOptions.Soft, Constants.UnknownSha));
+ Assert.Throws<LibGit2SharpException>(() => repo.Reset(ResetOptions.Soft, Constants.UnknownSha));
}
}
@@ -149,7 +149,7 @@ public void MixedResetInABareRepositoryThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.Reset(ResetOptions.Mixed, repo.Head.Tip.Sha));
+ Assert.Throws<LibGit2SharpException>(() => repo.Reset(ResetOptions.Mixed, repo.Head.Tip.Sha));
}
}
}
@@ -175,7 +175,7 @@ public void CreatingATagInAEmptyRepositoryThrows()
using (var repo = Repository.Init(scd.DirectoryPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.ApplyTag("mynotag"));
+ Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("mynotag"));
}
}
@@ -187,7 +187,7 @@ public void CreatingATagForHeadInAEmptyRepositoryThrows()
using (var repo = Repository.Init(scd.DirectoryPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.ApplyTag("mytaghead", "HEAD"));
+ Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("mytaghead", "HEAD"));
}
}
@@ -197,7 +197,7 @@ public void CreatingATagForAnUnknowReferenceThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.ApplyTag("mytagnorev", "aaaaaaaaaaa"));
+ Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("mytagnorev", "aaaaaaaaaaa"));
}
}
@@ -206,7 +206,7 @@ public void CreatingATagForANonCanonicalReferenceThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.ApplyTag("noncanonicaltarget", "br2"));
+ Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("noncanonicaltarget", "br2"));
}
}
@@ -216,7 +216,7 @@ public void CreatingATagForAnUnknowObjectIdThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.ApplyTag("mytagnorev", Constants.UnknownSha));
+ Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("mytagnorev", Constants.UnknownSha));
}
}
@@ -408,7 +408,7 @@ public void CreateTagWithNotExistingTargetThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Throws<LibGit2Exception>(() => repo.Tags.Create("test_tag", Constants.UnknownSha, signatureTim, "message"));
+ Assert.Throws<LibGit2SharpException>(() => repo.Tags.Create("test_tag", Constants.UnknownSha, signatureTim, "message"));
}
}
@@ -121,7 +121,7 @@ public void Delete(string name, bool isRemote = false)
int res = NativeMethods.git_branch_delete(repo.Handle, name, isRemote ? GitBranchType.GIT_BRANCH_REMOTE : GitBranchType.GIT_BRANCH_LOCAL);
- if (res == (int)GitErrorCode.GIT_ENOTFOUND)
+ if (res == (int)GitErrorCode.NotFound)
{
return;
}
@@ -141,7 +141,7 @@ public Commit FindCommonAncestor(Commit first, Commit second)
GitOid ret;
int result = NativeMethods.git_merge_base(out ret, repo.Handle, osw1.ObjectPtr, osw2.ObjectPtr);
- if (result == (int)GitErrorCode.GIT_ENOTFOUND)
+ if (result == (int)GitErrorCode.NotFound)
{
return null;
}
@@ -207,7 +207,7 @@ public Commit Create(string message, Signature author, Signature committer, bool
{
if (amendPreviousCommit && repo.Info.IsEmpty)
{
- throw new LibGit2Exception("Can not amend anything. The Head doesn't point at any commit.");
+ throw new LibGit2SharpException("Can not amend anything. The Head doesn't point at any commit.");
}
GitOid treeOid;
@@ -270,7 +270,7 @@ public bool MoveNext()
GitOid oid;
int res = NativeMethods.git_revwalk_next(out oid, handle);
- if (res == (int)GitErrorCode.GIT_EREVWALKOVER)
+ if (res == (int)GitErrorCode.RevWalkOver)
{
return false;
}
@@ -404,7 +404,7 @@ private IEnumerable<ObjectId> RetrieveCommitOids(object identifier)
yield break;
}
- throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture, "Unexpected kind of identifier '{0}'.", identifier));
+ throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unexpected kind of identifier '{0}'.", identifier));
}
}
}
@@ -109,7 +109,7 @@ private static string ConvertPath(Func<byte[], uint, int> pathRetriever)
int result = pathRetriever(buffer, NativeMethods.GIT_PATH_MAX);
- if (result == (int)GitErrorCode.GIT_ENOTFOUND)
+ if (result == (int)GitErrorCode.NotFound)
{
return null;
}
@@ -149,7 +149,7 @@ public void Delete(string key, ConfigurationLevel level = ConfigurationLevel.Loc
int res = NativeMethods.git_config_delete(h, key);
- if (res == (int)GitErrorCode.GIT_ENOTFOUND)
+ if (res == (int)GitErrorCode.NotFound)
{
return;
}
@@ -201,7 +201,7 @@ public T Get<T>(string key, T defaultValue)
ConfigurationSafeHandle handle = (LocalHandle ?? globalHandle) ?? systemHandle;
if (handle == null)
{
- throw new LibGit2Exception("Could not find a local, global or system level configuration.");
+ throw new LibGit2SharpException("Could not find a local, global or system level configuration.");
}
return (T)configurationTypedRetriever[typeof(T)](key, defaultValue, handle);
@@ -339,17 +339,17 @@ private ConfigurationSafeHandle RetrieveConfigurationHandle(ConfigurationLevel l
{
if (level == ConfigurationLevel.Local && !HasLocalConfig)
{
- throw new LibGit2Exception("No local configuration file has been found. You must use ConfigurationLevel.Global when accessing configuration outside of repository.");
+ throw new LibGit2SharpException("No local configuration file has been found. You must use ConfigurationLevel.Global when accessing configuration outside of repository.");
}
if (level == ConfigurationLevel.Global && !HasGlobalConfig)
{
- throw new LibGit2Exception("No global configuration file has been found.");
+ throw new LibGit2SharpException("No global configuration file has been found.");
}
if (level == ConfigurationLevel.System && !HasSystemConfig)
{
- throw new LibGit2Exception("No system configuration file has been found.");
+ throw new LibGit2SharpException("No system configuration file has been found.");
}
ConfigurationSafeHandle h;
@@ -382,7 +382,7 @@ private ConfigurationSafeHandle RetrieveConfigurationHandle(ConfigurationLevel l
{
T value;
var res = getter(out value, handle, key);
- if (res == (int)GitErrorCode.GIT_ENOTFOUND)
+ if (res == (int)GitErrorCode.NotFound)
{
return defaultValue;
}
@@ -10,8 +10,6 @@ namespace LibGit2Sharp.Core
[DebuggerStepThrough]
internal static class Ensure
{
- private static readonly Utf8Marshaler marshaler = (Utf8Marshaler)Utf8Marshaler.GetInstance(string.Empty);
-
/// <summary>
/// Checks an argument to ensure it isn't null.
/// </summary>
@@ -54,36 +52,17 @@ public static void ArgumentNotNullOrEmptyString(string argumentValue, string arg
/// True when positive values are allowed as well.</param>
public static void Success(int result, bool allowPositiveResult = false)
{
- if (result == (int)GitErrorCode.GIT_OK)
+ if (result == (int)GitErrorCode.Ok)
{
return;
}
- if (allowPositiveResult && result > (int)GitErrorCode.GIT_OK)
+ if (allowPositiveResult && result > (int)GitErrorCode.Ok)
{
return;
}
- string errorMessage;
- GitError error = NativeMethods.giterr_last().MarshalAsGitError();
-
-
- if (error == null)
- {
- error = new GitError { Klass = -1, Message = IntPtr.Zero };
- errorMessage = "No error message has been provided by the native library";
- }
- else
- {
- errorMessage = (string)marshaler.MarshalNativeToManaged(error.Message);
- }
-
- throw new LibGit2Exception(
- String.Format(CultureInfo.InvariantCulture, "An error was raised by libgit2. Class = {0} ({1}).{2}{3}",
- Enum.GetName(typeof(GitErrorType), error.Klass),
- result,
- Environment.NewLine,
- errorMessage));
+ throw new LibGit2Exception(NativeMethods.giterr_last());
}
/// <summary>
@@ -109,7 +88,7 @@ public static void GitObjectIsNotNull(GitObject gitObject, string identifier)
return;
}
- throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture,
+ throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture,
"No valid git object identified by '{0}' exists in the repository.",
identifier));
}
Oops, something went wrong.

0 comments on commit 637fee1

Please sign in to comment.