Overview

In TeamCity a plugin for Version Control System (VCS) is seen as a set of interface implementations grouped together by instances of

 (server-side part) and  (agent-side part). 

 

The server-side part of a VCS plugin is responsible the following major operations:

There are also optional parts:

The agent-side part is optional and only responsible for checking out and updating project sources on agents. In contrast to server-side checkout it offers a traditional approach to interacting between a CI system and VCS – when source code is checked out into the same location where it's built. For pros & cons of both solutions see VCS Checkout Mode.

 

You can use the source code of the existing VCS plugins as a reference, for example:

For more information on TeamCity plugins, please refer to the TeamCity Plugins section.

 

Before digging into the VCS plugin development details, it's important to understand the basic terms such as a Version, Modification, Change, Patch, and Checkout Rule, which are explained below.

Basic Terms

A Version is unambiguous representation of a particular snapshot within a repository pointed at by a VCS Root. The current version represents the head revision at the moment of obtaining.

The current version is taken by calling . The version here is an arbitrary text. It can be a representation of a transaction number, a revision number, a date, whatever suitable enough for getting a source snapshot in a particular VCS. Usually format of the version depends on a version control system, the only requirement which comes from TeamCity — it should be possible to sort changes by version in order of their happening (see
).
Version is used in several places:

TeamCity does not show Versions in the UI directly. For UI TeamCity converts a Version to its display name using
.

A Change is an atomic modification of a single file within a source repository. In other words, a change corresponds to a single increment of a file version.

A Modification is a set of changes made by some user at a certain time interval. It most closely corresponds to a single checkin transaction (commit), when a user commits all his modifications made locally to the central repository. A Modification also contains the Version of the VCS Root right after the corresponding Changes have been applied.
A collection of Modifications is what TeamCity expects as a result when asking a VCS plugin for changes.

A Patch is a set of operations to convert the directory state from one modification to another (e.g. change/add/remove file, add/remove directory).

A Checkout Rule is a way of changing default file layout.

Checkout rules allow to map the path in repository to another path on agent or to exclude some parts of repository, read more.

Checkout rules consist of include and exclude rules. Include rule can have "from" and "to" parts ("to" part allows to map path in repository to another path on agent). Mapping is performed by TeamCity itself and VCS plugin should not worry about it. However a VCS plugin can use checkout rules to speedup changes retrieval and patch building since checkout rules usually narrow a VCS Root to some its subset.

{show-if:mde=edit}

h2. Collecting Changes

Generally, the change collections cycle looks like this.

Once a VCS Root is created the first action performed on it is determining the current Version (*VcsSupportCore#getCurrentVersion(VcsRoot)*). This value is stored and used during the next checking for changes as the "from" Version. The current Version is obtained again to be used as the "to" Version. The Modifications collected are then shown as pending changes for corresponding build configurations. After the checking for changes interval passes the server requests for next portion of changes, but this time the "from" Version is replaced with the previous "current" Version. And so on.

Obtaining the current Version may be an expensive operation for some VCS. In this case some optimization can be involved by implementing (*VcsSupportCore#isCurrentVersionExpensive()*) to return true. Its method *CurrentVersionIsExpensiveVcsSupport#collectBuildChanges(VcsRoot, String, CheckoutRules)* takes only "from" Version assuming that the changes are to be collected for the head snapshot. In this case TeamCity will look for the Modification with the greatest Version in the returned Modifications and take it as the "from" parameter for the next checking cycle. If you decide to implement *CurrentVersionIsExpensiveVcsSupport*, method *VcsSupport#collectBuildChanges(VcsRoot, String, String, CheckoutRules)* can be left unimplemented.


h2. Constructing Patches

A _Patch_ is the set of all modifications of a VCS Root made between two arbitrary Versions packed into a single unit. With Patches there is no need to retrieve all the sources from the repository each time a build starts. Patches are sent to agents where they are applied to the checkout directory. Patches in TeamCity have their own format, and should be constructed using
{tc-javadoc:class=jetbrains.buildServer.vcs.patches.PatchBuilder}
When a build is about to start, the server determines for which Versions the patch is to be constructed and passes them to *VcsSupport#buildPatch(VcsRoot, String,String, PatchBuilder, CheckoutRules)*.
There are two types of patch: clean patch (if {{fromVersion}} is null) and incremental patch (if {{fromVersion}} is provided). Clean patch is just an export of files on the specified version, while incremental patch is a more complex thing. To create incremental patch you need to determine the difference between two snapshots including files and directories creations/deletions.

h2. Server-Side vs. Agent-Side Checkout

TODO
{show-if}

Server-Side Part

{show-if:mde=edit}

h2. Overview

TODO: summary class diagram

h2. Registering In TeamCity

TODO
{show-if}

Patch Building and Change Collecting Policies

{show-if:mode=edit}
TODO introduction

...
{show-if}

When implementing include rule policies it is important to understand how it works with Checkout Rules and paths. Let's consider an example with collecting changes.

Suppose, we have a VCS Root pointing to vcs://repository/project/. The project root contains the following directory structure:

{show-if:mode=edit}
TODO:PICTURE1
{show-if}

We want to monitor changes only in module1 and module2. Therefore we've configured the following checkout rules:

\+:module1

\+:module2

When collectBuildChanges(...) is invoked it will receive a VcsRoot instance that corresponds to vcs://repository/project/ and a CheckoutRules instance with two IncludeRules — one for "module1" and the other for "module2".

{show-if:mode=edit}
TODO:PICTURE2
{show-if}

If collectBuildChanges(...) utilizes VcsSupportUtil.collectBuildChanges(...) it transforms the invocation into two separate calls of CollectChangesByIncludeRule.collectBuildChange(...). If you have implemented CollectChangesByIncludeRule in the way described in the listing above you will have the following interaction.
{show-if:mode=edit}
TODO:PICTURE3
{show-if}

Now let's assume we've got a couple of changes in our sample repository, made by different users.
{show-if:mode=edit}
TODO:PICTURE4
{show-if}

The collection of ModificationData returned by VcsSupport.collectBuildChanges(...) should then be like this:
{show-if:mode=edit}
TODO:PICTURE5
{show-if}

But this is not a simple union of collections, returned by two calls of CollectChangesByIncludeRule.collectBuildChange(...). To see why let's have a closer look at the first calls.
{show-if:mode=edit}
TODO:PICTURE6
{show-if}

As you can see the paths in the resulting change must be relative to the path presented by the include rule, rather than the path in the VCS Root.

Then after collecting all changes for all the include rules VcsSupportUtil transforms the collected paths to be relative to the VCS Root's path.

Although being quite simple VcsSupportUtil.collectBuildChanges(...) has the following limitations.

Assume both changes in the example above are done by the same user within the same commit transaction. Logically, both corresponding VcsChange objects should be included into the same ModificationData instance. However, it's not true if you utilize VcsSupportUtil.collectBuildChanges(...), since a separate call is made for each include rule.

{show-if:mode=edit}
TODO:PICTURE7
{show-if}

Changes corresponding to different include rules cannot be aggregated under the same ModificationData instance even if they logically relate to the same commit transaction. This means a user will see these changes in separate change lists, which may be confusing. Experience shows that it's not very common situation when a user commits to directories monitored with different include rules. However, if the duplication is extremely undesirable an implementation should not utilize VcsSupportUtil.collectBuildChanges(...) and control CheckoutRules itself.

Another limitation is complete ignorance of exclude rules. As it said before this doesn't cause showing unneeded information in the UI. So an implementation can safely use VcsSupportUtil.collectBuildChanges(...) if this ignorance doesn't lead to significant performance problems. However, If an implementer believes the change collection speed can be significantly improved by taking into account include rules, the implementation must handle exclude rules itself.

All above is applicable to building patches using VcsSupportUtil.buildPatch(...) including the path relativity aspect.

{show-if:mode=edit}


h2. Challenges

TODO (detecting versions, grouping changes into modifications, handling checkout rules...)
{show-if}

Server-Side Caching

By default, the server caches clean patches created by VCS plugins, because clean patch construction can take significant time on large repositories. If clean patches created by your VCS plugin need not to be cached, you should return true from the method VcsSupport#ignoreServerCachesFor(VcsRoot).

Agent-Side Part

{show-if:mode=edit}
tc-ja 

h2. Overview

TODO: summary class diagram

h2. Registering in Agent

TODO ...
{show-if}

Agent-Side Checkout

Agent part of VCS plugin is optional, if it is provided then checkout can also be performed on the agent itself. This kind of checkout usually works faster but it may require additional configuration efforts, for example, if VCS plugin uses command line client then this client must be installed on all of the agents.

To enable agent-side checkout, be sure to include into agent plugin part and also enable agent-side checkout via
.