Sdílet prostřednictvím


PowerShell example: adding items from a changeset to a label

A recent request: "I want to be able to add a label to all files that were modified/added/whatever as part of a changeset."  Sounds like a job for PowerShell!

function label-changeset (
      [string] $serverName,
      [string] $labelName,
      [int] $changeset = $ (throw 'Usage: label-changeset <server> <labelName> <changeset>') )
{
      # get TFS object
      $tfs = get-tfs $serverName

      # set label info
      $label = new-object $tfs.versioncontrollabel ( $tfs.vcs, $labelName, $null, $null, "example label")
      
      # build up list of items
      $labelItemSpecs = @()
      $tfs.vcs.GetChangeset( $changeset).Changes | foreach {
            $itemSpec = new-object $tfs.itemspec ( $_ .Item.ServerItem, $tfs.recursiontype::none)
            $changesetSpec = new-object $tfs.changesetversionspec ( $changeset)
            $labelItemSpecs += new-object $tfs.labelitemspec ( $itemSpec, $changesetSpec, $False);
      }  

      # construct the label
      $tfs.vcs.CreateLabel( $label, $labelItemSpecs, $tfs.LabelChildOption::Replace)
}

Note the similarity to James' CreateLabel example.  The only difference is that we iterate over the items returned by GetChangeset() instead of taking the items as parameters.  And since this is PowerShell, not only does our function create the label on the server, it also returns an honest-to-god Label object you can pipe to other commands.

RICBERG470> label-changeset https://jpresto-test:8080 myLabel 10

Label Scope Status
----- ----- ------
myLabel $/r Created

Cool!  Note: this requires my modified get-tfs cmdlet.

Comments