Git development
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx@kernel.org>
To: git@vger.kernel.org
Subject: git-rev-parse(1): not printing HEAD
Date: Tue, 6 Oct 2026 14:53:30 +0200	[thread overview]
Message-ID: <asTtplhokqqZ3FFg@debian> (raw)

[-- Attachment #1: Type: text/plain, Size: 1319 bytes --]

Hi!

I'm imitating the argument-parsing of git-rebase(1) in a shell script.
For the <branch> argument, if the user specifies a branch name, we
should use the name; if it specifies something else, we should use that
*as a detached HEAD*; and if it doesn't specify anything, we should
use the active branch (if there's one), or the detached HEAD (else).

I've implemented it this way:

	if test $# -lt 1; then
		git rev-parse --abbrev-ref HEAD;
		git rev-parse HEAD;
	else
		git rev-parse --verify --symbolic "$1";
		git rev-parse --verify "$1";
	fi \
	| sed '/^HEAD$/d' \
	| sed '1!d' \
	>"$dir/head-name";

I wonder if I'm overthinking and maybe there's a simpler way.
Or maybe this is the way.

The script above works by defaulting to HEAD if not specified, or using
the commit ID in $1 (I've shifted, so it's just the last parameter) if
specified.  In the case where HEAD is explicitly specified, the user
wants a detached HEAD, while if we've done it, we want to try the active
branch if any.

The sed(1) part (together with the second commmand in each branch) makes
sure we never keep the literal HEAD, but rather a reference or a hash.

Is this the simplest way to do it, or is there a better way to do it?


Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

                 reply	other threads:[~2026-10-06 12:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=asTtplhokqqZ3FFg@debian \
    --to=alx@kernel.org \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox