简体   繁体   中英

How can I cherry pick from a TFVC repo to Git repo?

We have just migrated from TFVC to Git and immediately we have a problem - how to cherry pick TFVC commits to Git?

Given

  • TFVC branch $/Alice
  • TFVC branch $/Bob
  • Git repo with $/Alice migrated as the alice branch and $/Bob - as the bob branch.
  • The TFVC history was not migrated, so the entire TFVC history of '$/Alice' is just one Git commit. The same is true for $\\Bob .

Problem

Now we discover a TFVC commit in $/Alice that was not merged to $/Bob before the migration. Now after the migration we realize we need to have it in the bob branch. Major bummer.

I am talking about a big change - many files. Hence diffing the files manually and copying over the changes is not very feasible. I need to automate the process as much as possible.

What I did so far

I figured I should create a patch for the TFVC changeset in question. So, here is the code (assuming I need to cherry pick commit 123):

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

(I do it file by file, because it is much faster than running tf diff with /r flag)

Anyway, I get a patch file like this:

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" />
...

A typical Git stash patch looks a bit differently:

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!

And here I feel I need some guidance. Maybe I am doing it all wrong and there is an off-the-shelf solution for my pain. Or maybe I am in the right direction and all I need is a way to "fool" Git into accepting my patch as a stash patch. But devil is in the details, and I am lacking them.

I ended up with the following Powershell script:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM