33using System . Collections . Concurrent ;
44using System . Collections . Generic ;
55using System . Linq ;
6+ using System . Threading ;
7+ using System . Threading . Tasks ;
68using GitRewrite . GitObjects ;
79using GitRewrite . IO ;
810
@@ -20,17 +22,29 @@ public static void Run(string repositoryPath, IEnumerable<string> filesToDelete,
2022
2123 var rewrittenCommits = RemoveObjectsFromTree ( repositoryPath , fileDeleteStrategies , folderDeleteStrategies ,
2224 relevantPathes ) ;
25+
2326 if ( rewrittenCommits . Any ( ) )
2427 Refs . Update ( repositoryPath , rewrittenCommits ) ;
2528 }
2629
30+ private static readonly BlockingCollection < ( byte [ ] Bytes , ObjectHash Hash ) > ObjectsToWrite =
31+ new BlockingCollection < ( byte [ ] Bytes , ObjectHash Hash ) > ( ) ;
32+
2733 public static Dictionary < ObjectHash , ObjectHash > RemoveObjectsFromTree ( string vcsPath ,
2834 FileDeletionStrategies filesToDelete , FolderDeletionStrategies foldersToDelete ,
2935 List < byte [ ] > relevantPaths )
3036 {
3137 var rewrittenCommits = new Dictionary < ObjectHash , ObjectHash > ( ) ;
3238 var rewrittenTrees = new ConcurrentDictionary < ObjectHash , ObjectHash > ( ) ;
3339
40+ var writeThread = new Thread ( collection =>
41+ {
42+ var commits = ( BlockingCollection < ( byte [ ] Bytes , ObjectHash Hash ) > ) collection ;
43+ foreach ( var commit in commits . GetConsumingEnumerable ( ) )
44+ HashContent . WriteFile ( vcsPath , commit . Bytes , commit . Hash . ToString ( ) ) ;
45+ } ) ;
46+ writeThread . Start ( ObjectsToWrite ) ;
47+
3448 foreach ( var commit in CommitWalker
3549 . CommitsInOrder ( vcsPath ) )
3650 {
@@ -46,12 +60,16 @@ public static Dictionary<ObjectHash, ObjectHash> RemoveObjectsFromTree(string vc
4660
4761 if ( newCommitHash != commit . Hash )
4862 {
49- HashContent . WriteFile ( vcsPath , newCommitBytes , newCommitHash . ToString ( ) ) ;
63+ ObjectsToWrite . Add ( ( newCommitBytes , newCommitHash ) ) ;
5064 rewrittenCommits . TryAdd ( commit . Hash , newCommitHash ) ;
5165 }
5266 }
5367 }
5468
69+ ObjectsToWrite . CompleteAdding ( ) ;
70+ writeThread . Join ( ) ;
71+ ObjectsToWrite . Dispose ( ) ;
72+
5573 return rewrittenCommits ;
5674 }
5775
@@ -153,7 +171,10 @@ private static ObjectHash RemoveObjectFromTree(
153171
154172 var fixedTree = Tree . GetFixedTree ( resultingLines ) ;
155173 if ( fixedTree . Hash != tree . Hash )
156- HashContent . WriteObject ( vcsPath , fixedTree ) ;
174+ {
175+ var bytes = GitObjectFactory . GetBytesWithHeader ( GitObjectType . Tree , fixedTree . SerializeToBytes ( ) ) ;
176+ ObjectsToWrite . Add ( ( bytes , fixedTree . Hash ) ) ;
177+ }
157178
158179 rewrittenTrees . TryAdd ( treeHash , fixedTree . Hash ) ;
159180
0 commit comments