forked from WilbertOnGithub/TFS2GIT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTfs2Git.ps1
368 lines (296 loc) · 9.95 KB
/
Tfs2Git.ps1
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# Script that copies the history of an entire Team Foundation Server repository to a Git repository.
# Original author: Wilbert van Dolleweerd ([email protected])
#
# Contributions from:
# - Patrick Slagmeulen
# - Tim Kellogg ([email protected])
# - Mike Glenn ([email protected])
#
# Assumptions:
# - MSysgit is installed and in the PATH
# - Team Foundation commandline tooling is installed and in the PATH (tf.exe)
Param
(
[Parameter(Mandatory = $True)]
[string]$TFSRepository,
[string]$Server,
[string]$GitRepository = "",
[string]$WorkspaceName = "TFS2GIT",
[int]$StartingCommit,
[int]$EndingCommit,
[string]$UserMappingFile,
# create lightweight tags on the first and list imported commits
[switch]$t
)
function Exec([scriptblock]$cmd, [string]$errorMessage = "Error executing command: " + $cmd) {
& $cmd
if ($LastExitCode -ne 0) {
throw $errorMessage
}
}
function CheckPath([string]$program)
{
$count = (gcm -CommandType Application $program -ea 0 | Measure-Object).Count
if($count -le 0)
{
Write-Host "$program must be found in the PATH!"
Write-Host "Aborting..."
exit
}
}
# Do some sanity checks on specific parameters.
function CheckParameters
{
if($Script:GitRepository -eq "") {
$index = $TFSRepository.LastIndexOf("/") + 1;
$Script:GitRepository = $TFSRepository.SubString($index, $TFSRepository.Length - $index) + ".git";
}
Write-Host "Git Repo will be named: $Script:GitRepository"
# If Starting and Ending commit are not used, simply ignore them.
if (!$StartingCommit -and !$EndingCommit)
{
return;
}
if (!$StartingCommit -or !$EndingCommit)
{
Write-Host "You must supply values for both parameters StartingCommit and EndingCommit"
Write-Host "Aborting..."
exit
}
if ($EndingCommit -le $StartingCommit)
{
if ($EndingCommit -eq $StartingCommit)
{
Write-Host "Parameter StartingCommit" $StartingCommit "cannot have the same value as the parameter EndingCommit" $EndingCommit
Write-Host "Aborting..."
exit
}
Write-Host "Parameter EndingCommit" $EndingCommit "cannot have a lower value than parameter StartingCommit" $StartingCommit
Write-Host "Aborting..."
exit
}
}
# When doing a partial import, check if specified commits are actually present in the history
function AreSpecifiedCommitsPresent([array]$ChangeSets)
{
[bool]$StartingCommitFound = $false
[bool]$EndingCommitFound = $false
foreach ($ChangeSet in $ChangeSets)
{
if ($ChangeSet.ChangeSet -eq $StartingCommit)
{
$StartingCommitFound = $true
}
if ($ChangeSet.ChangeSet -eq $EndingCommit)
{
$EndingCommitFound = $true
}
}
if (!$StartingCommitFound -or !$EndingCommitFound)
{
if (!$StartingCommitFound)
{
Write-Host "Specified starting commit" $StartingCommit "was not found in the history of" $TFSRepository
Write-Host "Please check your starting commit parameter."
}
if (!$EndingCommitFound)
{
Write-Host "Specified ending commit" $EndingCommit "was not found in the history of" $TFSRepository
Write-Host "Please check your ending commit parameter."
}
Write-Host "Aborting..."
exit
}
}
# Build an array of changesets that are between the starting and the ending commit.
function GetSpecifiedRangeFromHistory
{
return GetAllChangeSetsFromHistory | Where-Object {($_.ChangeSet -ge $StartingCommit) -and ($_.ChangeSet -le $EndingCommit)}
}
function GetTemporaryDirectory
{
return $env:temp + "\workspace"
}
# Creates a hashtable with the user account name as key and the name/email address as value
function GetUserMapping
{
$UserMapping = @{}
if ((Test-Path $UserMappingFile))
{
Write-Host "Reading user mapping file" $UserMappingFile
Get-Content $UserMappingFile | foreach { [regex]::Matches($_, "^([^=#]+)=(.*)$") } | foreach { $userMapping[$_.Groups[1].Value] = $_.Groups[2].Value }
#foreach ($key in $userMapping.Keys)
#{
# Write-Host $key "=>" $userMapping[$key]
#}
}
else {
Write-Host "Could not read user mapping file" $UserMappingFile
}
return $UserMapping
}
function PrepareWorkspace
{
$TempDir = GetTemporaryDirectory
# Remove the temporary directory if it already exists.
if (Test-Path $TempDir)
{
remove-item -path $TempDir -force -recurse
}
md $TempDir | Out-null
Write-Host "Creating TFS workspace for $TFSRepository"
# Create the workspace and map it to the temporary directory we just created.
exec { tf workspace /delete $TFSServer $WorkspaceName /noprompt | Out-null }
exec { tf workspace /new $TFSServer /noprompt /comment:"Temporary workspace for converting a TFS repository to Git" $WorkspaceName }
exec { tf workfold /unmap $TFSServer /workspace:$WorkspaceName $/ }
exec { tf workfold /map $TFSServer /workspace:$WorkspaceName $TFSRepository $TempDir }
}
# Retrieve the history from Team Foundation Server, parse it line by line,
# and use a regular expression to retrieve the individual changeset numbers.
function GetAllChangesetsFromHistory
{
# read the usermapping file.
$UserMapping = GetUserMapping
Write-Host "Retrieving history for $TFSRepository"
$content = tf history $TFSServer $TFSRepository /recursive /noprompt /format:detailed | Out-String
$msg = $content -split "^-{100,}", 0, "multiline"
$sets = @()
$msg | Where-Object { $_.Length -gt 0 } | ForEach-Object {
$changeset = ((Select-String -InputObject $_ -Pattern "(?m)^Changeset: (?<Changeset>.*)$")| select -expand Matches | foreach {$_.groups["Changeset"].value})
$date = [DateTime]((Select-String -InputObject $_ -Pattern "(?m)^Date: (?<Date>.*)$")| select -expand Matches | foreach {$_.groups["Date"].value})
$user = ((Select-String -InputObject $_ -Pattern "(?m)^User: (?:\w+\\)?(?<User>\w+)")| select -expand Matches | foreach {$_.groups["User"].value})
$comment = [regex]::Match($_, "(?s)Comment:(?<Message>.*?)Items:").Groups[1].Value.Trim()
if($comment -ne "") {
$comment += "`n"
}
$comment += "Changeset: $changeset"
$output = New-Object PsObject
Add-Member -InputObject $output NoteProperty ChangeSet ([int]$changeset)
Add-Member -InputObject $output NoteProperty Date ($date)
Add-Member -InputObject $output NoteProperty Comment ($comment)
Add-Member -InputObject $output NoteProperty FullMessage ($_.Trim())
#Add-Member -InputObject $output NoteProperty TfsUser ($user)
if($UserMapping.ContainsKey($user)){
Add-Member -InputObject $output NoteProperty User ($userMapping[$user])
}
else {
Add-Member -InputObject $output NoteProperty User ($user)
}
$sets += $output
}
return ($sets | Sort-Object -Property ChangeSet)
}
# Actual converting takes place here.
function Convert ([array]$ChangeSets)
{
$TemporaryDirectory = GetTemporaryDirectory
# Initialize a new git repository.
Write-Host "Creating empty Git repository at $TemporaryDirectory"
pushd $TemporaryDirectory
git init -q
# Create a gitignore file to keep TFS version control files,
# user files and cache files from being added to the new repo
@"
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
## https://github.com/github/gitignore
*.suo
*.user
*.cache
*.vssscc
*.vspscc
"@ | Out-File .gitignore -encoding ascii
# Let git disregard casesensitivity for this repository (make it act like Windows).
# Prevents problems when someones only changes case on a file or directory.
git config core.ignorecase true
$totalcount = $ChangeSets.length
Write-Host "Preparing to retrieve $totalcount ChangeSets from $TFSRepository into $TemporaryDirectory"
[bool]$RetrieveAll = $true
[int]$count = 1
$ChangeSets | Foreach-Object {
$ChangeSet = $_
$version = $ChangeSet.ChangeSet
# Retrieve sources from TFS
Write-Host "Retrieving changeset $version"
if ($count -eq 1)
{
# For the first changeset, we have to get everything.
tf get $TemporaryDirectory /force /recursive /noprompt /version:C$version | Out-Null
$RetrieveAll = $false
}
else
{
# Now, only get the changed files.
tf get $TemporaryDirectory /recursive /noprompt /version:C$version | Out-Null
}
# Add sources to Git index
git add -A | Out-Null
$date = $ChangeSet.Date.ToString("ddd MMM d HH:mm:ss yyyy zz00")
$user = $ChangeSet.User
$ChangeSet.Comment | Out-File ..\commit.txt -encoding ascii
Write-Host "Commiting changeset $version to git! ($count of $totalcount imported)"
git commit -q --date `"$date`" --author `"$user`" -F ..\commit.txt
# if flag set create a lightweight tag on the first commit
if($count -eq 1 -AND $t) {
git tag Start-TFS-Import
}
# mom said clean up your room!
rm ..\commit.txt
$count++
}
# if flag set create a lightweight tag on the last commit
if($t) {
git tag End-TFS-Import
}
Write-Host "Running garbage collection on git repository!"
git gc
popd
}
# Clone the repository to the directory where you started the script.
function CloneToLocalBareRepository
{
$TemporaryDirectory = GetTemporaryDirectory
# If for some reason, old clone already exists, we remove it.
if (Test-Path $GitRepository)
{
remove-item -path $GitRepository -force -recurse
}
git clone --bare $TemporaryDirectory $GitRepository
$(Get-Item -force $GitRepository).Attributes = "Normal"
Write-Host "Your converted (bare) repository can be found in the" $GitRepository "directory."
}
# Clean up leftover directories and files.
function CleanUp
{
$TempDir = GetTemporaryDirectory
Write-Host "Removing workspace from TFS"
tf workspace /delete $TFSServer $WorkspaceName /noprompt
Write-Host "Removing working directories in" $TempDir
Remove-Item -path $TempDir -force -recurse
}
# This is where all the fun starts...
function Main
{
CheckPath("git.cmd")
CheckPath("tf.exe")
CheckParameters
$TFSServer = ""
if($Server -ne "") {
$TFSServer = "/server:$Server"
}
PrepareWorkspace
if ($StartingCommit -and $EndingCommit)
{
Write-Host "Filtering history..."
AreSpecifiedCommitsPresent(GetAllChangeSetsFromHistory)
Convert(GetSpecifiedRangeFromHistory)
}
else
{
Convert(GetAllChangeSetsFromHistory)
}
CloneToLocalBareRepository
CleanUp
Write-Host "Done!"
}
Main