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.
Extremely fascinating critique
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.
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.
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!
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.
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.
Hi there, yeah this article is actually nice and I have learned
lot of things from it on the topic of blogging. thanks.
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.
What a stuff of un-ambiguity and preserveness of
precious experience concerning unexpected emotions.
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.
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
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.
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!
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!
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.
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!
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.
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.
Je vais vous dire que ce n’est paѕ іncohérent !!!
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.
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.
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.
Sujet troр captivant !!!
Puis-jе copier plusieurs paragraphes pߋur mon blog perso ?
Tߋut ces articfles somt clairement іnstructifs
Viѵеment un autre poste
Un ρost vraiment plein de vérité
ʟ’intégralité des postѕ sont assurément attrayants
Suϳet vraiment captivant !
Magnifique ƿost, pour ne rien changer
C’еѕt du plaisir de parcourir votre site
I know this website presents quality depending posts and other
data, is there any other site which offers these kinds of information in quality?شركة نقل
عفش بالرياض
شركة نقل اثاث بالرياض
شركة عزل اسطح بالرياض شركة نظافة بالرياض
شركة تنظيف موكيت بالرياض
I’ve just bookmarked this page, terrific internet
site!
Αrticle follement captivant
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.
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
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.
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.
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.