如何从TFVC仓库到Git仓库挑选樱桃?

时间:2018-11-06 22:10:51

标签: git migration tfvc cherry-pick git-cherry-pick

我们刚刚从TFVC迁移到Git,立即遇到一个问题-如何挑选TFVC提交给Git?

给出

  • TFVC分支$/Alice
  • TFVC分支$/Bob
  • 具有$/Alice的Git存储库迁移为alice分支,迁移了$/Bob-迁移为bob分支。
  • TFVC历史记录尚未迁移,因此整个“ $ / Alice” TFVC历史记录只是一个Git提交。 $\Bob也是如此。

问题

现在,我们发现$/Alice中的TFVC提交在迁移之前没有合并到$/Bob中。现在,在迁移之后,我们意识到需要将其放在bob分支中。大笨蛋。

我说的是一个巨大的变化-许多文件。因此,手动区分文件并复制更改不是很可行。我需要使流程尽可能自动化。

我到目前为止所做的

我认为我应该为有问题的TFVC变更集创建补丁。所以,这是代码(假设我需要选择commit 123):

$files = (tf changeset /noprompt 123 | sls '\$/') -replace '^[^$]+',''
$files |% { tf diff /version:C122~C123 /format:unified $_ } >> 123.diff

(我逐个文件地处理它,因为它比带有tf diff标志的运行/r快得多)

无论如何,我得到了这样的补丁文件:

File: BackgroundJobTests\BackgroundJobTests.csproj
===================================================================
--- Server: BackgroundJobTests.csproj;115493
+++ Server: BackgroundJobTests.csproj;389742
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
===================================================================
File: BI\a8i\a8i.csproj
===================================================================
--- Server: a8i.csproj;342293
+++ Server: a8i.csproj;389742
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
+<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
...

一个典型的Git隐藏补丁看起来有点不同:

diff --git a/Yogi.txt b/Yogi.txt
index 056fd9e..1f73d44 100644
--- a/Yogi.txt
+++ b/Yogi.txt
@@ -1 +1 @@
-yaba daba do
+yaba daba doo
diff --git a/hello.txt b/hello.txt
index ce01362..980a0d5 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1 @@
-hello
+Hello World!

在这里,我觉得我需要一些指导。也许我做错了所有事情,有一种现成的解决方案可以解决我的痛苦。也许我朝着正确的方向前进,而我所需要的只是一种“愚弄” Git来接受我的补丁作为隐藏补丁的方法。但是细节决定成败,而我却缺乏这些细节。

1 个答案:

答案 0 :(得分:1)

我最终得到了以下Powershell脚本:

param(
    [Parameter(Mandatory = $true, Position = 0)]$SrcBaseDir, 
    [Parameter(Mandatory = $true, Position = 1)]$SrcRepo,
    [Parameter(Mandatory = $true, Position = 2)]$DstBaseDir, 
    [Parameter(Mandatory = $true, Position = 3)][int]$Changeset)

[io.directory]::SetCurrentDirectory($DstBaseDir)
cd $SrcBaseDir
$files = @((tf changeset /noprompt $Changeset | sls '\$/') -replace '^[^$]+','')
Write-Host -ForegroundColor Green "Found $($files.Length) files"

cd $DstBaseDir
$GitStatus = git status --porcelain
$FailedPatches = @{}
$PatchFilePathPrefix = "$env:TEMP\$(Get-Date -Format 'yyyyMMddHHmmss')_"
$NotFound = @()
$Modified = @()
$i = 0
$files |% {
    ++$i
    $TargetFile = $_.Substring($SrcRepo.Length)
    if (!(Test-Path $TargetFile))
    {
        Write-Host -ForegroundColor Yellow "[$i] not found skipped $TargetFile"
        $NotFound += $TargetFile
    }
    elseif ($GitStatus | sls -SimpleMatch $TargetFile)
    {
        # Very important - git status returns wrong result if the case is different
        # This is why I pipe it through PowerShell Select-String command which lets me check
        # the status case insensitively
        Write-Host -ForegroundColor Yellow "[$i] already modified skipped $TargetFile"
        $Modified += $TargetFile
    }
    else 
    {
        Write-Host -ForegroundColor Green "[$i] $TargetFile"
        pushd $SrcBaseDir
        try
        {
            $patch = tf diff /version:"C$($Changeset - 1)~C$Changeset" /format:unified $_
        }
        finally
        {
            popd
        }

        $PatchFileName = "${PatchFilePathPrefix}$($TargetFile -replace '[\\/]','_').patch"
        $patch `
            -replace "^--- Server: .*","--- a/$TargetFile" `
            -replace "^\+\+\+ Server: .*","+++ b/$TargetFile" | Out-File -Encoding utf8 $PatchFileName

        $res = git apply --whitespace=nowarn $PatchFileName 2>&1
        $failed = $LASTEXITCODE
        if ($failed)
        {
            $bytes = [io.file]::ReadAllBytes($TargetFile)
            $BOMCount = 0
            while ($bytes[$BOMCount] -gt 127)
            {
                ++$BOMCount
            }
            if ($BOMCount)
            {
                $fs = [io.file]::Create($TargetFile)
                $fs.Write($bytes, $BOMCount, $bytes.Length - $BOMCount)
                $fs.Close()
                $res = git apply --whitespace=nowarn $PatchFileName 2>&1
                $failed = $LASTEXITCODE
                if ($failed)
                {
                    [io.file]::WriteAllBytes($TargetFile, $bytes)
                }
                else
                {
                    $NewBytes = [io.file]::ReadAllBytes($TargetFile)
                    $fs = [io.file]::Create($TargetFile)
                    $fs.Write($bytes, 0, $BOMCount)
                    $fs.Write($NewBytes, 0, $NewBytes.Length)
                    $fs.Close();
                }
            }
        }
        if ($failed)
        {
            $res |% {
                if ($_ -is [Management.Automation.ErrorRecord])
                {
                    $_.Exception.Message
                }
                else
                {
                    $_
                }
            } | Write-Host -ForegroundColor Red
            $FailedPatches[$TargetFile] = $PatchFileName
        }
        else 
        {
            del $PatchFileName
        }
    }
}
@{
    Failed = $FailedPatches
    NotFound = $NotFound
    AlreadyModified = $Modified
}