forked from trevorr/vss2git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChangesetBuilder.cs
executable file
·241 lines (217 loc) · 9.99 KB
/
ChangesetBuilder.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* Copyright 2009 HPDI, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using Hpdi.VssLogicalLib;
namespace Hpdi.Vss2Git
{
/// <summary>
/// Reconstructs changesets from independent revisions.
/// </summary>
/// <author>Trevor Robinson</author>
class ChangesetBuilder : Worker
{
private readonly RevisionAnalyzer revisionAnalyzer;
private readonly LinkedList<Changeset> changesets = new LinkedList<Changeset>();
public LinkedList<Changeset> Changesets
{
get { return changesets; }
}
private TimeSpan anyCommentThreshold = TimeSpan.FromSeconds(30);
public TimeSpan AnyCommentThreshold
{
get { return anyCommentThreshold; }
set { anyCommentThreshold = value; }
}
private TimeSpan sameCommentThreshold = TimeSpan.FromMinutes(10);
public TimeSpan SameCommentThreshold
{
get { return sameCommentThreshold; }
set { sameCommentThreshold = value; }
}
public ChangesetBuilder(WorkQueue workQueue, Logger logger, RevisionAnalyzer revisionAnalyzer)
: base(workQueue, logger)
{
this.revisionAnalyzer = revisionAnalyzer;
}
public void BuildChangesets()
{
workQueue.AddLast(delegate(object work)
{
logger.WriteSectionSeparator();
LogStatus(work, "Building changesets");
var stopwatch = Stopwatch.StartNew();
var pendingChangesByUser = new Dictionary<string, Changeset>();
foreach (var dateEntry in revisionAnalyzer.SortedRevisions)
{
var dateTime = dateEntry.Key;
foreach (Revision revision in dateEntry.Value)
{
// determine target of project revisions
var actionType = revision.Action.Type;
var namedAction = revision.Action as VssNamedAction;
var targetFile = revision.Item.PhysicalName;
if (namedAction != null)
{
targetFile = namedAction.Name.PhysicalName;
}
// Create actions are only used to obtain initial item comments;
// items are actually created when added to a project
var creating = (actionType == VssActionType.Create ||
(actionType == VssActionType.Branch && !revision.Item.IsProject));
// Share actions are never conflict (which is important,
// since Share always precedes Branch)
var nonconflicting = creating || (actionType == VssActionType.Share);
// look up the pending change for user of this revision
// and flush changes past time threshold
var pendingUser = revision.User;
Changeset pendingChange = null;
LinkedList<string> flushedUsers = null;
foreach (var userEntry in pendingChangesByUser)
{
var user = userEntry.Key;
var change = userEntry.Value;
// flush change if file conflict or past time threshold
var flush = false;
var timeDiff = revision.DateTime - change.DateTime;
if (timeDiff > anyCommentThreshold)
{
if (HasSameComment(revision, change.Revisions.Last.Value))
{
string message;
if (timeDiff < sameCommentThreshold)
{
message = "Using same-comment threshold";
}
else
{
message = "Same comment but exceeded threshold";
flush = true;
}
logger.WriteLine("NOTE: {0} ({1} second gap):",
message, timeDiff.TotalSeconds);
}
else
{
flush = true;
}
}
else if (!nonconflicting && change.TargetFiles.Contains(targetFile))
{
logger.WriteLine("NOTE: Splitting changeset due to file conflict on {0}:",
targetFile);
flush = true;
}
if (flush)
{
AddChangeset(change);
if (flushedUsers == null)
{
flushedUsers = new LinkedList<string>();
}
flushedUsers.AddLast(user);
}
else if (user == pendingUser)
{
pendingChange = change;
}
}
if (flushedUsers != null)
{
foreach (string user in flushedUsers)
{
pendingChangesByUser.Remove(user);
}
}
// if no pending change for user, create a new one
if (pendingChange == null)
{
pendingChange = new Changeset();
pendingChange.User = pendingUser;
pendingChangesByUser[pendingUser] = pendingChange;
}
// update the time of the change based on the last revision
pendingChange.DateTime = revision.DateTime;
// add the revision to the change
pendingChange.Revisions.AddLast(revision);
// track target files in changeset to detect conflicting actions
if (!nonconflicting)
{
pendingChange.TargetFiles.Add(targetFile);
}
// build up a concatenation of unique revision comments
var revComment = revision.Comment;
if (revComment != null)
{
revComment = revComment.Trim();
if (revComment.Length > 0)
{
if (string.IsNullOrEmpty(pendingChange.Comment))
{
pendingChange.Comment = revComment;
}
else if (!pendingChange.Comment.Contains(revComment))
{
pendingChange.Comment += "\n" + revComment;
}
}
}
}
}
// flush all remaining changes
foreach (var change in pendingChangesByUser.Values)
{
AddChangeset(change);
}
stopwatch.Stop();
logger.WriteSectionSeparator();
logger.WriteLine("Found {0} changesets in {1:HH:mm:ss}",
changesets.Count, new DateTime(stopwatch.ElapsedTicks));
});
}
private bool HasSameComment(Revision rev1, Revision rev2)
{
return !string.IsNullOrEmpty(rev1.Comment) && rev1.Comment == rev2.Comment;
}
private void AddChangeset(Changeset change)
{
changesets.AddLast(change);
int changesetId = changesets.Count;
DumpChangeset(change, changesetId);
}
private void DumpChangeset(Changeset changeset, int changesetId)
{
var firstRevTime = changeset.Revisions.First.Value.DateTime;
var changeDuration = changeset.DateTime - firstRevTime;
logger.WriteSectionSeparator();
logger.WriteLine("Changeset {0} - {1} ({2} secs) {3} {4} files",
changesetId, changeset.DateTime, changeDuration.TotalSeconds, changeset.User,
changeset.Revisions.Count);
if (!string.IsNullOrEmpty(changeset.Comment))
{
logger.WriteLine(changeset.Comment);
}
logger.WriteLine();
foreach (var revision in changeset.Revisions)
{
logger.WriteLine(" {0} {1}@{2} {3}",
revision.DateTime, revision.Item, revision.Version, revision.Action);
}
}
}
}