Skip to content

Instantly share code, notes, and snippets.

@InPermutation
Created September 19, 2013 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InPermutation/6625573 to your computer and use it in GitHub Desktop.
Save InPermutation/6625573 to your computer and use it in GitHub Desktop.
Check if a treeish is a ref or a (partial) sha
$treeish = "my_tag"
# $treeish = "my_branch"
# $treeish = "900fe0a22ea332fcbfdb4b0bc7136760ea30cc19"
# $treeish = "900fe0a22"
$exists = [bool](git rev-parse --quiet --verify $treeish)
$ref = [bool](git show-ref $treeish)
$sha = -not $ref
# Check if it's a tag. (Careful; --list uses shell wildcard syntax)
$tag = [bool](git tag --list $treeish)
@InPermutation
Copy link
Author

I didn't test this with ancestry references (HEAD^, 900fe0a22^2) or reflog shortnames {HEAD@{2.months.ago}}, so those will probably give you bogus results.

@InPermutation
Copy link
Author

Wow, and I totally butchered the definition of tree-ish. Oh well.

@InPermutation
Copy link
Author

(e.g. master:test.bat would set $exists=$true and $sha=$true, which is obviously bogus.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment