hive-forge: stdin beats bare positional for comment / comment-edit (closes #379)

This commit is contained in:
damocles 2026-05-24 13:23:26 +02:00 committed by Mara
parent d4bb3e4aa0
commit 1294ca2755

View file

@ -137,24 +137,41 @@ pkgs.writeShellApplication {
} }
cmd_comment() { cmd_comment() {
# comment <number> [--body <text> | -f <file> | -] # comment <number> [--body <text> | -f <file> | -] [repo]
# Post a comment on an issue or PR. # Post a comment on an issue or PR.
# Accepts --body <text> (consistent with issue-create/pr-create), # Body sources, in priority order:
# -f <file>, - (stdin), or a bare positional body string. # --body <text> | -f <file> | - (explicit stdin)
if [ $# -lt 1 ]; then echo "usage: hive-forge comment <number> [--body <text> | -f <file> | -]" >&2; exit 1; fi # piped stdin (HEREDOC / shell pipe — `! -t 0`)
# bare positional argument
# Stdin wins over a bare positional so the natural
# hive-forge comment 377 owner/repo <<EOF ...
# form posts the HEREDOC and treats `owner/repo` as a repo
# override — instead of silently dropping the HEREDOC on the
# floor (closes #379).
if [ $# -lt 1 ]; then echo "usage: hive-forge comment <number> [--body <text> | -f <file> | -] [repo]" >&2; exit 1; fi
local _n="$1"; shift local _n="$1"; shift
local _body _repo="$HIVE_FORGE_REPO" local _body _repo="$HIVE_FORGE_REPO"
if [ $# -eq 0 ] || [ "''${1:-}" = "-" ]; then if [ "''${1:-}" = "--body" ]; then
_body=$(cat) _body="$2"; shift 2 || true
elif [ "''${1:-}" = "--body" ]; then
_body="$2"
elif [ "''${1:-}" = "-f" ]; then elif [ "''${1:-}" = "-f" ]; then
_body=$(cat "$2") _body=$(cat "$2"); shift 2 || true
elif [ "''${1:-}" = "-" ]; then
_body=$(cat); shift
elif [[ "''${1:-}" == --* ]]; then elif [[ "''${1:-}" == --* ]]; then
echo "hive-forge comment: unknown flag '$1' (did you mean --body?)" >&2; exit 1 echo "hive-forge comment: unknown flag '$1' (did you mean --body?)" >&2; exit 1
elif [ ! -t 0 ]; then
# Stdin is piped — use it for the body. Any trailing
# positional is a repo override (the #379 trap).
_body=$(cat)
elif [ $# -eq 0 ]; then
echo "hive-forge comment: no body pass --body <text>, -f <file>, - (stdin), or a bare positional" >&2; exit 1
else else
_body="$*" _body="$1"; shift
fi fi
# Any leftover positional (after the body source consumed its
# args) is a repo override, same shape as comment-edit /
# issue-edit / etc.
if [ $# -gt 0 ]; then _repo="$1"; fi
if [ -z "$(printf '%s' "$_body" | tr -d '[:space:]')" ]; then if [ -z "$(printf '%s' "$_body" | tr -d '[:space:]')" ]; then
echo "hive-forge comment: refusing to post empty comment body" >&2; exit 1 echo "hive-forge comment: refusing to post empty comment body" >&2; exit 1
fi fi
@ -167,23 +184,28 @@ pkgs.writeShellApplication {
cmd_comment_edit() { cmd_comment_edit() {
# comment-edit <id> [--body <text> | -f <file> | -] [repo] # comment-edit <id> [--body <text> | -f <file> | -] [repo]
# Edit an existing comment by its id. # Edit an existing comment by its id.
# Same precedence rules as `comment` (see #379): piped stdin
# always wins over a bare positional, so the natural HEREDOC
# form doesn't silently get its body replaced by the repo arg.
if [ $# -lt 1 ]; then echo "usage: hive-forge comment-edit <id> [--body <text> | -f <file> | -] [repo]" >&2; exit 1; fi if [ $# -lt 1 ]; then echo "usage: hive-forge comment-edit <id> [--body <text> | -f <file> | -] [repo]" >&2; exit 1; fi
local _id="$1"; shift local _id="$1"; shift
local _body _repo="$HIVE_FORGE_REPO" local _body _repo="$HIVE_FORGE_REPO"
if [ $# -eq 0 ] || [ "''${1:-}" = "-" ]; then if [ "''${1:-}" = "--body" ]; then
_body=$(cat)
elif [ "''${1:-}" = "--body" ]; then
_body="$2"; shift 2 || true _body="$2"; shift 2 || true
if [ $# -gt 0 ]; then _repo="$1"; fi
elif [ "''${1:-}" = "-f" ]; then elif [ "''${1:-}" = "-f" ]; then
_body=$(cat "$2"); shift 2 || true _body=$(cat "$2"); shift 2 || true
if [ $# -gt 0 ]; then _repo="$1"; fi elif [ "''${1:-}" = "-" ]; then
_body=$(cat); shift
elif [[ "''${1:-}" == --* ]]; then elif [[ "''${1:-}" == --* ]]; then
echo "hive-forge comment-edit: unknown flag '$1'" >&2; exit 1 echo "hive-forge comment-edit: unknown flag '$1'" >&2; exit 1
elif [ ! -t 0 ]; then
_body=$(cat)
elif [ $# -eq 0 ]; then
echo "hive-forge comment-edit: no body pass --body <text>, -f <file>, - (stdin), or a bare positional" >&2; exit 1
else else
_body="$1"; shift _body="$1"; shift
if [ $# -gt 0 ]; then _repo="$1"; fi
fi fi
if [ $# -gt 0 ]; then _repo="$1"; fi
if [ -z "$(printf '%s' "$_body" | tr -d '[:space:]')" ]; then if [ -z "$(printf '%s' "$_body" | tr -d '[:space:]')" ]; then
echo "hive-forge comment-edit: refusing to post empty comment body" >&2; exit 1 echo "hive-forge comment-edit: refusing to post empty comment body" >&2; exit 1
fi fi