Skip to content

Commit ea096f0

Browse files
committed
Simplify HistoryRewriter.RewriteReference()
1 parent 5e04efc commit ea096f0

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

LibGit2Sharp/Core/HistoryRewriter.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ internal class HistoryRewriter
1111

1212
private readonly HashSet<Commit> targetedCommits;
1313
private readonly Dictionary<ObjectId, ObjectId> shaMap = new Dictionary<ObjectId, ObjectId>();
14+
private readonly Queue<Action> rollbackActions = new Queue<Action>();
1415

1516
private readonly string backupRefsNamespace;
1617
private readonly RewriteHistoryOptions options;
@@ -49,8 +50,6 @@ public void Execute()
4950
RewriteCommit(commit);
5051
}
5152

52-
var rollbackActions = new Queue<Action>();
53-
5453
try
5554
{
5655
// Ordering matters. In the case of `A -> B -> commit`, we need to make sure B is rewritten
@@ -59,17 +58,7 @@ public void Execute()
5958
{
6059
// TODO: Check how rewriting of notes actually behaves
6160

62-
var dref = reference as DirectReference;
63-
if (dref == null)
64-
{
65-
// TODO: Handle a cornercase where a symbolic reference
66-
// points to a Tag which name has been rewritten
67-
continue;
68-
}
69-
70-
var newTarget = RewriteTarget(dref.Target);
71-
72-
RewriteReference(dref, newTarget, backupRefsNamespace, rollbackActions);
61+
RewriteReference(reference);
7362
}
7463
}
7564
catch (Exception)
@@ -81,9 +70,30 @@ public void Execute()
8170

8271
throw;
8372
}
73+
finally
74+
{
75+
rollbackActions.Clear();
76+
}
77+
}
78+
79+
private void RewriteReference(Reference reference)
80+
{
81+
var sref = reference as SymbolicReference;
82+
if (sref != null)
83+
{
84+
// TODO: Handle a cornercase where a symbolic reference
85+
// points to a Tag which name has been rewritten
86+
return;
87+
}
88+
89+
var dref = reference as DirectReference;
90+
if (dref != null)
91+
{
92+
RewriteReference(dref);
93+
}
8494
}
8595

86-
private void RewriteReference(DirectReference oldRef, ObjectId newTarget, string backupNamePrefix, Queue<Action> rollbackActions)
96+
private void RewriteReference(DirectReference oldRef)
8797
{
8898
string newRefName = oldRef.CanonicalName;
8999
if (oldRef.IsTag() && options.TagNameRewriter != null)
@@ -92,13 +102,15 @@ private void RewriteReference(DirectReference oldRef, ObjectId newTarget, string
92102
options.TagNameRewriter(oldRef.CanonicalName.Substring(Reference.TagPrefix.Length), false, oldRef.Target);
93103
}
94104

105+
var newTarget = RewriteTarget(oldRef.Target);
106+
95107
if (oldRef.Target.Id == newTarget && oldRef.CanonicalName == newRefName)
96108
{
97109
// The reference isn't rewritten
98110
return;
99111
}
100112

101-
string backupName = backupNamePrefix + oldRef.CanonicalName.Substring("refs/".Length);
113+
string backupName = backupRefsNamespace + oldRef.CanonicalName.Substring("refs/".Length);
102114

103115
if (repo.Refs.Resolve<Reference>(backupName) != null)
104116
{

0 commit comments

Comments
 (0)