TFS API Part 19: Merge

20/04/2009

TFS API Part 19: Merge

In my last post about TFS API TFS API Part 18: More Basic Stuff On Workspaces I demonstrate how to get / create and delete workspaces.
In this post I’ll show how to perform Merge using TFS API.

First add reference for
     Microsoft.TeamFoundation
     Microsoft.TeamFoundation.Client
     Microsoft.TeamFoundation.Common.dll
     Microsoft.TeamFoundation.VersionControl.Client.dll

located in – C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies

Download Demo Project

BeforeMerge

Step 1 – Connect TFS + Create VersionControlServer Object + Get all project list

private void btn_dp_Click(object sender, RoutedEventArgs e)

{

    DomainProjectPicker dp = new DomainProjectPicker(DomainProjectPickerMode.None);

    dp.ShowDialog();

 

    if (dp.SelectedServer != null)

    {

        tfs = new TeamFoundationServer(dp.SelectedServer.Name,new UICredentialsProvider());

        tfs.EnsureAuthenticated();

 

        //Using ICommonStructureService to get all project in TFS.

        ICommonStructureService structureService = (ICommonStructureService)tfs.GetService(typeof(ICommonStructureService));

        ProjectInfo[] projects = structureService.ListAllProjects();

        combo_projects.ItemsSource = projects;

 

        sourceControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));

        AddWorkSapces();

    }

}

 

Step 2 – Add workspaces

In order to perform merge you need workspace to work on.

private void AddWorkSapces()

{

    Workspace[] wss = GetWorkspaces();

    cob_workspace_list.Items.Clear();

    foreach (Workspace ws in wss)

    {

        WorkSpaceItem item = new WorkSpaceItem(ws.Name, ws);

        cob_workspace_list.Items.Add(item);

    }

}

 

public Workspace[] GetWorkspaces()

{

    try

    {

        return sourceControl.QueryWorkspaces(null, sourceControl.AuthenticatedUser, System.Net.Dns.GetHostName().ToString());

        //You also can get remote workspaces

        //QueryWorkspaces(worspaceName,worksapceOwner,computer)

    }

    catch

    {

        throw;

    }

}

Step 3 – Map source control folders and files

A simple view to help us select Source folder and Target folder.

private void GetPathFiles(TreeViewItem TreeItem, String path)

{

    try

    {

        RecursionType recursion = RecursionType.OneLevel;

        //Also have – Full, None

        Item[] items = null;

 

        // Get the latest version of the information for the items.

        ItemSet itemSet = sourceControl.GetItems(path, recursion);

        items = itemSet.Items;

 

        foreach (Item keyItem in items)

        {

            char[] charSeparators = new char[] { ‘/’ };

            //Using split to isolated the Project Name and the File Name

            string[] ss = keyItem.ServerItem.Split(charSeparators, StringSplitOptions.None);

 

            //!= items[0] ignore the first item, the Team Project Name

            if (keyItem != items[0])

            {

                TreeViewItem new_item = null;

                Execute exe = delegate()

                {

                    new_item = new TreeViewItem();

 

                    //Get File or Folder Name

                    string filename = keyItem.ServerItem.Replace(path + "/", string.Empty);

                    new_item.Header = filename;

                    //Saving the full path of the file/folder

                    new_item.Tag = keyItem.ServerItem;

                    TreeItem.Items.Add(new_item);

                };

                this.Dispatcher.Invoke(exe, null);

                GetPathFiles(new_item, keyItem.ServerItem);

            }

        }

    }

    catch (Exception ex)

    {

    }

}

 

Step 4 – Perform Merge

Read more about Workspace.Merge

private void Merge(Workspace workspace,string source,string target)

{

    GetStatus status = workspace.Merge(source,

                target,

                null,

                null,

                LockLevel.None,

                RecursionType.Full,

                MergeOptions.None);

 

    //public GetStatus Merge (string sourcePath,

    //        string targetPath,

    //        VersionSpec versionFrom,

    //        VersionSpec versionTo,

    //        LockLevel lockLevel,

    //        RecursionType recursion,

    //        MergeOptions mergeOptions)

 

    lbl_NumConflicts.Content = status.NumConflicts.ToString();

    lbl_NumFailures.Content = status.NumFailures.ToString();

    lbl_NumOperations.Content = status.NumOperations.ToString();

    lbl_NumWarnings.Content = status.NumWarnings.ToString();

 

}

 

Parameters:

sourcePath – The source of the merge (local or server path).
targetPath – The target of the merge (local or server path — must be mapped).
versionFrom – The starting version that may be a null reference (Nothing in Visual Basic).
versionTo – The ending version that may be a null reference (Nothing in Visual Basic).
lockLevel – The lock level to apply to each item specified by the target.
recursion – The level of recursion desired.
mergeOptions – Specifies the merge options.

    AlwaysAcceptMine - discard any changes in source, and just update merge history

    Baseless - perform baseless merge

    ForceMerge – do not look at merge history and perform merge for specified range of versions

    NoMerge - do not perform actual merge

    None - no special options

 

AfterMerge

AfterApiMerge

Download Demo Project

Add comment
facebook linkedin twitter email

Leave a Reply

3 comments

  1. Llorenç23/07/2009 ב 18:22

    I have read all your posts and that really help me …
    Thanks for that.
    Now I have a question about Merge:

    When you want to get latest version of a directory mapped to TFS, and locally there is some of the files
    that differs from theses under SC (for exemple you just edit some of them), does Merge can be used for this task or not ? (if I do just a ‘GetLatestVersion’, writable files are lost and conflicts are not resolved)

    Thanks,
    Llorenç

  2. Shai Raiten23/07/2009 ב 18:58

    Hi,

    Yes merge can perform this action.
    When using workspace.Merge and you have different files on local computer you will get status code contains:
    status.NumConflicts
    status.NumFailures
    status.NumOperations
    status.NumWarnings
    Then you can deside how to resolve those problem – overwrite or not.

  3. lielayAeronna27/01/2011 ב 17:59

    Hi, I left you a DOFOLLOW backlink on my website. This isnt a spam message, i actually did leave you a backlink on my site. If you check the top of the page you will see “Sites we like” and there will be a link to this site. Would you be kind enough to leave me a backlink? If so my website is http://crush-the-castle.com please use the anchor text “Crush The Castle” for the link and add it to a post or as a widget. Then please send me a email at backlink@crush-the-castle.com – If you want me to change your links anchor text let me know. Thanks