forked from CamTechConsultants/CvsntGitImporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExclusionFilter.cs
174 lines (152 loc) · 5.02 KB
/
ExclusionFilter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* John Hall <john.hall@camtechconsultants.com>
* Copyright (c) Cambridge Technology Consultants Ltd. All rights reserved.
*/
using System;
using System.Collections.Generic;
using System.Linq;
namespace CTC.CvsntGitImporter
{
/// <summary>
/// Apply any file exclusions to a stream of commits, and also track the state of any files that are
/// "head-only", i.e. are excluded but we want to apply their latest version to relevant branches at the end.
/// </summary>
class ExclusionFilter
{
private readonly ILogger m_log;
private readonly IConfig m_config;
private readonly Renamer m_branchRenamer;
private readonly RepositoryState m_headOnlyState;
public ExclusionFilter(ILogger log, IConfig config)
{
m_log = log;
m_config = config;
m_branchRenamer = config.BranchRename;
m_headOnlyState = RepositoryState.CreateWithBranchChangesOnly();
}
public RepositoryState HeadOnlyState
{
get { return m_headOnlyState; }
}
/// <summary>
/// Filter a stream of commits, removing files from commits or entire commits where files have
/// been excluded from the import.
/// </summary>
public IEnumerable<Commit> Filter(IEnumerable<Commit> commits)
{
foreach (var commit in commits)
{
if (commit.All(f => m_config.IncludeFile(f.File.Name)))
{
yield return commit;
}
else
{
var replacement = SplitCommit(commit, f => m_config.IncludeFile(f.Name));
if (replacement != null)
yield return replacement;
var headOnly = SplitCommit(commit, f => m_config.IsHeadOnly(f.Name));
if (headOnly != null)
m_headOnlyState.Apply(headOnly);
}
}
}
/// <summary>
/// Create extra commits for files that have been marked as "head-only".
/// </summary>
public void CreateHeadOnlyCommits(IEnumerable<string> headOnlyBranches, BranchStreamCollection streams,
FileCollection allFiles)
{
var branches = SortBranches(headOnlyBranches, streams);
var branchMerges = new Dictionary<string, string>();
if (branches.Any())
{
m_log.DoubleRuleOff();
m_log.WriteLine("Creating head-only commits");
}
using (m_log.Indent())
{
foreach (var branch in branches)
{
// record where this branch will be merged to
if (streams[branch].Predecessor != null)
branchMerges[streams[branch].Predecessor.Branch] = branch;
string branchMergeFrom;
branchMerges.TryGetValue(branch, out branchMergeFrom);
CreateHeadOnlyCommit(branch, streams, allFiles, branchMergeFrom);
}
}
}
private void CreateHeadOnlyCommit(string branch, BranchStreamCollection streams, FileCollection allFiles, string branchMergeFrom)
{
var headOnlyState = m_headOnlyState[branch];
var commitId = String.Format("headonly-{0}", branch);
var commit = new Commit(commitId);
var message = String.Format("Adding head-only files to {0}", m_branchRenamer.Process(branch));
foreach (var file in headOnlyState.LiveFiles.OrderBy(i => i, StringComparer.OrdinalIgnoreCase))
{
var fileRevision = new FileRevision(allFiles[file], headOnlyState[file],
mergepoint: Revision.Empty, time: DateTime.Now, author: "", commitId: commitId);
fileRevision.AddMessage(message);
commit.Add(fileRevision);
}
// check for a merge
if (branchMergeFrom != null)
{
Commit mergeSource = streams.Head(branchMergeFrom);
if (mergeSource.CommitId.StartsWith("headonly-"))
{
commit.MergeFrom = mergeSource;
// check for deleted files
var fileLookup = new HashSet<string>(commit.Select(r => r.File.Name));
foreach (var r in mergeSource)
{
if (!fileLookup.Contains(r.File.Name))
{
var fileRevision = new FileRevision(
file: r.File,
revision: Revision.Empty, // a bit lazy, but I think we can get away with it
mergepoint: Revision.Empty,
time: DateTime.Now,
author: "",
commitId: commitId,
isDead: true);
fileRevision.AddMessage(message);
commit.Add(fileRevision);
}
}
}
}
if (commit.Any())
{
m_log.WriteLine("Added commit {0}", commitId);
streams.AppendCommit(commit);
}
}
private Commit SplitCommit(Commit commit, Predicate<FileInfo> filter)
{
Commit newCommit = null;
foreach (var f in commit)
{
if (filter(f.File))
{
if (newCommit == null)
newCommit = new Commit(commit.CommitId);
newCommit.Add(f);
}
}
return newCommit;
}
private List<string> SortBranches(IEnumerable<string> headOnlyBranches, BranchStreamCollection streams)
{
// build a set of branches that we care about
var branches = new HashSet<string> { "MAIN" };
foreach (var branch in headOnlyBranches)
branches.Add(branch);
// get a list of all branches in order and filter it
var list = streams.OrderedBranches.Where(b => branches.Contains(b)).ToList();
list.Reverse();
return list;
}
}
}