@@ -10,7 +10,7 @@ internal class HistoryRewriter
10
10
private readonly Repository repo ;
11
11
12
12
private readonly HashSet < Commit > targetedCommits ;
13
- private readonly Dictionary < ObjectId , ObjectId > shaMap = new Dictionary < ObjectId , ObjectId > ( ) ;
13
+ private readonly Dictionary < GitObject , GitObject > objectMap = new Dictionary < GitObject , GitObject > ( ) ;
14
14
private readonly Queue < Action > rollbackActions = new Queue < Action > ( ) ;
15
15
16
16
private readonly string backupRefsNamespace ;
@@ -104,7 +104,7 @@ private void RewriteReference(DirectReference oldRef)
104
104
105
105
var newTarget = RewriteTarget ( oldRef . Target ) ;
106
106
107
- if ( oldRef . Target . Id == newTarget && oldRef . CanonicalName == newRefName )
107
+ if ( oldRef . Target == newTarget && oldRef . CanonicalName == newRefName )
108
108
{
109
109
// The reference isn't rewritten
110
110
return ;
@@ -121,14 +121,14 @@ private void RewriteReference(DirectReference oldRef)
121
121
repo . Refs . Add ( backupName , oldRef . TargetIdentifier , false , "filter-branch: backup" ) ;
122
122
rollbackActions . Enqueue ( ( ) => repo . Refs . Remove ( backupName ) ) ;
123
123
124
- if ( newTarget == ObjectId . Zero )
124
+ if ( newTarget == null )
125
125
{
126
126
repo . Refs . Remove ( oldRef ) ;
127
127
rollbackActions . Enqueue ( ( ) => repo . Refs . Add ( oldRef . CanonicalName , oldRef , true , "filter-branch: abort" ) ) ;
128
128
return ;
129
129
}
130
130
131
- Reference newRef = repo . Refs . UpdateTarget ( oldRef , newTarget , "filter-branch: rewrite" ) ;
131
+ Reference newRef = repo . Refs . UpdateTarget ( oldRef , newTarget . Id , "filter-branch: rewrite" ) ;
132
132
rollbackActions . Enqueue ( ( ) => repo . Refs . UpdateTarget ( oldRef , oldRef . Target . Id , "filter-branch: abort" ) ) ;
133
133
134
134
if ( newRef . CanonicalName == newRefName )
@@ -173,15 +173,14 @@ private void RewriteCommit(Commit commit)
173
173
// Create the new commit
174
174
var mappedNewParents = newParents
175
175
. Select ( oldParent =>
176
- shaMap . ContainsKey ( oldParent . Id )
177
- ? shaMap [ oldParent . Id ]
178
- : oldParent . Id )
179
- . Where ( id => id != ObjectId . Zero )
180
- . Select ( id => repo . Lookup < Commit > ( id ) )
176
+ objectMap . ContainsKey ( oldParent )
177
+ ? objectMap [ oldParent ] as Commit
178
+ : oldParent )
179
+ . Where ( newParent => newParent != null )
181
180
. ToList ( ) ;
182
181
183
182
if ( options . PruneEmptyCommits &&
184
- TryPruneEmptyCommit ( commit . Id , mappedNewParents , newTree ) )
183
+ TryPruneEmptyCommit ( commit , mappedNewParents , newTree ) )
185
184
{
186
185
return ;
187
186
}
@@ -191,36 +190,36 @@ private void RewriteCommit(Commit commit)
191
190
mappedNewParents ) ;
192
191
193
192
// Record the rewrite
194
- shaMap [ commit . Id ] = newCommit . Id ;
193
+ objectMap [ commit ] = newCommit ;
195
194
}
196
195
197
- private bool TryPruneEmptyCommit ( ObjectId commitId , IList < Commit > mappedNewParents , Tree newTree )
196
+ private bool TryPruneEmptyCommit ( Commit commit , IList < Commit > mappedNewParents , Tree newTree )
198
197
{
199
198
var parent = mappedNewParents . Count > 0 ? mappedNewParents [ 0 ] : null ;
200
199
201
200
if ( parent == null )
202
201
{
203
202
if ( newTree . Count == 0 )
204
203
{
205
- shaMap [ commitId ] = ObjectId . Zero ;
204
+ objectMap [ commit ] = null ;
206
205
return true ;
207
206
}
208
207
}
209
208
else if ( parent . Tree == newTree )
210
209
{
211
- shaMap [ commitId ] = parent . Id ;
210
+ objectMap [ commit ] = parent ;
212
211
return true ;
213
212
}
214
213
215
214
return false ;
216
215
}
217
216
218
- private ObjectId RewriteTarget ( GitObject oldTarget )
217
+ private GitObject RewriteTarget ( GitObject oldTarget )
219
218
{
220
219
// Has this target already been rewritten?
221
- if ( shaMap . ContainsKey ( oldTarget . Id ) )
220
+ if ( objectMap . ContainsKey ( oldTarget ) )
222
221
{
223
- return shaMap [ oldTarget . Id ] ;
222
+ return objectMap [ oldTarget ] ;
224
223
}
225
224
226
225
Debug . Assert ( ( oldTarget as Commit ) == null ) ;
@@ -229,13 +228,11 @@ private ObjectId RewriteTarget(GitObject oldTarget)
229
228
if ( annotation == null )
230
229
{
231
230
//TODO: Probably a Tree or a Blob. This is not covered by any test
232
- return oldTarget . Id ;
231
+ return oldTarget ;
233
232
}
234
233
235
234
// Recursively rewrite annotations if necessary
236
- ObjectId newTargetId = RewriteTarget ( annotation . Target ) ;
237
-
238
- var newTarget = repo . Lookup ( newTargetId ) ;
235
+ var newTarget = RewriteTarget ( annotation . Target ) ;
239
236
240
237
string newName = annotation . Name ;
241
238
@@ -246,8 +243,8 @@ private ObjectId RewriteTarget(GitObject oldTarget)
246
243
247
244
var newAnnotation = repo . ObjectDatabase . CreateTagAnnotation ( newName , newTarget , annotation . Tagger ,
248
245
annotation . Message ) ;
249
- shaMap [ annotation . Id ] = newAnnotation . Id ;
250
- return newAnnotation . Id ;
246
+ objectMap [ annotation ] = newAnnotation ;
247
+ return newAnnotation ;
251
248
}
252
249
253
250
private int ReferenceDepth ( Reference reference )
0 commit comments