Skip to content
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
32 changes: 32 additions & 0 deletions LibGit2Sharp.Tests/NetworkFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,38 @@ public void CanPullIntoEmptyRepo()
}
}

[Fact]
public void PullWithoutMergeBranchThrows()
{
var scd = BuildSelfCleaningDirectory();
string clonedRepoPath = Repository.Clone(StandardTestRepoPath, scd.DirectoryPath);

using (var repo = new Repository(clonedRepoPath))
{
Branch branch = repo.Branches["master"];

// Update the Upstream merge branch
repo.Branches.Update(branch,
b => b.UpstreamBranch = "refs/heads/another_master");

bool didPullThrow = false;
MergeFetchHeadNotFoundException thrownException = null;

try
{
repo.Network.Pull(Constants.Signature, new PullOptions());
}
catch(MergeFetchHeadNotFoundException ex)
{
didPullThrow = true;
thrownException = ex;
}

Assert.True(didPullThrow, "Pull did not throw.");
Assert.True(thrownException.Message.Contains("refs/heads/another_master"), "Exception message did not contain expected reference.");
}
}

/*
* git ls-remote https://github.com/libgit2/TestGitRepository
* 49322bb17d3acc9146f98c97d078513228bbf3c0 HEAD
Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
<Compile Include="PushOptions.cs" />
<Compile Include="Core\GitBuf.cs" />
<Compile Include="FilteringOptions.cs" />
<Compile Include="MergeFetchHeadNotFoundException.cs" />
<Compile Include="ResetMode.cs" />
<Compile Include="NoteCollectionExtensions.cs" />
<Compile Include="RefSpecDirection.cs" />
Expand Down
49 changes: 49 additions & 0 deletions LibGit2Sharp/MergeFetchHeadNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Runtime.Serialization;
using LibGit2Sharp.Core;

namespace LibGit2Sharp
{
/// <summary>
/// The exception that is thrown when the ref to merge with was as part of a pull operation not fetched.
/// </summary>
[Serializable]
public class MergeFetchHeadNotFoundException : NotFoundException
{
/// <summary>
/// Initializes a new instance of the <see cref="MergeFetchHeadNotFoundException"/> class.
/// </summary>
public MergeFetchHeadNotFoundException()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MergeFetchHeadNotFoundException"/> class with a specified error message.
/// </summary>
/// <param name="message">A message that describes the error.</param>
public MergeFetchHeadNotFoundException(string message)
: base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MergeFetchHeadNotFoundException"/> 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 MergeFetchHeadNotFoundException(string message, Exception innerException)
: base(message, innerException)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MergeFetchHeadNotFoundException"/> 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 MergeFetchHeadNotFoundException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}
4 changes: 3 additions & 1 deletion LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,9 @@ internal MergeResult MergeFetchHeads(Signature merger, MergeOptions options)

if (fetchHeads.Length == 0)
{
throw new LibGit2SharpException("Remote ref to merge from was not fetched.");
var expectedRef = this.Head.UpstreamBranchCanonicalName;
throw new MergeFetchHeadNotFoundException(string.Format(CultureInfo.InvariantCulture,
"The current branch is configured to merge with the reference '{0}' from the remote, but this reference was not fetched.", expectedRef));
}

GitMergeHeadHandle[] mergeHeadHandles = fetchHeads.Select(fetchHead =>
Expand Down