Re: Teach "git checkout" to use git-show-ref

Previous thread: Re: bare repositories: packing and fetching by Jakub Narebski on Friday, September 15, 2006 - 11:11 am. (2 messages)

Next thread: Re: Add "git show-ref" builtin command by Junio C Hamano on Friday, September 15, 2006 - 2:00 pm. (1 message)
From: Linus Torvalds
Date: Friday, September 15, 2006 - 11:19 am

It's kind of like "git peek-remote", but works only locally (and thus 
avoids the whole overhead of git_connect()) and has some extra 
verification features.

For example, it allows you to filter the results, and to choose whether 
you want the tag dereferencing or not. You can also use it to just test 
whether a particular ref exists.

For example:

	git show-ref master

will show all references called "master", whether tags or heads or 
anything else, and regardless of how deep in the reference naming 
hierarchy they are (so it would show "refs/heads/master" but also 
"refs/remote/other-repo/master").

When using the "--verify" flag, the command requires an exact ref path:

	git show-ref --verify refs/heads/master

will only match the exact branch called "master".

If nothing matches, show-ref will return an error code of 1, and in the 
case of verification, it will show an error message.

For scripting, you can ask it to be quiet with the "--quiet" flag, which 
allows you to do things like

	git-show-ref --quiet --verify -- "refs/heads/$headname" ||
		echo "$headname is not a valid branch"

to check whether a particular branch exists or not (notice how we don't 
actually want to show any results, and we want to use the full refname for 
it in order to not trigger the problem with ambiguous partial matches).

To show only tags, or only proper branch heads, use "--tags" and/or 
"--heads" respectively (using both means that it shows tags _and_ heads, 
but not other random references under the refs/ subdirectory).

To do automatic tag object dereferencing, use the "-d" or "--dereference" 
flag, so you can do

	git show-ref --tags --dereference

to get a listing of all tags together with what they dereference.

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---

NOTE! This is against the current master branch, and does _not_ depend on 
my other ref-work. In fact, I'd argue that this is much better to merge 
first and independently of the packed references ...
From: Linus Torvalds
Date: Friday, September 15, 2006 - 2:56 pm

That way, it doesn't care how the refs are stored any more

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---

Just as an example of how to use git-show-ref rather than knowing about 
"$GIT_DIR/refs/..." paths.

This basically replaces my much hackier version that is in the "lt/refs" 
branch, but can go into the master branch independently of the 
refs-packing work.

diff --git a/git-checkout.sh b/git-checkout.sh
index 580a9e8..6e4c535 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -22,7 +22,7 @@ while [ "$#" != "0" ]; do
 		shift
 		[ -z "$newbranch" ] &&
 			die "git checkout: -b needs a branch name"
-		[ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
+		git-show-ref --verify --quiet -- "refs/heads/$newbranch" &&
 			die "git checkout: branch $newbranch already exists"
 		git-check-ref-format "heads/$newbranch" ||
 			die "git checkout: we do not like '$newbranch' as a branch name."
@@ -51,7 +51,7 @@ while [ "$#" != "0" ]; do
 			fi
 			new="$rev"
 			new_name="$arg^0"
-			if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
+			if git-show-ref --verify --quiet -- "refs/heads/$arg" ]; then
 				branch="$arg"
 			fi
 		elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
-

From: Junio C Hamano
Date: Saturday, September 16, 2006 - 2:33 am

I kind of liked the rev-parse one better for this particular
command, but only slightly (by using --verify on "$arg^0", you
were also making sure what's in refs/heads/ was a commit object,
but that check is gone with this patch.  It is not a loss,
because the original code did not check it, and nobody should be

-

Previous thread: Re: bare repositories: packing and fetching by Jakub Narebski on Friday, September 15, 2006 - 11:11 am. (2 messages)

Next thread: Re: Add "git show-ref" builtin command by Junio C Hamano on Friday, September 15, 2006 - 2:00 pm. (1 message)