diff options
author | nulltoken <emeric.fermas@gmail.com> | 2013-01-23 14:10:19 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2013-01-23 14:12:05 +0400 |
commit | 9b71e0f8c203cd1394dff22ea1dfeedc7b841e96 (patch) | |
tree | 2e046b692fa031955ca00a2bde1f0f89c5d64a70 | |
parent | c5bb01bc1078d794314e2fcb5fe51daf7428dd05 (diff) |
Introduce OrphanedHeadException
-rw-r--r-- | LibGit2Sharp.Tests/CheckoutFixture.cs | 13 | ||||
-rw-r--r-- | LibGit2Sharp/LibGit2Sharp.csproj | 1 | ||||
-rw-r--r-- | LibGit2Sharp/OrphanedHeadException.cs | 49 | ||||
-rw-r--r-- | LibGit2Sharp/Repository.cs | 5 |
4 files changed, 66 insertions, 2 deletions
diff --git a/LibGit2Sharp.Tests/CheckoutFixture.cs b/LibGit2Sharp.Tests/CheckoutFixture.cs index b75c7a63..0ab18740 100644 --- a/LibGit2Sharp.Tests/CheckoutFixture.cs +++ b/LibGit2Sharp.Tests/CheckoutFixture.cs @@ -291,6 +291,19 @@ namespace LibGit2Sharp.Tests } [Fact] + public void CheckingOutAgainstAnUnbornBranchThrows() + { + SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); + + using (var repo = Repository.Init(scd.DirectoryPath)) + { + Assert.True(repo.Info.IsHeadOrphaned); + + Assert.Throws<OrphanedHeadException>(() => repo.Checkout(repo.Head)); + } + } + + [Fact] public void CheckingOutANonExistingBranchThrows() { using (var repo = new Repository(StandardTestRepoWorkingDirPath)) diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index 4e8f1ce1..d9723b04 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -65,6 +65,7 @@ <Compile Include="Changes.cs" /> <Compile Include="CheckoutCallbacks.cs" /> <Compile Include="CheckoutOptions.cs" /> + <Compile Include="OrphanedHeadException.cs" /> <Compile Include="UnmergedIndexEntriesException.cs" /> <Compile Include="Commit.cs" /> <Compile Include="CommitLog.cs" /> diff --git a/LibGit2Sharp/OrphanedHeadException.cs b/LibGit2Sharp/OrphanedHeadException.cs new file mode 100644 index 00000000..015dbfc4 --- /dev/null +++ b/LibGit2Sharp/OrphanedHeadException.cs @@ -0,0 +1,49 @@ +using System; +using System.Runtime.Serialization; + +namespace LibGit2Sharp +{ + /// <summary> + /// The exception that is thrown when a operation requiring an existing + /// branch is performed against an unborn branch. + /// </summary> + [Serializable] + public class OrphanedHeadException : LibGit2SharpException + { + /// <summary> + /// Initializes a new instance of the <see cref = "OrphanedHeadException" /> class. + /// </summary> + public OrphanedHeadException() + { + } + + /// <summary> + /// Initializes a new instance of the <see cref = "OrphanedHeadException" /> class with a specified error message. + /// </summary> + /// <param name = "message">A message that describes the error. </param> + public OrphanedHeadException(string message) + : base(message) + { + } + + /// <summary> + /// Initializes a new instance of the <see cref = "OrphanedHeadException" /> class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// </summary> + /// <param name = "message">The error message that explains the reason for the exception. </param> + /// <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> + public OrphanedHeadException(string message, Exception innerException) + : base(message, innerException) + { + } + + /// <summary> + /// Initializes a new instance of the <see cref = "OrphanedHeadException" /> class with a serialized data. + /// </summary> + /// <param name = "info">The <see cref="SerializationInfo "/> that holds the serialized object data about the exception being thrown.</param> + /// <param name = "context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param> + protected OrphanedHeadException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index 27fd6ab4..323e2a63 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -530,9 +530,10 @@ namespace LibGit2Sharp Ensure.ArgumentNotNull(branch, "branch"); // Make sure this is not an unborn branch. - if (branch.Tip.Tree == null) + if (branch.Tip == null) { - throw new Exception("branch tip is null, nothing to checkout."); + throw new OrphanedHeadException( + string.Format("The tip of branch '{0}' is null. There's nothing to checkout.", branch.Name)); } CheckoutTree(branch.Tip.Tree, checkoutOptions, onCheckoutProgress); |