Friday, February 4, 2011

Finding Missing Snapshots in Xcode

The snapshot feature in XCode is very convenient way to change code in a project to try something out, and easily revert the changes (remember to clean all before a build and run). It is not a substitute for source code management like GIT or SVN, but it is really fast to change back and forward between versions - especially when you have a project manager who wants quick changes on previous released versions and then get back to the new features.

But there are times when things can go a bit wrong. For example, you need to spring clean your drive when it is running out of space and so you change around the project directories.

Then look at all the snapshots, and SHOCK! HORROR! All gone! What happened? Where did they go?

Of course, they are not gone, it is just that they are not linked up to the new paths for the projects. The good news is that this can be easily fixed.

XCode stores all the snapshots in a sparse image - an OS X way of having a dynamically expandable disk image. This is automatically mounted each time you startup XCode. It is located under the Library folder in your user account.


An easy way to see sparse image location is to launch Disk Utility. This indicates the mount point.

/private/var/folders/Rt/RtLAi33nHOmxhqgrelqmWk+++TM/-Tmp-/XcodeSnapshots


As the /private directory path is hidden in Finder, use Terminal to cd to the path.


But there is a problem. if you try to cd into the "-Tmp-" dir, you can't.


Macsat:RtLAi33nHOmxhqgrelqmWk+++TM ronanocsono$ cd "-Tmp-/"

-bash: cd: -T: invalid option

cd: usage: cd [-L|-P] [dir]


You can cd to the full path though, so do that. Next open the file SnapshotArchive.plist and check for the project paths that have changed.

You can open this directory in Finder if you type the "open ./" command.

See in this file that there are only 2 critical paths that need to be changed for each snapshot:

<key>SourceDirs</key>
<array>
<string>/Users/ronanocsono/Documents/code/herriazsoto/trunk/OmmWriter</string>
</array>

and

<key>ProjectPath </key>

<string>/Users/ronanocsono/Documents/code/herriazsoto/trunk/OmmWriter/OmmWriter.xcodeproj</string>


Make the changes and save the file. Quit XCode and restart it. Your snapshots are restored!

At a boot note, if you were wondering why your Time Machine backups take so damn long and the files are huge, consider that all the snapshots from XCode are stored in one sparse disk image - and this is considered a single file. If you make any changes to this then the next backup will have to save the whole sparse image. For example, a project I am working on is say 25MB of code and some images. I have been making snapshots for 2 years. This is now a total size of 964MB that must be backed up in TimeMachine. It is probably better to only make snapshots at a minimum, and delete the ones you don't need. And of course a clean up of the snapshots of projects dumped is a good idea.