8000 Merge pull request #842 from libgit2/jamill/error_update · GiTechLab/libgit2sharp@05e81e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 05e81e7

Browse files
committed
Merge pull request libgit2#842 from libgit2/jamill/error_update
Throw NotFound and CannotDereference Exceptions - Reborn
2 parents 29e6a3c + 24446f5 commit 05e81e7

12 files changed

+96
-20
lines changed

LibGit2Sharp.Tests/BlameFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void CanBlameFromADifferentCommit()
3636
using (var repo = new Repository(path))
3737
{
3838
// File doesn't exist at HEAD
39-
Assert.Throws<LibGit2SharpException>(() => repo.Blame("ancestor-only.txt"));
39+
Assert.Throws<NotFoundException>(() => repo.Blame("ancestor-only.txt"));
4040

4141
var blame = repo.Blame("ancestor-only.txt", new BlameOptions { StartingAt = "9107b30" });
4242
Assert.Equal(1, blame.Count());

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public void CreatingBranchWithUnknownNamedTargetThrows()
254254
string path = SandboxBareTestRepo();
255255
using (var repo = new Repository(path))
256256
{
257-
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Add("my_new_branch", "my_old_branch"));
257+
Assert.Throws<NotFoundException>(() => repo.Branches.Add("my_new_branch", "my_old_branch"));
258258
}
259259
}
260260

@@ -264,8 +264,8 @@ public void CreatingBranchWithUnknownShaTargetThrows()
264264
string path = SandboxBareTestRepo();
265265
using (var repo = new Repository(path))
266266
{
267-
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Add("my_new_branch", Constants.UnknownSha));
268-
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Add("my_new_branch", Constants.UnknownSha.Substring(0, 7)));
267+
Assert.Throws<NotFoundException>(() => repo.Branches.Add("my_new_branch", Constants.UnknownSha));
268+
Assert.Throws<NotFoundException>(() => repo.Branches.Add("my_new_branch", Constants.UnknownSha.Substring(0, 7)));
269269
}
270270
}
271271

@@ -729,7 +729,7 @@ public void SetTrackedBranchForUnreasolvableRemoteThrows()
729729

730730
Branch trackedBranch = repo.Branches[trackedBranchName];
731731

732-
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Update(branch,
732+
Assert.Throws<NotFoundException>(() => repo.Branches.Update(branch,
733733
b => b.TrackedBranch = trackedBranch.CanonicalName));
734734
}
735735
}

LibGit2Sharp.Tests/CheckoutFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public void CheckingOutANonExistingBranchThrows()
383383
string path = SandboxBareTestRepo();
384384
using (var repo = new Repository(path))
385385
{
386-
Assert.Throws<LibGit2SharpException>(() => repo.Checkout("i-do-not-exist"));
386+
Assert.Throws<NotFoundException>(() => repo.Checkout("i-do-not-exist"));
387387
}
388388
}
389389

@@ -895,7 +895,7 @@ public void CheckoutLowerCasedHeadThrows()
895895
string path = SandboxStandardTestRepo();
896896
using (var repo = new Repository(path))
897897
{
898-
Assert.Throws<LibGit2SharpException>(() => repo.Checkout("head"));
898+
Assert.Throws<NotFoundException>(() => repo.Checkout("head"));
899899
}
900900
}
901901

LibGit2Sharp.Tests/CommitFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ public void QueryingTheCommitHistoryWithUnknownShaOrInvalidEntryPointThrows()
107107
string path = SandboxBareTestRepo();
108108
using (var repo = new Repository(path))
109109
{
110-
Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new CommitFilter { Since = Constants.UnknownSha }).Count());
111-
Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new CommitFilter { Since = "refs/heads/deadbeef" }).Count());
110+
Assert.Throws<NotFoundException>(() => repo.Commits.QueryBy(new CommitFilter { Since = Constants.UnknownSha }).Count());
111+
Assert.Throws<NotFoundException>(() => repo.Commits.QueryBy(new CommitFilter { Since = "refs/heads/deadbeef" }).Count());
112112
Assert.Throws<ArgumentNullException>(() => repo.Commits.QueryBy(new CommitFilter { Since = null }).Count());
113113
}
114114
}
@@ -121,8 +121,8 @@ public void QueryingTheCommitHistoryFromACorruptedReferenceThrows()
121121
{
122122
CreateCorruptedDeadBeefHead(repo.Info.Path);
123123

124-
Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new CommitFilter { Since = repo.Branches["deadbeef"] }).Count());
125-
Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new CommitFilter { Since = repo.Refs["refs/heads/deadbeef"] }).Count());
124+
Assert.Throws<NotFoundException>(() => repo.Commits.QueryBy(new CommitFilter { Since = repo.Branches["deadbeef"] }).Count());
125+
Assert.Throws<NotFoundException>(() => repo.Commits.QueryBy(new CommitFilter { Since = repo.Refs["refs/heads/deadbeef"] }).Count());
126126
}
127127
}
128128

LibGit2Sharp.Tests/ReferenceFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void CreatingADirectReferenceWithARevparseSpecPointingAtAnUnknownObjectFa
7373
string path = SandboxBareTestRepo();
7474
using (var repo = new Repository(path))
7575
{
76-
Assert.Throws<LibGit2SharpException>(() => repo.Refs.Add(name, "master^42"));
76+
Assert.Throws<NotFoundException>(() => repo.Refs.Add(name, "master^42"));
7777
}
7878
}
7979

@@ -610,7 +610,7 @@ public void UpdatingADirectReferenceTargetWithARevparsePointingAtAnUnknownObject
610610
string path = SandboxBareTestRepo();
611611
using (var repo = new Repository(path))
612612
{
613-
Assert.Throws<LibGit2SharpException>(() => repo.Refs.UpdateTarget(repo.Refs["refs/heads/master"], "refs/heads/nope"));
613+
Assert.Throws<NotFoundException>(() => repo.Refs.UpdateTarget(repo.Refs["refs/heads/master"], "refs/heads/nope"));
614614
}
615615
}
616616

LibGit2Sharp.Tests/RepositoryFixture.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,21 @@ public void LookingUpWithATooShortShaThrows()
493493
}
494494
}
495495

496+
[Fact]
497+
public void LookingUpByAWrongRevParseExpressionThrows()
498+
{
499+
string path = SandboxBareTestRepo();
500+
using (var repo = new Repository(path))
501+
{
502+
Assert.Throws<InvalidSpecificationException>(() => repo.Lookup("tags/point_to_blob^{tree}"));
503+
Assert.Throws<InvalidSpecificationException>(() => repo.Lookup("tags/point_to_blob^{commit}"));
504+
Assert.Throws<InvalidSpecificationException>(() => repo.Lookup<Commit>("tags/point_to_blob^{commit}"));
505+
Assert.Throws<InvalidSpecificationException>(() => repo.Lookup("master^{tree}^{blob}"));
506+
Assert.Throws<InvalidSpecificationException>(() => repo.Lookup<Blob>("master^{blob}"));
507+
Assert.Throws<PeelException>(() => repo.Lookup<Blob>("tags/e90810b^{blob}"));
508+
}
509+
}
510+
496511
[Fact]
497512
public void LookingUpAGitLinkThrows()
498513
{

LibGit2Sharp.Tests/ResetHeadFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void ResettingWithBadParamsThrows()
7171
Assert.Throws<ArgumentNullException>(() => repo.Reset(ResetMode.Soft, (string)null));
7272
Assert.Throws<ArgumentNullException>(() => repo.Reset(ResetMode.Soft, (Commit)null));
7373
Assert.Throws<ArgumentException>(() => repo.Reset(ResetMode.Soft, ""));
74-
Assert.Throws<LibGit2SharpException>(() => repo.Reset(ResetMode.Soft, Constants.UnknownSha));
74+
Assert.Throws<NotFoundException>(() => repo.Reset(ResetMode.Soft, Constants.UnknownSha));
7575
Assert.Throws<InvalidSpecificationException>(() => repo.Reset(ResetMode.Soft, repo.Head.Tip.Tree.Sha));
7676
}
7777
}

LibGit2Sharp.Tests/TagFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public void CreatingATagForAnUnknowReferenceThrows()
258258
string path = SandboxBareTestRepo();
259259
using (var repo = new Repository(path))
260260
{
261-
Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("mytagnorev", "aaaaaaaaaaa"));
261+
Assert.Throws<NotFoundException>(() => repo.ApplyTag("mytagnorev", "aaaaaaaaaaa"));
262262
}
263263
}
264264

@@ -269,7 +269,7 @@ public void CreatingATagForAnUnknowObjectIdThrows()
269269
string path = SandboxBareTestRepo();
270270
using (var repo = new Repository(path))
271271
{
272-
Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("mytagnorev", Constants.UnknownSha));
272+
Assert.Throws<NotFoundException>(() => repo.ApplyTag("mytagnorev", Constants.UnknownSha));
273273
}
274274
}
275275

@@ -507,7 +507,7 @@ public void AddTagWithNotExistingTargetThrows()
507507
string path = SandboxBareTestRepo();
508508
using (var repo = new Repository(path))
509509
{
510-
Assert.Throws<LibGit2SharpException>(() => repo.Tags.Add("test_tag", Constants.UnknownSha, signatureTim, "message"));
510+
Assert.Throws<NotFoundException>(() => repo.Tags.Add("test_tag", Constants.UnknownSha, signatureTim, "message"));
511511
}
512512
}
513513

@@ -623,7 +623,7 @@ public void RemovingAnUnknownTagShouldFail()
623623
string path = SandboxBareTestRepo();
624624
using (var repo = new Repository(path))
625625
{
626-
Assert.Throws<LibGit2SharpException>(() => repo.Tags.Remove("unknown-tag"));
626+
Assert.Throws<NotFoundException>(() => repo.Tags.Remove("unknown-tag"));
627627
}
628628
}
629629

LibGit2Sharp/Core/Ensure.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ private static readonly Dictionary<GitErrorCode, Func<string, GitErrorCode, GitE
100100
{ GitErrorCode.NonFastForward, (m, r, c) => new NonFastForwardException(m, r, c) },
101101
{ GitErrorCode.MergeConflict, (m, r, c) => new MergeConflictException(m, r, c) },
102102
{ GitErrorCode.LockedFile, (m, r, c) => new LockedFileException(m, r, c) },
103+
{ GitErrorCode.NotFound, (m, r, c) => new NotFoundException(m, r, c) },
104+
{ GitErrorCode.Peel, (m, r, c) => new PeelException(m, r, c) },
103105
};
104106

105107
private static void HandleError(int result)
@@ -215,7 +217,7 @@ public static void GitObjectIsNotNull(GitObject gitObject, string identifier)
215217
}
216218
else
217219
{
218-
exceptionBuilder = m => new LibGit2SharpException(m);
220+
exceptionBuilder = m => new NotFoundException(m);
219221
}
220222

221223
GitObjectIsNotNull(gitObject, identifier, exceptionBuilder);

LibGit2Sharp/InvalidSpecificationException.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
namespace LibGit2Sharp
66
{
77
/// <summary>
8-
/// The exception that is thrown when the provided specification is syntactically incorrect.
8+
/// The exception that is thrown when a provided specification is bad. This
9+
/// can happen if the provided specification is syntactically incorrect, or
10+
/// if the spec refers to an object of an incorrect type (e.g. asking to
11+
/// create a branch from a blob, or peeling a blob to a commit).
912
/// </summary>
1013
[Serializable]
1114
public class InvalidSpecificationException : LibGit2SharpException

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<Compile Include="GitObjectMetadata.cs" />
9999
<Compile Include="PatchEntryChanges.cs" />
100100
<Compile Include="PatchStats.cs" />
101+
<Compile Include="PeelException.cs" />
101102
<Compile Include="PullOptions.cs" />
102103
<Compile Include="RefSpec.cs" />
103104
<Compile Include="RefSpecCollection.cs" />

LibGit2Sharp/PeelException.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
using LibGit2Sharp.Core;
4+
5+
namespace LibGit2Sharp
6+
{
7+
/// <summary>
8+
/// The exception that is thrown when a tag cannot be peeled to the
9+
/// target type due to the object model.
10+
/// </summary>
11+
[Serializable]
12+
public class PeelException : LibGit2SharpException
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="PeelException"/> class.
16+
/// </summary>
17+
public PeelException()
18+
{
19+
}
20+
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="PeelException"/> class with a specified error message.
23+
/// </summary>
24+
/// <param name="message">A message that describes the error.</param>
25+
public PeelException(string message)
26+
: base(message)
27+
{
28+
}
29+
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="PeelException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.
32+
/// </summary>
33+
/// <param name="message">The error message that explains the reason for the exception.</param>
34+
/// <param name="innerException">The exception that is the cause of the current exception. If the <paramref name="innerException"/> parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.</param>
35+
public PeelException(string message, Exception innerException)
36+
: base(message, innerException)
37+
{
38+
}
39+
40+
/// <summary>
41+
/// Initializes a new instance of the <see cref="PeelException"/> class with a serialized data.
42+
/// </summary>
43+
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
44+
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
45+
protected PeelException(SerializationInfo info, StreamingContext context)
46+
: base(info, context)
47+
{
48+
}
49+
50+
internal PeelException(string message, GitErrorCode code, GitErrorCategory category)
51+
: base(message, code, category)
52+
{
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)
0