8000 Revert should clean up repository state when there is nothing to revert · coding2233/libgit2sharp4unity3d@7776cdb · GitHub
[go: up one dir, main page]

Skip to content

Commit 7776cdb

Browse files
jamillStelioCantos
authored andcommitted
Revert should clean up repository state when there is nothing to revert
If reverting with the option to commit on success, and the revert completes successfully but there are no changes to commit, then revert will clean up the revert operation in progress state and the RevertStatus will indicate that there was nothing to revert.
1 parent e335cd0 commit 7776cdb

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed

LibGit2Sharp.Tests/RevertFixture.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,5 +391,48 @@ public void CanNotRevertAMergeCommitWithoutSpecifyingTheMainlineBranch()
391391
Assert.Throws<LibGit2SharpException>(() => repo.Revert(commitToRevert, Constants.Signature));
392392
}
393393
}
394+
395+
[Theory]
396+
[InlineData(true)]
397+
[InlineData(false)]
398+
public void RevertWithNothingToRevert(bool commitOnSuccess)
399+
{
400+
// The branch name to perform the revert on
401+
const string revertBranchName = "refs/heads/revert";
402+
403+
string path = CloneRevertTestRepo();
404+
using (var repo = new Repository(path))
405+
{
406+
// Checkout the revert branch.
407+
Branch branch = repo.Checkout(revertBranchName);
408+
Assert.NotNull(branch);
409+
410+
Commit commitToRevert = repo.Head.Tip;
411+
412+
// Revert tip commit.
413+
RevertResult result = repo.Revert(commitToRevert, Constants.Signature);
414+
Assert.NotNull(result);
415+
Assert.Equal(RevertStatus.Reverted, result.Status);
416+
417+
// Revert the same commit a second time
418+
result = repo.Revert(
419+
commitToRevert,
420+
Constants.Signature,
421+
new RevertOptions() { CommitOnSuccess = commitOnSuccess });
422+
423+
Assert.NotNull(result);
424+
Assert.Equal(null, result.Commit);
425+
Assert.Equal(RevertStatus.NothingToRevert, result.Status);
426+
427+
if (commitOnSuccess)
428+
{
429+
Assert.Equal(CurrentOperation.None, repo.Info.CurrentOperation);
430+
}
431+
else
432+
{
433+
Assert.Equal(CurrentOperation.Revert, repo.Info.CurrentOperation);
434+
}
435+
}
436+
}
394437
}
395438
}

LibGit2Sharp/Repository.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,14 @@ internal MergeResult MergeFetchHeads(Signature merger, MergeOptions options)
10841084

10851085
/// <summary>
10861086
/// Revert the specified commit.
1087+
/// <para>
1088+
/// If the revert is successful but there are no changes to commit,
1089+
/// then the <see cref="RevertStatus"/> will be <see cref="RevertStatus.NothingToRevert"/>.
1090+
/// If the revert is successful and there are changes to revert, then
1091+
/// the <see cref="RevertStatus"/> will be <see cref="RevertStatus.Reverted"/>.
1092+
/// If the revert resulted in conflicts, then the <see cref="RevertStatus"/>
1093+
/// will be <see cref="RevertStatus.Conflicts"/>.
1094+
/// </para>
10871095
/// </summary>
10881096
/// <param name="commit">The <see cref="Commit"/> to revert.</param>
10891097
/// <param name="reverter">The <see cref="Signature"/> of who is performing the revert.</param>
@@ -1123,12 +1131,38 @@ public RevertResult Revert(Commit commit, Signature reverter, RevertOptions opti
11231131
if (Index.IsFullyMerged)
11241132
{
11251133
Commit revertCommit = null;
1134+
1135+
// Check if the revert generated any changes
1136+
// and set the revert status accordingly
1137+
bool anythingToRevert = Index.RetrieveStatus(
1138+
new StatusOptions()
1139+
{
1140+
DetectRenamesInIndex = false,
1141+
Show = StatusShowOption.IndexOnly
1142+
}).Any();
1143+
1144+
RevertStatus revertStatus = anythingToRevert ?
1145+
RevertStatus.Reverted : RevertStatus.NothingToRevert;
1146+
11261147
if (options.CommitOnSuccess)
11271148
{
1128-
revertCommit = this.Commit(Info.Message, author: reverter, committer: reverter);
1149+
if (!anythingToRevert)
1150+
{
1151+
// If there were no changes to revert, and we are
1152+
// asked to commit the changes, then cleanup
1153+
// the repository state (following command line behavior).
1154+
Proxy.git_repository_state_cleanup(handle);
1155+
}
1156+
else
1157+
{
1158+
revertCommit = this.Commit(
1159+
Info.Message,
1160+
author: reverter,
1161+
committer: reverter);
1162+
}
11291163
}
11301164

1131-
result = new RevertResult(RevertStatus.Reverted, revertCommit);
1165+
result = new RevertResult(revertStatus, revertCommit);
11321166
}
11331167
else
11341168
{

LibGit2Sharp/RevertOptions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@ public RevertOptions()
4343
public CheckoutNotifyHandler OnCheckoutNotify { get; set; }
4444

4545
/// <summary>
46-
/// Commit the revert if the revert is successful.
46+
/// Commit changes if there are no conflicts and the revert results
47+
/// in changes.
48+
/// <para>
49+
/// Following command line behavior, if the revert results in no
50+
/// changes, then Revert will cleanup the repository state if
51+
/// <see cref="CommitOnSuccess"/> is true (i.e. the repository
52+
/// will not be left in a "revert in progress" state).
53+
/// If <see cref="CommitOnSuccess"/> is false and there are no
54+
/// changes to revert, then the repository will be left in
55+
/// the "revert in progress" state.
56+
/// </para>
4757
/// </summary>
4858
public bool CommitOnSuccess { get; set; }
4959

LibGit2Sharp/RevertResult.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public enum RevertStatus
5252
/// <summary>
5353
/// The revert resulted in conflicts.
5454
/// </summary>
55-
Conflicts
55+
Conflicts,
56+
57+
/// <summary>
58+
/// Revert was run, but there were no changes to commit.
59+
/// </summary>
60+
NothingToRevert,
5661
}
5762
}

0 commit comments

Comments
 (0)
0