2

I'm writing a Puppet module which manages the branch or tag that is currently checked out on a local Git repository. I'd basically like to enable users to specify which version of a repository they'd like checked out. This could be master (a branch) or v3.6.2 (a tag).

Here's what I've got so far:

exec { 'gitolite_select_version':
    command => "git checkout ${actual_version}",
    cwd     => '/usr/src/gitolite',
    path    => '/bin:/usr/bin',
    unless  => "test \"$(git describe --contains --all HEAD)\" == ${actual_version}""
}

This works fine on branches like master, but gives bad output when on a detached HEAD like a tag. Example on master

$ git describe --contains --all HEAD
master

Example on tag:

$ git describe --contains --all HEAD
tags/v3.6.2^0

Is there a Git command which will get me the branch name or current tag in a simple, unified format?

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
  • Duplicate of http://stackoverflow.com/questions/1593051/how-to-programmatically-determine-the-current-checked-out-git-branch, http://stackoverflow.com/questions/6245570/how-to-get-current-branch-name-in-git, or http://stackoverflow.com/questions/2111042/how-to-get-the-name-of-the-current-git-branch-into-a-variable-in-a-shell-script. – Magnus Bäck Mar 12 '15 at 06:29
  • Does the `revision` parameter from `vcsrepo` not suffice for your needs? – Felix Frank Mar 12 '15 at 11:41

1 Answers1

2

Have you considered using puppetlabs-vcsrepo? This is the kind of thing that module manages:

vcsrepo { "/usr/src/gitolite":
  ensure   => present,
  provider => git,
  source   => 'git://example.com/repo.git',
  revision => $actual_version,
}
Chris Pitman
  • 12,990
  • 3
  • 41
  • 56