4

My company switched from TFVC to TFS Git. I'm migrating a release automation tool to support the switch.

Among other things, the tool creates a new branch for the release, as well as a new label ("tag" in Git). For TFVC, these operations are performed directly on the TFS server, and do not require a local copy of the repository. Is this still possible to achieve in Git, or would I be forced to clone the repository locally for these operations?

The two libraries I've looked at so far are Microsoft.TeamFoundation.Git.Client which seems to be the official wrapper for TFS Git REST API, and LibGit2Sharp. I am not limited to those libraries, so another option is welcome as well.

We are also in the process of upgrading from TFS 2015 to TFS 2017, so if there is an answer that only targets one of these I'd like to know as well.

EDIT 1

To be clear, by "directly on remote repository" I mean without passing through a local one. The tool is fully automated and if possible I'd rather not require file-system access.

EDIT 2

As mentioned below by @rmcsharry it is now possible to create a tag directly on the remote server. That's a nice improvement, but of course the main goals is to first create a branch directly on the remote, which is definitely possible in the web UI, just not (yet?) via the API. Here's the web UI screenshot:

enter image description here

Without that part I cannot automate the process I mentioned without passing through the local file system, which is my goal.

tsemer
  • 2,959
  • 3
  • 29
  • 26
  • What have you tried? There are documented API's for Git in TFS/VSTS that you can easily find online. What, specific difficulty are you running into? – MrHinsh - Martin Hinshelwood Dec 21 '16 at 18:03
  • 1
    @MrHinsh I've read a lot of documentation for both mentioned libraries. With Microsoft.TeamFountadion.Git.Client I could connect to the remote repository but it seems to expose only a portion of the documented Git REST API, but even in the documentation I can find no way to create a remote branch and tag directly on the remote. With LibGit2Sharp I didn't even manage to connect directly to the remote without first cloning the repository locally, so I couldn't perform ANY operation directly on the remote. Can you point me to any code in either library that operates directly on the remote? – tsemer Dec 22 '16 at 13:00
  • Use the create refs REST API endpoint: https://www.visualstudio.com/en-us/docs/integrate/api/git/refs#modify-one-or-more-refs – Edward Thomson Nov 06 '17 at 10:59

2 Answers2

5

You can now manually add a Tag to a commit in TFS (directly on the server), which may help.

You have to first select a commit, then click the ... button on the far right, which opens a drop-down menu, one option of which is to create a tag:

enter image description here

This is also exposed via the REST api. See the answer here.


UPDATE in response to EDIT 2 in question

It is also possible to create a branch on the API. The documentation refers to a branch as a 'ref':

Creating a ref is achieved by updating the ref from the nil commit (represented by 40 0s) to a different commit.

The structure is like this (note that {project} is optional):

POST https://{instance}/DefaultCollection
/{project}/_apis/git/repositories
/{repository}/refs?api-version={version}


Content-Type: application/json

[
  {
    "name": {string},
    "oldObjectId": {string},
    "newObjectId": {string}
  }
]

For example:

POST https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection
/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249
/refs?api-version=1.0

with this body, will create a new branch called 'live':

[
  {
    "name": "refs/heads/live",
    "oldObjectId": "0000000000000000000000000000000000000000",
    "newObjectId": "4b223e9c93ec3b6aaa6499f06e3ebb7c702e6106"
  },
]

Documentation here.


My TFS Version is:

enter image description here


rmcsharry
  • 5,363
  • 6
  • 65
  • 108
  • 1
    Thank you! This answers indeed the part of my question regarding creating a tag. Of course, the more important part is first creating a branch, directly on remote, via the API. Without that part I cannot automate the process I mentioned without passing through the local file system, which is my goal. I'll try to change the title and question a little to reflect that. – tsemer Nov 06 '17 at 09:45
  • @tsemer Good point. I have updated the answer to include how to create a branch via the API. – rmcsharry Nov 07 '17 at 13:50
  • Thanks! I hadn't realised (or had forgotten?) that branches and tags in git are only different by name :) Marking this as the answer. Now I only have to figure out if this has also been added to the NuGet package containing Microsoft.TeamFountadion.Git.Client, or if I have to fallback to direct web API communication. – tsemer Nov 07 '17 at 17:14
1

Currently, you can only create Git tags (local) in VS2015, but can't push it to the remote server. You have to use the git command line: git push --tags to push the tag.

But the feature of Support the ability to push Git Tags to remote has been under plan:

https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/10189500-support-the-ability-to-push-git-tags-to-remote

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • Thank you, good to know about this limitation, I guess I will have to use LibGit2Sharp then anyway :) but as you can see my question refers to operating directly on the remote, without passing through local. I guess this is simply not the Git way (which is a shame, for me). – tsemer Dec 22 '16 at 13:03
  • Yes, because git is decentralised, git is fundamentaly local. All is done in the local repository before pushing it. I don't think there is a rest api for that because Microsoft has other priorities before doing some extra stuff like that (but I could be wrong). It seems there is only read api... https://www.visualstudio.com/en-us/docs/integrate/api/git/overview – Philippe Dec 22 '16 at 23:02
  • Libgit2sharp is a very good library but if you have git installed, plain git commands could be easier to do such trivial stuff... – Philippe Dec 22 '16 at 23:04
  • I agree with Philippe, if you have git installed, plain git commands could be easier to use. – Cece Dong - MSFT Dec 23 '16 at 09:35