TFS API Create Branch Programmatically visualization - tfs

I have a folder into tfs,and I want to take branch of this folder with creating new folder and put branch under this new folder programmatically.Normally when we do it in tfs it automatically change folder to branch.
When I use createbranch command ,it works ,create folder and under these new folder it create new branch,but branch seen like folder in tfs but I can merge it vs so it is working.If I want to change visualization I have to use second command CreateBranchObject.Is it possible to that in one command
Folder A-->take new branch
Folder A'(New Folder) --> Branch
Code Sample
int changesetId = VersionControlServer.CreateBranch(#"myfolder ", "mynewfolder\newbranch",
VersionSpec.Latest);
Changeset changeset = vcs.GetChangeset(changesetId);
changeset.Update();

This is not like in a single command and you will need to call both in sequence.

Related

Choosing prefix name on creating new branch

In our company, we use azure-devops and edited some settings about folder hierarchy (hotfix, feature, bugfix etc.) with this documentation.
We want to choose one of those prefix names and type branch name. For example, I choose feature and type login-page. Branch name will be hotfix/login-page.
I search a lot but don't any result about that. Is there any way to do that?
I choose feature and type login-page. Branch name will be
hotfix/login-page.
If you mean creating a branch name such as hotfix/login-page, then it's not supported. We can only select based on branch but cannot select the folder, that means we cannot create a branch based on a folder.
In your scenario, it will create a new branch called login-pagebranch under the hotfix folder.
That means / here will be identified as the symbol of the folder hierarchy.
If you want to create a branch under the feature folder, then you should type feature/login-page. We cannot select prefix name on creating a new branch.

Include branch name in post build event on Team Build

I would like to perform the following steps in the TFS build process:
do post build event that will copy some files from my compiled projects to another predefined directory, I'd like that directory path to include the branch name.
I'd like to be able to refer to the branch name inside my xaml workflow template as well.
The first one is rather simple. When you're using the new TFS 2013 build server and process template, you can simply add a post-build powershell script in the Build Definition Configuration, check in the script and run it during the build.
The second one is dependent on whether you're using TFVC or Git, in the first case, use the VersionControlServer class to query the BranchObjects, then check which one is the root for your working folder. Be aware though, that in TFVC multiple branches can be referenced in one workspace, so there may be multiple answers to this query, depending on which file you use the find the branchroot. A custom CodeActivity would do the trick, similar to this check in a custom checkin policy.
The code will be similar to:
IBuildDetail buildDetail = context.GetExtension<IBuildDetail>();
var workspace = buildDetail.BuildDefinition.Workspace;
var versionControlServer = buildDetail.BuildServer.TeamProjectCollection.GetService<VersionControlServer>();
var branches = versionControlServer.QueryRootBranchObjects(RecursionType.Full);
var referencedBranches = listOfFilePaths.GroupBy(
file =>
branches.SingleOrDefault(
branch => file.ServerItem.StartsWith(branch.Properties.RootItem.Item)
)
).Where(group => group.Key != null);
To get a list of all items in yo workspace, you can use Workspace.GetItems.
In case you're using Git, you have a few options as well. The simplest is to invoke the command line:
git symbolic-ref --short HEAD
or dive into LibGit2Sharp and use it to find the branch name based on the current working folder from a custom activity.
If you want to include this in an MsBuild task, this may well be possible as well. It goes a bit far for this answer to completely outline the steps required, but it's not that hard once you know what to do.
Create a custom MsBuild task that invokes the same snippet of code above, though instead of getting access to the workspace through BuildDetail.BuildDefinition.Workspace, but through the WorkStation class:
Workstation workstation = Workstation.Current;
WorkspaceInfo info = workstation.GetLocalWorkspaceInfo(path);
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(info.ServerUri);
Workspace workspace = info.GetWorkspace(collection);
VersionControlServer versionControlServer = collection.GetService<VersionControlServer>();
Once the task has been created, you can create a custom .targets file that hooks into the MsBuild process by overriding certain variables or copying data when the build is finished. You can hook into multiple Targets and define whether you need to do something before or after them.
You can either <import> these into each of your projects, or you can place it in the ImportAfter or ImportBefore folder of your MsBuild version to make it load globally. You can find the folder here:
C:\Program Files (x86)\MSBuild\{MsBuild Version}\Microsoft.Common.Targets\ImportAfter

List Source Control Files and Folder under a given TFS Path (without work-space mapping)

I'm redesigning a control that lists the contents (files and folders) under a given TFS path. The tricky part is that I don't want to create a workspace for achieving this, as my intention is just to list the contents and display the history of a selected item. The current implementation creates a local workspace mapping in the background to achieve this, is this needed? Can I attain this without a local workspace mapping?
Thanks
Joe.
Use GetItems, which does not require a workspace. For example:
TeamFoundationServer tfs = new TeamFoundationServer("http://tfs:8080/tfs/DefaultCollection");
VersionControlServer versionControl = tfs.GetService<VersionControlServer>();
ItemSet items = versionControl.GetItems(tfsPath, RecursionType.Full);

TFS - is it possible to delete merge flag from files and send commit just like a normal changeset?

Let's say I have two branches: A and child branch B. I wanna merge one changeset from branch A to B, but with one detail: I don't want flag [merge] for files in this changeset, it must look just like I edit files manualy and don't have a link on changeset from branch A. Is it possible?
Yes, the general idea is to generate a diff on the source branch with the following command
tf.exe diff [...] /recursive /format:unified /version:[...] >> diff.patch
Replace the [...] with the actual values (branch folder and versions). The documentation is on MSDN
Then use the patch utility (from sourceforge.net) to apply it the other branch:
patch.exe -p0 < diff.patch
Then check in.

Branch moved folder based on label or date

I've moved a folder in tfs using the "move" command but now I cannot create branches off the moved folder based on date or label (label was created when source was in the old folder). I can however create a branch based on "latest version". I get an error message "no items match in if I try to branch of a label. I'm guessing the label references files using the old folder before I moved it. I also get no files if I try to "get specific version" by either date or label.
I've tried to roll back moving the folder but this gives me errors such as "An unexpected error occured".
If you need to branch from points in time prior to the move, you need to specify the old name.
# fails
tf branch $/project/folder-newname $/project/folder-somebranch /version:Loldlabel
# should work
tf branch $/project/folder-oldname $/project/folder-somebranch /version:Loldlabel
The error related to rollback was that I had to roll back a folder rename first. I could then roll back moving the folder.

Resources