48

Is there a way to tag a remote git repository without having cloned it locally?

In order to correlate a code repository with a config repository, I want to (as a CI build step) tag whatever is the current head of the config repository with build-n (where N is the current build number provided by jenkins).

The config repository isn't used as part of the build, I simply want an easy way to fetch the config revision as it was when for example version 1234 was built, and tagging it as "build-1234" seems like the simplest way to achieve this.

gfxmonk
  • 8,614
  • 5
  • 42
  • 53
  • 1
    How do you access this repository? For example, if this is a bare repository and you access it using SSH, I think you can just execute a tag command on it over SSH. – Legolas Aug 03 '11 at 06:56
  • @Legolas No, you can't run any command in the bare repo. – Ruslan Kabalin Aug 03 '11 at 11:01
  • I just tried running `git tag alpha master` on a bare repository, which worked. – Legolas Aug 03 '11 at 11:11
  • @Legolas Did you create your repo using 'git init --bare' or remote system, then pushed changes to it from the local branch, then entered directory on the remote server and were able to run 'git tag' there? I doubt because "bare" directory does not have a working tree by definition. Correct me if I am wrong :) – Ruslan Kabalin Aug 03 '11 at 11:49
  • I made a bare repository, then I cloned it and pushed some testfiles as an empty repository is unwanted, then I executed the tag command in the bare repository, then I pulled it to see if the new tag existed. The thing is that the working direcotry is not needed for all commands, you just enter the git directory self to execute commands. I use this type of working for maintance on my git-server. Anyhow, tagging a remote repository is, I think, not possible, as this proposal is actually tagging a bare repository which is by chance the remote of another repository. – Legolas Aug 03 '11 at 11:53
  • Opps.. I am wrong, just tested and it really works :) Though git complains about working tree absence :) Sorry @Legolas, you were right, it is good to know that such thing is possible in fact. – Ruslan Kabalin Aug 03 '11 at 11:57
  • @gfxmonk There is some discussion regarding remote tagging on [jenkins git pluginpage](https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin) It even looks like tagging functionality should be there already, see [this](https://issues.jenkins-ci.org/browse/JENKINS-5676). – Ruslan Kabalin Aug 03 '11 at 12:04

3 Answers3

15

To have this as an answer: there is at the moment no way to do remote tagging with git, but if you have access in some way to the remote (bare) repository, you may be able to tag on the remote location.

For example, if you access the git repository via SSH, you can login using SSH, go to the (bare) repository and execute the tag command (git tag build-1234 master) in the (bare) repository.

(I am not completely sure about the tool mentioned by @ruslan-kabalin)

Legolas
  • 1,432
  • 10
  • 11
  • If you use gitolite on your server, you may try [gitolite custom command](http://gitolite.com/gitolite/cookbook.html#adding-your-own-commands). – xmedeko Feb 19 '16 at 07:35
9

It's possible to tag the current commit at the tip of a branch remotely, but not (as far as I can tell) with git porcelain or plumbing. We'll have to speak to a remote git receive-pack directly.

Here's some python that uses dulwich to do what we want:

#!/usr/bin/env python

from dulwich.client import get_transport_and_path
import sys


def tag_remote_branch(repo_url, branch, tag):
    client, path = get_transport_and_path(repo_url)

    def determine_wants(refs):
        tag_ref_name = 'refs/tags/%s' % tag
        branch_ref_name = 'refs/heads/%s' % branch
        # try not to overwrite an existing tag
        if tag_ref_name in refs:
            assert refs[tag_ref_name] == refs[branch_ref_name]
        refs[tag_ref_name] = refs[branch_ref_name]
        return refs

    # We know the other end already has the object referred to by our tag, so
    # our pack should contain nothing.
    def generate_pack_contents(have, want):
        return []

    client.send_pack(path, determine_wants, generate_pack_contents)


if __name__ == '__main__':
    repo_url, branch, tag = sys.argv[1:]
    tag_remote_branch(repo_url, branch, tag)
Evan Krall
  • 2,915
  • 3
  • 23
  • 20
9

Gitlab has an API for it. Pretty confident other might have an endpoint for this. http://docs.gitlab.com/ce/api/tags.html

Pramod Setlur
  • 811
  • 1
  • 15
  • 27
  • 1
    thanks for this answer! it reminded me to check the relevant API for our system and Bitbucket Server _also_ supports this https://docs.atlassian.com/bitbucket-server/rest/6.6.1/bitbucket-rest.html#idp335 – dayer4b Sep 11 '19 at 17:33