Tuesday, January 8, 2008

Xcode 3 Feature: Version Control Repository Access

Xcode 3 adds support for configuring and viewing version control repositories. With Xcode 3 you can import projects and checkout files inside Xcode. All you have to do from the command line is create the repository. This article will show you how to take advantage of Xcode 3's enhanced version control capabilities. One word of caution. If you hated Xcode's version control support in earlier versions of Xcode, you'll want to stick with the command line. Xcode 3 repository features are a little flaky.

Xcode supports Subversion, Perforce, and CVS. I'm going to focus on Subversion because Mac OS X 10.5 ships with Subversion, and CVS seems to be losing popularity with developers.
Step 1: Create the Repository

If you haven't done so already, open the Terminal application and create a repository by running the svnadmin create command. I have detailed instructions you can read (PDF) on creating a repository if you need them. One thing to note is that because Mac OS X 10.5 ships with Subversion, you don't have to install it, and in Mac OS X 10.5 Subversion is in /usr/bin instead of /usr/local/bin.

Step 2: Configure Your Repository

Before you can take advantage of Xcode's repository features, you must configure your repository in Xcode. Launch Xcode and choose SCM > Configure SCM Repositories. A window similar to the following figure will open:

Click the + button to add a repository. You will be asked to name the repository and specify what version control system you're using.

Now you must specify the URL for the repostiory. A local repository's URL starts with file:// and follows with the path to the repository on your hard drive. If the path you specify does not show up in the Path field, you'll have to enter the path to your repository in the Path field.

I have not tried configuring a remote repository, but Xcode's repository configuration window has Host, Port, User, and Password fields. I assume you would fill these fields to configure a remote repository. If you're going to connect to a remote repository using SSH, click the SSH tab in the window. 

Click the OK button, and you've configured the repository.

Step 3: Open the Repositories Window

Choose SCM > Repositories to open Xcode's repositories window, which you can see in the figure below. The repositories window lets you examine the directories inside your version control repositories. Select a repository from the list on the left to examine that repository's directories. Some things you can do from the repositories window is import projects, check files out of the repository, create directories, move directories, and delete directories.

If you've created a fresh repository, I strongly recommend creating a directory inside the repository and storing your projects inside that directory. In my experience using Xcode's repository features on a fresh repository, I noticed Xcode let me add only one directory to the root of the repository. Any additional directories I tried to create were created inside the first directory I created. This is why I recommend creating a directory, naming it something like Projects, and import your individual Xcode projects into the Projects directory.

If you have a repository that you've been using for some time, you'll see directories inside the repository root for the projects you have in your repository. If you try to add or import a directory to the repository root, the directory will get added to one of the subdirectories inside the root. There are two solutions.

First, you can fire up Terminal and create a directory from the command line using the svn mkdir command. I had difficulty getting my created directory to appear in the repositories window, but after a few attempts I did get the directory to appear. Second, you can create a fresh repository for your new projects, add a Projects directory to the new repository, and use the new repository for any new projects you want to place under version control.

Step 4: Import the Project

Before importing a project, you must create an Xcode project and move the project's files to a folder. If you follow Subversion conventions, you'll go to your project folder in the Finder and add three folders named branches, tags, and trunk. Move your project files to the trunk folder.

To import a project, select the directory in the repositories window where you want to import the project (If you're following along, this will be the Projects directory you created in Xcode's repositories window) and click the Import button. An Open File dialog opens. Navigate to your project folder, type a comment, and click the Import button.

There should be a directory inside the Projects directory with the name of your project. Inside the project name directory should be the branches, tags, and trunk folders. The trunk folder should contain your project's files. If these folders don't show up, click the Reload button to refresh the repositories window.

Step 5: Check Out Files

After importing a project, you want to check out the source code files in your project. Select your trunk folder in the repositories window and click the Checkout button. A dialog box opens asking you where you want to store the checked out files on your hard drive. Navigate to where you want to store the checked out files. Normally you will store the checked out files in a folder inside your Xcode project's folder. Click the Checkout button. You'll be asked if you want to open the Xcode project. Open it if you want.

Step 6: Turn on Version Control

Although you've imported a project and checked out files, you still have to tell Xcode to use version control for the project you imported. Open the Xcode project (the project file you checked out in Step 5, not the original one you created in Xcode). Select the name of the project from the Groups and Files list and click the Info button on the project window toolbar. An information panel will open. Click the General button in the information panel. Choose your repository from the SCM Repository pop-up menu, and you're ready to use version control with Xcode in Mac OS X 10.5.