4

I use tf.exe in my C# code to create new branches in TFS. This is what I do:

public static bool TFBranch(string projectPath, string originName, string branchName)
{
    string branchedProject = projectPath + "\\" + branchName;

    string projectToBranch = projectPath + "\\" + originName;

    string path = Environment.ExpandEnvironmentVariables(@"branch " + projectToBranch + " " + branchedProject);
    path = path.Replace("\\", "/");

    return ExecuteProcess(path);
}

private static bool ExecuteProcess(string path)
{
    MyProcess proc = new MyProcess();

    var currentDirectory = WorkspaceHandler.GetLocalWorkspace();

    var command = @"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf.exe";

    Directory.SetCurrentDirectory(currentDirectory);
    proc.FileName = command;
    proc.Arguments = path;

    try
    {
        proc.Execute();
    }
    catch (Exception e)
    {
        _Logger.Error("Could not execute TF.exe. Exception: " + e.ToString());
        return false;
    }

    return true;
}

Mostly everything works as I except it. I branch from an directory that already contains a branch, so the new branch contains also a branch. But sometimes tf.exe creates a folder, not a branch. Is this a bug from tf.exe, or am I doing anything wrong?

Example:

This is how an branch command can look like:

enter image description here

enter image description here

-- EDIT --

Here is how I do it manually:

  1. I use the menu to branch Templates/BranchSource/Folder/Main:

enter image description here

  1. In the dialog I define the "Target Branch Name":

enter image description here

  1. The result is what I expected to get:

enter image description here

This is the same result that I get when I do it with my code. The only difference is that the code creates a folder sometimes.

L4c0573
  • 333
  • 1
  • 7
  • 23

1 Answers1

2

tf.exe can't do that, but you can do it with the tfpt.exe from the power tools after the branch had been created. (or from the Source control explorer in Visual Studio):

tfpt branches /convertToBranch

See also:

Community
  • 1
  • 1
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • Yes, I was also thinking about converting the folder to branch after branching, but that seems very dirty to me. I mean, if it works the first time, then it should also work twice. If I do it a third time tf.exe creates a branch as I expect it again. – L4c0573 Nov 23 '16 at 11:24
  • I don't think that tf.exe tries anything special it just calls the CreateBranch function on he server and waits for the result. – jessehouwing Nov 23 '16 at 11:45
  • I think so too. But when I branch manually from a folder that is already converted to a branch, the new folder also should be a branch too. Or am I wrong? As you can see in my example I branch from Templates/BranchSource/Folder/Main and Main is already a branch. If I am right then the Main folder from SecondBranchedFolder/Folder/Main should also be a branch and no folder. – L4c0573 Nov 23 '16 at 14:18
  • Not that I know of. From the UI the checkbox gets enabled by default I believe, from the commandline there is no such checkbox. – jessehouwing Nov 23 '16 at 14:24
  • Yes there is no checkbox. But as I said in the question, my code works fine. From about nine of ten times the branch is branched as I expect it. As you can see in FirstBranchedFoler/Folder/Main. This was created by my code which uses TF.exe. Whithout any kind of checkbox. But sometimes and it seems to be very randomely, it doesent work as I wish to. I just want to know why it sometimes creates a branch and sometimes a folder. – L4c0573 Nov 23 '16 at 14:30
  • It´s not what I originally wanted, but it soloves my problem. There are no branches as folders anymore. If anyone has an idea why tf.exe generates sometimes folders and sometimes branches, you are welcome to let me know :) ! – L4c0573 Nov 25 '16 at 11:35