{"thread":{"id":"66472","subject":"git-rev-parse(1): not printing HEAD","startedAt":"2026-10-06T12:53:30Z","lastAt":"2026-10-06T12:53:30Z","messageCount":1,"participants":["Alejandro Colomar"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"554279","messageId":"asTtplhokqqZ3FFg@debian","threadId":"66472","inReplyTo":null,"subject":"git-rev-parse(1): not printing HEAD","fromName":"Alejandro Colomar","fromEmail":"alx@kernel.org","sentAt":"2026-10-06T12:53:30Z","receivedAt":"2026-10-06T12:53:30Z","isPatch":false,"sender":{"key":"alx@kernel.org","avatar":null},"body":"Hi!\n\nI'm imitating the argument-parsing of git-rebase(1) in a shell script.\nFor the <branch> argument, if the user specifies a branch name, we\nshould use the name; if it specifies something else, we should use that\n*as a detached HEAD*; and if it doesn't specify anything, we should\nuse the active branch (if there's one), or the detached HEAD (else).\n\nI've implemented it this way:\n\n\tif test $# -lt 1; then\n\t\tgit rev-parse --abbrev-ref HEAD;\n\t\tgit rev-parse HEAD;\n\telse\n\t\tgit rev-parse --verify --symbolic \"$1\";\n\t\tgit rev-parse --verify \"$1\";\n\tfi \\\n\t| sed '/^HEAD$/d' \\\n\t| sed '1!d' \\\n\t>\"$dir/head-name\";\n\nI wonder if I'm overthinking and maybe there's a simpler way.\nOr maybe this is the way.\n\nThe script above works by defaulting to HEAD if not specified, or using\nthe commit ID in $1 (I've shifted, so it's just the last parameter) if\nspecified.  In the case where HEAD is explicitly specified, the user\nwants a detached HEAD, while if we've done it, we want to try the active\nbranch if any.\n\nThe sed(1) part (together with the second commmand in each branch) makes\nsure we never keep the literal HEAD, but rather a reference or a hash.\n\nIs this the simplest way to do it, or is there a better way to do it?\n\n\nHave a lovely day!\nAlex\n\n-- \n<https://www.alejandro-colomar.es>\n"}]}