Skip to content

Commit 329c652

Browse files
jamillStelioCantos
authored andcommitted
Revert should throw if HEAD branch is orphaned
1 parent 7776cdb commit 329c652

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

LibGit2Sharp.Tests/RevertFixture.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,5 +434,29 @@ public void RevertWithNothingToRevert(bool commitOnSuccess)
434434
}
435435
}
436436
}
437+
438+
[Fact]
439+
public void RevertOrphanedBranchThrows()
440+
{
441+
// The branch name to perform the revert on
442+
const string revertBranchName = "refs/heads/revert";
443+
444+
string path = CloneRevertTestRepo();
445+
using (var repo = new Repository(path))
446+
{
447+
// Checkout the revert branch.
448+
Branch branch = repo.Checkout(revertBranchName);
449+
Assert.NotNull(branch);
450+
451+
Commit commitToRevert = repo.Head.Tip;
452+
453+
// Move the HEAD to an orphaned branch.
454+
repo.Refs.UpdateTarget("HEAD", "refs/heads/orphan");
455+
Assert.True(repo.Info.IsHeadUnborn);
456+
457+
// Revert the tip of the refs/heads/revert branch.
458+
Assert.Throws<UnbornBranchException>(() => repo.Revert(commitToRevert, Constants.Signature));
459+
}
460+
}
437461
}
438462
}

LibGit2Sharp/Repository.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,11 @@ public RevertResult Revert(Commit commit, Signature reverter, RevertOptions opti
11021102
Ensure.ArgumentNotNull(commit, "commit");
11031103
Ensure.ArgumentNotNull(reverter, "reverter");
11041104

1105+
if (Info.IsHeadUnborn)
1106+
{
1107+
throw new UnbornBranchException("Can not revert the commit. The Head doesn't point at a commit.");
1108+
}
1109+
11051110
options = options ?? new RevertOptions();
11061111

11071112
RevertResult result = null;

0 commit comments

Comments
 (0)