Skip to content

Commit

Permalink
tools/read-version: Fix read-version when in a git worktree.
Browse files Browse the repository at this point in the history
read-version --json would report bad data when working in a worktree.
This is just because in a worktree, .git is not a directory, but
rather a metadata file that points to the another path.
  $ git worktree ../mytree
  $ cat ../mytree/.git
  gitdir: /path/to/cloud-init/.git/worktrees/mytree
  $ rm -Rf ../mytree; git worktree prune
  • Loading branch information
smoser committed Jan 24, 2018
1 parent df182de commit 30597f2
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tools/read-version
Expand Up @@ -45,14 +45,27 @@ def which(program):
return None


def is_gitdir(path):
# Return boolean indicating if path is a git tree.
git_meta = os.path.join(path, '.git')
if os.path.isdir(git_meta):
return True
if os.path.exists(git_meta):
# in a git worktree, .git is a file with 'gitdir: x'
with open(git_meta, "rb") as fp:
if b'gitdir:' in fp.read():
return True
return False


use_long = '--long' in sys.argv or os.environ.get('CI_RV_LONG')
use_tags = '--tags' in sys.argv or os.environ.get('CI_RV_TAGS')
output_json = '--json' in sys.argv

src_version = ci_version.version_string()
version_long = None

if os.path.isdir(os.path.join(_tdir, ".git")) and which("git"):
if is_gitdir(_tdir) and which("git"):
flags = []
if use_tags:
flags = ['--tags']
Expand Down

0 comments on commit 30597f2

Please sign in to comment.