From: Caspar Duregger Date: Fri, 13 Nov 2026 14:28:02 GMT Subject: [PATCH 2/4] object-name: let GET_OID_GENTLY affect only failing lookups Message-ID: <20261113142804.7465-3-caspar.duregger@gmail.com> In-Reply-To: <20260212040708.GA1894049@coredump.intra.peff.net> 393bbb21c9 (object-name: make get_oid quietly return an error, 2025-06-12) introduced GET_OID_GENTLY so that a caller can turn the die() on an exhausted reflog into an error return. The check was placed after the if/else cascade that decides whether the lookup succeeded, though, so passing the flag also turns two successful lookups into errors: - "ref@{}" older than the reflog; - "ref@{N}" for a reflog with N entries. This was questioned in review [1], but the code was merged as-is. Make the check a branch of the cascade, just before the die(), so that the flag changes only how a failing lookup fails. With the flag, "ref@{}" then resolves to the oldest known value and "ref@{N}" to the "old" side of the oldest entry, as they do without it. The only gentle caller so far is "git stash export", which can now export "stash@{}" like "git stash show" can show it. The oldest entry of a stash reflog normally has a null "old" side, so "stash@{N}" past the end keeps failing. [1] https://lore.kernel.org/git/xmqqa5745ttw.fsf@gitster.g/ Signed-off-by: Caspar Duregger --- object-name.c | 9 ++++----- t/t3903-stash.sh | 9 +++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/object-name.c b/object-name.c index 13e4dc0f21..c066a7d6a2 100644 --- a/object-name.c +++ b/object-name.c @@ -812,17 +812,16 @@ static int get_oid_basic(struct repository *r, const char *str, int len, * still fill in the oid with the "old" value, * which we can use. */ - } else if (!(flags & GET_OID_GENTLY)) { + } else if (flags & GET_OID_GENTLY) { + free(real_ref); + return -1; + } else { if (flags & GET_OID_QUIETLY) { exit(128); } die(_("log for '%.*s' only has %d entries"), len, str, co_cnt); } - if (flags & GET_OID_GENTLY) { - free(real_ref); - return -1; - } } } diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 70c6031958..74ac66f22e 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -1557,6 +1557,15 @@ test_expect_success 'stash export can accept specified stashes' ' test_line_count = 2 actual ' +test_expect_success 'stash export resolves stash reflog selectors like rev-parse' ' + git stash clear && + git stash import refs/heads/bar && + git stash export --to-ref refs/heads/oldest "stash@{1970-01-02}" && + test_cmp_rev t-stash0 oldest^2 && + test_must_fail git stash export --print stash@{2} 2>err && + test_grep "unable to find stash entry stash@{2}" err +' + test_expect_success 'stash export rejects invalid arguments' ' test_must_fail git stash export --print --to-ref refs/heads/invalid 2>err && test_grep "exactly one of --print and --to-ref is required" err && -- 2.56.1.windows.1