Git development
 help / color / mirror / Atom feed
* git-rev-parse(1): not printing HEAD
@ 2026-10-06 12:53 Alejandro Colomar
  0 siblings, 0 replies; only message in thread
From: Alejandro Colomar @ 2026-10-06 12:53 UTC (permalink / raw)
  To: git

[-- 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 --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-10-06 12:53 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-06 12:53 git-rev-parse(1): not printing HEAD Alejandro Colomar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox