Home > General > Syncing TFS with a local directory

Syncing TFS with a local directory

I started learning PowerShell in order to automate some of the more tedious activities in TFS. For example, I needed to treat a local directory as the source of truth instead of the version in source control. I came across this blog post that provides a script that seemed to do exactly what I needed. The first time I executed this script on a directory with over 10,000 sub-directories and files, it took 10 minutes to finish.

Here is phase 1 of the script:

# Phase 1: add all local files into TFS which aren't under source control yet
$items = Get-ChildItem -Recurse 

foreach($item in $items) {
	$localItem = $item.FullName
	$serverItem = Get-TfsChildItem -Item "$localItem"
	
	if (!$serverItem -and !($pendingAdds -contains $localItem)) {
		# if there's no server item AND there's no a pending Add
		write "No such item as '$localItem' on the server, adding"
		Add-TfsPendingChange -Add "$localItem"
	}
}

With over 10,000 files in the local directory, calling Get-TfsChildItem for each item wasn’t time efficient. Running phase 1 took approximately 3 minutes.

Looking at the help page for Get-TfsChildItem, we may pass an array QualifiedItemSpec[] instead of a single item. Instead of calling Get-TfsChildItem thousands of time, we can call it just once.

# Phase 1: add all local files into TFS which aren't under source control yet
$items = Get-ChildItem -Recurse | % { $_.FullName }

$serverItems = Get-TfsChildItem -Item $items

foreach($serverItem in $serverItems){
   if (!$serverItem -and !($pendingAdds -contains $localItem)) {
      write "No such item as '$localItem' on the server"
   }
}

Running the updated script still took approximately 2 minutes. It isn’t much of an improvement, but it is better nonetheless. Phase 2 of the script has the same problem.

# Phase 2: delete all subfolder/files in TFS if there's no local subfolder/file for them anymore, and check out other items
$items = Get-TfsChildItem -Recurse

foreach($item in $items) {
   $serverItem = $item.ServerItem
   $localItem = Get-TfsItemProperty -Item $serverItem 
   
   # Do other stuff...
}

For the entire collection of files found in TFS, the script will iterate through and call Get-TfsItemProperty for each file. Running phase 2 took approximately 6 minutes. Again, we can update the script to call Get-TfsItemProperty once by passing it an array.

# Phase 2: delete all subfolder/files in TFS if there's no local subfolder/file for them anymore, and check out other items
$items = Get-TfsChildItem -Recurse
$itemProperties = Get-TfsItemProperty -Item $items

foreach($item in $itemProperties) {
   $serverItem = $item.SourceServerItem
   $localItem = $item.LocalItem
   
   # Do other stuff...
}

Running the updated script still took approximately 3 minutes. Better, but still slow. After reading the help page for each cmdlet more carefully, I finally noticed that most of these cmdlets offer the -Recurse switch. Instead of iterating through each of the files in the local workspace and server, we can use the top directory along with the -Recurse switch.

$localItems = Get-ChildItem -Recurse | % { $_.FullName }
$serverItemProperties = Get-TfsItemProperty -Item . -Recurse
$serverItems = $serverItemProperties | % { $_.LocalItem }

# Phase 1: add all local files into TFS which aren't under source control yet
foreach ($item in $localItems) {
   if (!($serverItems -contains $item) -and !($pendingAdds -contains $item)) {
      write "No such item as '$localItem' on the server, adding"
      Add-TfsPendingChange -Add "$item"
   }  
}

# Phase 2: delete all subfolder/files in TFS if there's no local subfolder/file for them anymore, and check out
foreach($item in $serverItemProperties) {  
	$serverItem = $item.SourceServerItem
	$localItem = $item.LocalItem
	
	# Do other stuff...
}

The entire script runs in just 16 seconds instead of the original 10 minutes.

  1. July 19, 2014 at 8:17 pm

    Extremely fascinating critique

  2. July 25, 2014 at 4:26 pm

    A personal injury lawyer is a lawyer who provides legal representation to those who claim to have been injured, physically
    or psychologically, as a result of the negligence or wrongdoing of
    another person, company, government agency, or
    other entity. In most cases, personal injury attorneys need certification programs for them
    to be recognized as qualified specialists. However, personal injury
    claims and trials can be very tricky, causing plaintiffs to lose the case.

  3. July 26, 2014 at 6:38 am

    You want a successful lawyer, of course, and one who has years of experience.

    The personal injury lawyers are perfect for any legal issues requiring a mediation or arbitration, but when a trial is required, they have the
    expertise to represent your case in the court of law.
    People who are involved in such accidents often seek compensation from
    the parties who responsible for their injuries.

  4. August 2, 2014 at 9:06 pm

    I like the helpful info you provide in your articles.
    I will bookmark your blog and check again here frequently. I am quite sure
    I will learn a lot of new stuff right here! Best of luck
    for the next!

  5. August 3, 2014 at 3:16 pm

    When some one searches for his required thing, therefore he/she
    desires to be available that in detail, so that thing is maintained over here.

  6. August 4, 2014 at 6:31 pm

    No matter if some one searches for his required thing, so
    he/she wants to be available that in detail, so
    that thing is maintained over here.

  7. August 8, 2014 at 9:15 pm

    Hi there, yeah this article is actually nice and I have learned
    lot of things from it on the topic of blogging. thanks.

  8. August 9, 2014 at 5:26 am

    Some of the commonly used electrical gadgets are batteries
    and inverters. Test equipment is generally used for testing:
    The voltage The measurement of current, resistance,
    induction or capacitance The use of wrong instrument can not only harm the
    electric flow but also cause loss of life and damage to machinery.
    Deformation alter the electrical resistance strain gauge, and previously calibrated display can obtain readings in the desired units
    of power.

  9. August 24, 2014 at 2:28 pm

    What a stuff of un-ambiguity and preserveness of
    precious experience concerning unexpected emotions.

  10. September 1, 2014 at 7:54 am

    Normally I do not read post on blogs, however I wish to say that this
    write-up very compelled me to check out and do so!
    Your writing taste has been amazed me. Thank
    you, very great post.

  11. September 1, 2014 at 11:31 am

    This is really attention-grabbing, You’re an overly professional blogger.
    I’ve joined your feed and stay up for searching
    for more of your wonderful post. Additionally,
    I have shaded your web site in my social networks

  12. September 1, 2014 at 1:30 pm

    After looking over a few of the blog posts on your web site, I honestly appreciate your way of
    writing a blog. I added it to my bookmark website list and will be checking back soon. Take a look
    at my website as well and let me know your opinion.

  13. September 1, 2014 at 1:45 pm

    My brother recommended I may like this web
    site. He used to be entirely right. This submit actually made my day.
    You cann’t imagine just how much time I had spent for
    this info! Thank you!

  14. September 1, 2014 at 2:30 pm

    Very nice post. I just stumbled upon your blog and wished to say that I have really enjoyed browsing your blog posts.
    In any case I will be subscribing to your feed and I hope you write again soon!

  15. September 12, 2014 at 5:30 am

    That¹s why I recommend lead generation companies.

    They offer useful advice and solutions for solving all operational problems you might
    be facing during your marketing strategy. It’s refreshing to have a quality
    focused medium that can set your businesses apart.

  16. September 12, 2014 at 12:56 pm

    Somone necessarily help to make severely posts I would state.
    This is the very first time I frequened your web page
    and up to now? I surprised with the analysis you made
    to create this particular put up amazing. Wonderful task!

  17. September 14, 2014 at 12:06 am

    SEO Service Provider is a full-service SEO Company.
    First of all, with telemarketing, you need to search for potential customers yourself.
    This technique is used effectively by Mc – Donald’s restaurants and other fast
    food franchises.

  18. September 20, 2014 at 6:36 am

    Magnificent goods from you, man. I have bear in mind your stuff prior to and
    you are just too great. I really like what you have acquired right here, really like what you’re stating and the
    best way during which you say it. You make it enjoyable and you still care for to
    keep it wise. I cant wait to learn much more from you. That is actually a great website.

  19. September 23, 2014 at 5:18 am

    Je vais vous dire que ce n’est paѕ іncohérent !!!

  20. September 28, 2014 at 7:04 am

    If some one needs to be updated with most up-to-date technologies
    therefore he must be visit this web page and be
    up to date everyday.

  21. October 1, 2014 at 12:58 am

    Our Western decorations are very well imbedded to add a
    niche over others. (h)Choosing strong fabrics- Graphic prints or bold colors gives home a
    new look. Starting from one room in the home to the next, one can redo
    them into arresting home d.

  22. October 2, 2014 at 12:37 am

    Hey There. I discovered your blog the use of msn.

    That is a very neatly written article. I’ll make sure to bookmark it and come back to read extra of your helpful information.

    Thanks for the post. I’ll certainly return.

  23. October 2, 2014 at 7:30 pm

    Sujet troр captivant !!!

  24. October 12, 2014 at 6:56 pm

    Puis-jе copier plusieurs paragraphes pߋur mon blog perso ?

  25. October 12, 2014 at 7:00 pm

    Tߋut ces articfles somt clairement іnstructifs

  26. October 19, 2014 at 9:59 pm

    Viѵеment un autre poste

  27. October 20, 2014 at 9:07 pm

    Un ρost vraiment plein de vérité

  28. October 20, 2014 at 10:04 pm

    ʟ’intégralité des postѕ sont assurément attrayants

  29. October 22, 2014 at 12:13 pm

    Suϳet vraiment captivant !

  30. October 23, 2014 at 8:05 pm

    Magnifique ƿost, pour ne rien changer

  31. October 23, 2014 at 10:02 pm

    C’еѕt du plaisir de parcourir votre site

  32. October 26, 2014 at 4:25 am

    I know this website presents quality depending posts and other
    data, is there any other site which offers these kinds of information in quality?شركة نقل
    عفش بالرياض
    شركة نقل اثاث بالرياض
    شركة عزل اسطح بالرياض شركة نظافة بالرياض
    شركة تنظيف موكيت بالرياض

  33. November 19, 2014 at 3:56 pm

    I’ve just bookmarked this page, terrific internet
    site!

  34. December 6, 2014 at 1:59 pm

    Αrticle follement captivant

  35. December 8, 2014 at 7:55 pm

    Simlly wish to say your article is aas astonishing.
    The clearness in your post is just soectacular aand i could
    asseume you aree an expert on this subject. Fine with your permission allow me to
    grabb your feed to keep updated with forthcoming post.
    Thanks a million and please keep up the gratifying work.

  36. January 17, 2015 at 6:15 am

    C’est celui de la sécurité, car, sans la sécurité des biens
    et des personnes il est inutile de parler de qualité de vie

  37. April 3, 2015 at 2:49 pm

    Normally I don’t learn article on blogs, but I wish to say
    that this write-up very pressured me to check out and do so!
    Your writing style has been surprised me. Thank you, very nice article.

  38. October 27, 2015 at 2:01 am

    Anyone hoping to buy their first car, or replace an existing one can be forgiven for being
    a little confused about the range of credit solutions available to finance the vehicle.
    Is there any reason why any company should offer a car loan to anybody with
    a poor credit record. It is because having your own car is most likely more beneficial to you
    in so many ways than not having one.

  39. June 4, 2016 at 11:16 pm

    Good day I am so glad I found your weblog, I really
    found you by mistake, while I was browsing on Aol for something
    else, Anyhow I am here now and would just like to say thanks
    a lot for a marvelous post and a all round exciting blog (I also
    love the theme/design), I don’t have time to read through it
    all at the moment but I have saved it and also included your RSS feeds, so when I have time I will be back to read a great deal more,
    Please do keep up the fantastic work.

  1. August 12, 2015 at 2:44 pm
  2. February 28, 2016 at 2:54 am
  3. April 21, 2016 at 12:23 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: