From patchwork Thu Jan 27 05:26:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12726279 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 125BEC433F5 for ; Thu, 27 Jan 2022 05:26:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236156AbiA0F05 (ORCPT ); Thu, 27 Jan 2022 00:26:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232965AbiA0F04 (ORCPT ); Thu, 27 Jan 2022 00:26:56 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83F60C06173B for ; Wed, 26 Jan 2022 21:26:56 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id k18so2558897wrg.11 for ; Wed, 26 Jan 2022 21:26:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IB+HySO7RhV3m+YJsU8OoafOl7+wSjN/HS8QDu1KAlA=; b=p/6lCCDj8D0Z5a9jh8zGKB28Og5yPhEYsB04ByNfIbDitme+vKysMoc0J4mvohfxN6 NGZQ0SoG82eoe5MpqGZDU68FNDVgiQ63+5xYh40pnWRn4ngEt4sXFcpY4JeNEHwBbEWa gLiA0MBS3JjQyUZvWXGtCbHNju3v/+XEZldrnoubptdOMr8g/oHb+gzCE+8Mv+Ql4092 xWolFKm0rymWEh45AiEI/l1U+yWjYfx61wEBPZMmVfeRCk+X+tNQE8yNkTjPvwU/rfzz lhf2OyX5ExLmDd9/UM3yIz3hbqxOTBmEW82JWWfZU92KMbdRgpWaCZmVcwDXoxedPkE9 +wFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IB+HySO7RhV3m+YJsU8OoafOl7+wSjN/HS8QDu1KAlA=; b=CSRSNSpre7nZM24CXF8e5pKg3qL/0mgBbotpUuMSlmPENsWfn3fzVkUKiE0/6rkm92 KxTGKhX16VFIP86m9iJx1/iJjUzvYicvt9zgNqzgElzoYV+8lb1NqMt80f0pW2CDLBth jY5PxxZUDii31xfkhAV/Gmg4a1+9Q+0Jo+m3pzIUhb/QSa75hYCw1EuC/61/jAB6CEsT 5u2GveEw3KT6jJSTxTjbPJcwVMGtqJ7M0l2NW9W7Omr0abvspbj9fvNZGzxOYXAWkwqi GV8J1jLm0Sm1z1DBzGwKWypVZxPI3m54OUW0PVpyuLm4DR/F06HB+SKdiIHEELlwZUAo 59Ew== X-Gm-Message-State: AOAM531FbWjKYHjSMpt33eZs6e3dxY+yeyv5rzl70xmQZ2KImGP7vu18 pFxLp6JMQfcx0Bok2szOBl+htFvqH0Yexg== X-Google-Smtp-Source: ABdhPJzW1XiVJGp1WTzjrfguZilSITIoOhYsxdw1EF0BlktOLue1vYXQF6l4vqxb70vSPJYCVxkPRw== X-Received: by 2002:adf:ef48:: with SMTP id c8mr1553278wrp.368.1643261214834; Wed, 26 Jan 2022 21:26:54 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z5sm6017661wmp.10.2022.01.26.21.26.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 21:26:54 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Bagas Sanjaya , Josh Steadmon , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v8 1/7] object-name tests: add tests for ambiguous object blind spots Date: Thu, 27 Jan 2022 06:26:43 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.890.gd7e422415d9 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Extend the tests for ambiguous objects to check how we handle objects where we return OBJ_BAD when trying to parse them. As noted in [1] we have a blindspot when it comes to this behavior. Since we need to add new test data here let's extend these tests to be tested under SHA-256, in d7a2fc82491 (t1512: skip test if not using SHA-1, 2018-05-13) all of the existing tests were skipped, as they rely on specific SHA-1 object IDs. For these tests it only matters that the first 4 characters of the OID prefix are the same for both SHA-1 and SHA-256. This uses strings that I mined, and have the same prefix when hashed with both. We "test_cmp" the full output to guard against any future regressions, and because a subsequent commit will tweak it. Showing a diff of how the output changes is helpful to explain those subsequent commits. The "sed" invocation in test_cmp_failed_rev_parse() doesn't need a "/g" because under both SHA-1 and SHA-256 we'll wildcard match any trailing part of the OID after our known starting prefix. We'd like to convert all of that to just "..." for the "test_cmp" which follows. 1. https://lore.kernel.org/git/YZwbphPpfGk78w2f@coredump.intra.peff.net/ Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t1512-rev-parse-disambiguation.sh | 82 +++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/t/t1512-rev-parse-disambiguation.sh b/t/t1512-rev-parse-disambiguation.sh index b0119bf8bc8..c14d88eae20 100755 --- a/t/t1512-rev-parse-disambiguation.sh +++ b/t/t1512-rev-parse-disambiguation.sh @@ -25,6 +25,88 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME . ./test-lib.sh +test_cmp_failed_rev_parse () { + dir=$1 + rev=$2 + + cat >expect && + test_must_fail git -C "$dir" rev-parse "$rev" 2>actual.raw && + sed "s/\($rev\)[0-9a-f]*/\1.../" actual && + test_cmp expect actual +} + +test_expect_success 'ambiguous blob output' ' + git init --bare blob.prefix && + ( + cd blob.prefix && + + # Both start with "dead..", under both SHA-1 and SHA-256 + echo brocdnra | git hash-object -w --stdin && + echo brigddsv | git hash-object -w --stdin && + + # Both start with "beef.." + echo 1agllotbh | git hash-object -w --stdin && + echo 1bbfctrkc | git hash-object -w --stdin + ) && + + test_must_fail git -C blob.prefix rev-parse dead && + test_cmp_failed_rev_parse blob.prefix beef <<-\EOF + error: short object ID beef... is ambiguous + hint: The candidates are: + hint: beef... blob + hint: beef... blob + fatal: ambiguous argument '\''beef...'\'': unknown revision or path not in the working tree. + Use '\''--'\'' to separate paths from revisions, like this: + '\''git [...] -- [...]'\'' + EOF +' + +test_expect_success 'ambiguous loose bad object parsed as OBJ_BAD' ' + git init --bare blob.bad && + ( + cd blob.bad && + + # Both have the prefix "bad0" + echo xyzfaowcoh | git hash-object -t bad -w --stdin --literally && + echo xyzhjpyvwl | git hash-object -t bad -w --stdin --literally + ) && + + test_cmp_failed_rev_parse blob.bad bad0 <<-\EOF + error: short object ID bad0... is ambiguous + hint: The candidates are: + fatal: invalid object type + EOF +' + +test_expect_success POSIXPERM 'ambigous zlib corrupt loose blob' ' + git init --bare blob.corrupt && + ( + cd blob.corrupt && + + # Both have the prefix "cafe" + echo bnkxmdwz | git hash-object -w --stdin && + oid=$(echo bmwsjxzi | git hash-object -w --stdin) && + + oidf=objects/$(test_oid_to_path "$oid") && + chmod 755 $oidf && + echo broken >$oidf + ) && + + test_cmp_failed_rev_parse blob.corrupt cafe <<-\EOF + error: short object ID cafe... is ambiguous + hint: The candidates are: + error: inflate: data stream error (incorrect header check) + error: unable to unpack cafe... header + error: inflate: data stream error (incorrect header check) + error: unable to unpack cafe... header + hint: cafe... unknown type + hint: cafe... blob + fatal: ambiguous argument '\''cafe...'\'': unknown revision or path not in the working tree. + Use '\''--'\'' to separate paths from revisions, like this: + '\''git [...] -- [...]'\'' + EOF +' + if ! test_have_prereq SHA1 then skip_all='not using SHA-1 for objects' From patchwork Thu Jan 27 05:26:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12726281 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8CFDC433EF for ; Thu, 27 Jan 2022 05:27:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236163AbiA0F06 (ORCPT ); Thu, 27 Jan 2022 00:26:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236157AbiA0F05 (ORCPT ); Thu, 27 Jan 2022 00:26:57 -0500 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D561C06161C for ; Wed, 26 Jan 2022 21:26:57 -0800 (PST) Received: by mail-wr1-x431.google.com with SMTP id e2so2652479wra.2 for ; Wed, 26 Jan 2022 21:26:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UU/6gUMshEycNWEKbnVcb9laMhbI36Z4CDYpXl5We0E=; b=BLOQW9oWSYmIKYEuQHBVmFeckLkOzmJEDCv/VALjsyzg5tp0rmPsGV2wYtO7M/hos2 EC9mI0rRRYnlC6ujll/YsYl8wJmo0gcjcP2qu7/quPqTmyZc9FxVHpXGtt6F1mrKAgWC RUzCx0ZtlU6/12/8WYqX8Ei1Ak1Qsa7jGQQKw1WNPPnHNYBj9h8JaiAsAA5eZhwNR/kc WJgXP3KqEeADVYwxWR43BdKj+o+Pg11vCqiSU4bs3gCqxHhcY3qhD5g/QgXQwExm1Dtz Df0nwUVBk1MLcxzM9np/I9O8dflHzB4A14mv1zSuO93yPjTuJI2fR2djzIYOVjWo36he mifg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UU/6gUMshEycNWEKbnVcb9laMhbI36Z4CDYpXl5We0E=; b=tFXadGJFuIeuAiyY9Eo5Q3OfZniY9GOD3+NzOcuMnzQpE5JFpzgeQZuIaSocm7LpKc QuTefSgVLmBvQFbOLmjfkATpWE4IVHylPTxy7vJA/xcPBRgtCcvfQsvf3cSPS6D970uQ wAe3iNwb6MJrhlHcVLNv+bnxuhq/v7G5hzCiOWDjcyi7QVFEmX2doKKiyyR2B/Bbr35F rQs4wThFhg9/9fUBEn7pmYosWfcVo+Ca/Jr8KcitUr5zv5hFXn7Q91orap4lTxgaAFUS jAZbsB+chkMiypcpAVr4CNzviNn1yQ7AcUI9gICEOumbn0ZZx5Z9A5f7fMtNK4nrZdeO 4moA== X-Gm-Message-State: AOAM531TxXP8Y5qZcW7ECQNJEhzxOADczp5FxMgLc7W3QLIg0cJGPXjb cPx1boukQ9gVzLQtqcdZYceLzZfIOnRoVg== X-Google-Smtp-Source: ABdhPJw8JwBUjIQx7DTH+i5ckVTU654W71YFBwwUNdWvQOu+cHWG8NJh05O7I6D3H9T7nFheAVdwDA== X-Received: by 2002:adf:fb10:: with SMTP id c16mr1598527wrr.200.1643261215624; Wed, 26 Jan 2022 21:26:55 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z5sm6017661wmp.10.2022.01.26.21.26.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 21:26:55 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Bagas Sanjaya , Josh Steadmon , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v8 2/7] object-name: explicitly handle OBJ_BAD in show_ambiguous_object() Date: Thu, 27 Jan 2022 06:26:44 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.890.gd7e422415d9 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Amend the "unknown type" handling in the code that displays the ambiguous object list to assert() that we're either going to get the "real" object types we can pass to type_name(), or a -1 (OBJ_BAD) return value from oid_object_info(). See [1] for the current output, and [1] for the commit that added the "unknown type" handling. We are never going to get an "unknown type" in the sense of custom types crafted with "hash-object --literally", since we're not using the OBJECT_INFO_ALLOW_UNKNOWN_TYPE flag. If we manage to otherwise unpack such an object without errors we'll die() in parse_loose_header_extended() called by sort_ambiguous() before we get to show_ambiguous_object(), as is asserted by the test added in the preceding commit. So saying "unknown type" here was always misleading, we really meant to say that we had a failure parsing the object at all, i.e. that we had repository corruption. If the problem is only that it's type is unknown we won't reach this code. So let's emit a generic "[bad object]" instead. As our tests added in the preceding commit show, we'll have emitted various "error" output already in those cases. We should do better in the truly "unknown type" cases, which we'd need to handle if we were passing down the OBJECT_INFO_ALLOW_UNKNOWN_TYPE flag. But let's leave that for some future improvement. In a subsequent commit I'll improve the output we do show, and not having to handle the "unknown type" (as in OBJECT_INFO_ALLOW_UNKNOWN_TYPE) simplifies that change. 1. 5cc044e0257 (get_short_oid: sort ambiguous objects by type, then SHA-1, 2018-05-10) 2. 1ffa26c461 (get_short_sha1: list ambiguous objects on error, 2016-09-26) Signed-off-by: Ævar Arnfjörð Bjarmason --- object-name.c | 14 ++++++++++++-- t/t1512-rev-parse-disambiguation.sh | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/object-name.c b/object-name.c index fdff4601b2c..9750634ee76 100644 --- a/object-name.c +++ b/object-name.c @@ -361,6 +361,16 @@ static int show_ambiguous_object(const struct object_id *oid, void *data) return 0; type = oid_object_info(ds->repo, oid, NULL); + + if (type < 0) { + strbuf_addstr(&desc, "[bad object]"); + goto out; + } + + assert(type == OBJ_TREE || type == OBJ_COMMIT || + type == OBJ_BLOB || type == OBJ_TAG); + strbuf_addstr(&desc, type_name(type)); + if (type == OBJ_COMMIT) { struct commit *commit = lookup_commit(ds->repo, oid); if (commit) { @@ -374,9 +384,9 @@ static int show_ambiguous_object(const struct object_id *oid, void *data) strbuf_addf(&desc, " %s", tag->tag); } - advise(" %s %s%s", +out: + advise(" %s %s", repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV), - type_name(type) ? type_name(type) : "unknown type", desc.buf); strbuf_release(&desc); diff --git a/t/t1512-rev-parse-disambiguation.sh b/t/t1512-rev-parse-disambiguation.sh index c14d88eae20..80102cc43a3 100755 --- a/t/t1512-rev-parse-disambiguation.sh +++ b/t/t1512-rev-parse-disambiguation.sh @@ -99,7 +99,7 @@ test_expect_success POSIXPERM 'ambigous zlib corrupt loose blob' ' error: unable to unpack cafe... header error: inflate: data stream error (incorrect header check) error: unable to unpack cafe... header - hint: cafe... unknown type + hint: cafe... [bad object] hint: cafe... blob fatal: ambiguous argument '\''cafe...'\'': unknown revision or path not in the working tree. Use '\''--'\'' to separate paths from revisions, like this: From patchwork Thu Jan 27 05:26:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12726282 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6DA17C433EF for ; Thu, 27 Jan 2022 05:27:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236187AbiA0F1C (ORCPT ); Thu, 27 Jan 2022 00:27:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236161AbiA0F06 (ORCPT ); Thu, 27 Jan 2022 00:26:58 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CE6DC06161C for ; Wed, 26 Jan 2022 21:26:58 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id m14so2374536wrg.12 for ; Wed, 26 Jan 2022 21:26:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=D1Z4pYdw2r7o4bLtu+9LKIhBbIJzKGv7DuIEzQnHj3w=; b=Q80s3+a378JjyXrxxoUMDi/JBg1iubT9EzI7LSo+vfGIDNNyHgXU6V05kekadvzccG /VTqauzk7BvHwVbSPitd/2etoJhtfK0r7FAbzkYBaTXHtmvgh3E7Z7wVqseBSVc2mkhF qptbF6DK/UpQDXbtPlNvkWXvgFTKVzxMupjyWt4pXlDP2J377rrrgMv7O3JIohworHvi 3j9nvUZ1ADvDOcYtqm5x2Ok2S+5GMlJ4ILdWYCJGuXqkHf1pP/QCoHZQBDrz/U7ZGt1C kd9lJeWibwUmq7FxbyAhTT9E1YEtv5Cq40h9ixWCHYLc8MTl8O4CQcadmkcuqWJlgIcZ SQBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=D1Z4pYdw2r7o4bLtu+9LKIhBbIJzKGv7DuIEzQnHj3w=; b=PCUi3ZMA+edFuRVblty6KJ0uwL+xf5bgARkUKuvfFioEjlIcOZA37v56JhURiBdpHt VSmU4Pr9iI10vAX7IgomD04fX/7uUmheqySJRLFPiWRkiTTpsaSPhAtxknxYU/GL1M4k VL5UX6cwI3ihyScBnlmod5rGRD7zHU4YHaBP5V5LSIZ2rivH9Hmxu0X/6BGmir17szPM bM2qf5STUx9s6rAdFgRe0Cl6IKEIvmCgyEEB/4NbECjDwZ0pOsc/o8/5AStZwykGv9mo /72yfFRZmJVmOb9NuEbOBPQjhg0wAVpRPpO4ErW3NNXxQOPaLhbpcu93ge1QEgFi2J7+ I+ZA== X-Gm-Message-State: AOAM532K8Gbw0RFuleTQI7zQIpsrxPkfywzpvayaL2gtvC0/2L5+luWl jzgJDx4VncT3GPYCjFIge14e42VBnUzdaA== X-Google-Smtp-Source: ABdhPJzgqPUKkU2HCva3uKdlYftSIFDK073K0Q0VxKkuFz3hymM6N3nHeJ85rNP4CxjCDgIVmyTO5Q== X-Received: by 2002:adf:a11b:: with SMTP id o27mr1442260wro.645.1643261216467; Wed, 26 Jan 2022 21:26:56 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z5sm6017661wmp.10.2022.01.26.21.26.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 21:26:55 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Bagas Sanjaya , Josh Steadmon , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v8 3/7] object-name: explicitly handle bad tags in show_ambiguous_object() Date: Thu, 27 Jan 2022 06:26:45 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.890.gd7e422415d9 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Follow-up the handling of OBJ_BAD in the preceding commit and explicitly handle those cases where parse_tag() fails, or we don't end up with a non-NULL pointer in in tag->tag. If we run into such a tag we'd previously be silent about it. We really should also be handling these batter in parse_tag_buffer() by being more eager to emit an error(), instead of silently aborting with "return -1;". One example of such a tag is the one that's tested for in "t3800-mktag.sh", where the code takes the "size < the_hash_algo->hexsz + 24" branch. But in lieu of earlier missing "error" output let's show the user something to indicate why we're not showing a tag message in these cases, now instead of showing: hint: deadbeef tag We'll instead display: hint: deadbeef tag [tag could not be parsed] Signed-off-by: Ævar Arnfjörð Bjarmason --- object-name.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/object-name.c b/object-name.c index 9750634ee76..298b742bac9 100644 --- a/object-name.c +++ b/object-name.c @@ -382,6 +382,8 @@ static int show_ambiguous_object(const struct object_id *oid, void *data) struct tag *tag = lookup_tag(ds->repo, oid); if (!parse_tag(tag) && tag->tag) strbuf_addf(&desc, " %s", tag->tag); + else + strbuf_addstr(&desc, " [tag could not be parsed]"); } out: From patchwork Thu Jan 27 05:26:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12726283 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F077FC433FE for ; Thu, 27 Jan 2022 05:27:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236182AbiA0F1D (ORCPT ); Thu, 27 Jan 2022 00:27:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236157AbiA0F07 (ORCPT ); Thu, 27 Jan 2022 00:26:59 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5211C06173B for ; Wed, 26 Jan 2022 21:26:58 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id s18so2607590wrv.7 for ; Wed, 26 Jan 2022 21:26:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NrUDksqLiAVFkzbMo8K6Bau53hIy5hMUMZ2OKJNsgh4=; b=aOZ25ZA4QgxW1ZiA9rT4a+hRgdyKQerlD61MfX4V/mNHSSSLlntfnioZNErdATzDVr C4U8iYtmGq8O762Mzgz9Aixn7gJVvsxF2zGMT2fJnw3+XZvCPtSAw2l+CgSZax8BI01x hrasbDGeBt2TqPGg2sSxgIGMke1Oxm+A+/I93PDk3ASVosrOzNhgrHh66MkBIO6/8IIg fu3zunqwpu/ODM8JsQ3qz2hEgsrznpW9lSsbYb2na+zr9jTWAzMmBb6/x4VGMZuspvqC vIcqCgflqn/kDbmLgAqwvH2He1MyDQJORCTccmtgmvrGpeVQczDnNIRu7mHXoo++cgaT hilA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NrUDksqLiAVFkzbMo8K6Bau53hIy5hMUMZ2OKJNsgh4=; b=QafDIGDhDquXWVEiLVUzxbHijKNJrw4rTYsd7h9oQWzfj+DLxmPfyBRc+v17M1VAXH iekz6HTvcwtbmJpxfAXqv/QXI3roRpvCLPrOC8sCJmIArvOzHJcU3dPVmKXEUQwC1RUH ko7fe4sEMZ6VlSTSP+lRG4ZbHDxGSXMKrVdsmDtRBr8K7RlNR4GWICh9E2FQUWXUa9Lh HkW98DwT5Z0NYoGRkcj7EaJvGcsB1ND6lGg0pE23jsOXIHlLeY1a6AyR4/dUhCTloGua sZiCvqICbWFHMepLCRctNPDTHSCNUPzv6QxqurBhLAVn1/nROsEbrUFaWkebbkZdKBXR 2AeA== X-Gm-Message-State: AOAM531443RnKk2OiYtOu3KJw5cTJaOKfB/5F5VBBAzF3+5fNJghpLSr 9a8tSuF2ilKY93Tvrkjh4ngVV+yOPOz46g== X-Google-Smtp-Source: ABdhPJx5jd7GEZj+K+0Z3fVecUFF3Bgi2aKDQtb+++I/tbqBL2dzUSs62A0GDSo1HZrR8NyNpX1Iig== X-Received: by 2002:adf:ffd0:: with SMTP id x16mr1402368wrs.489.1643261217334; Wed, 26 Jan 2022 21:26:57 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z5sm6017661wmp.10.2022.01.26.21.26.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 21:26:56 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Bagas Sanjaya , Josh Steadmon , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v8 4/7] object-name: make ambiguous object output translatable Date: Thu, 27 Jan 2022 06:26:46 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.890.gd7e422415d9 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the output of show_ambiguous_object() added in [1] and last tweaked in [2] and the preceding commit to be more friendly to translators. By being able to customize the "%s\n" format we're even ready for RTL languages, who'd presumably like to change that to "%s\n". In the case of the existing "tag [tag could not be parsed]" output we'll now instead emit "[bad tag, could not parse it]". This is consistent with the "[bad object]" output. Rephrasing the message like this is possible because we're not unconditionally adding the type_name() at the beginning. 1. 1ffa26c461 (get_short_sha1: list ambiguous objects on error, 2016-09-26) 2. 5cc044e0257 (get_short_oid: sort ambiguous objects by type, then SHA-1, 2018-05-10) Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Josh Steadmon --- object-name.c | 78 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 10 deletions(-) diff --git a/object-name.c b/object-name.c index 298b742bac9..f31b50bc315 100644 --- a/object-name.c +++ b/object-name.c @@ -356,40 +356,98 @@ static int show_ambiguous_object(const struct object_id *oid, void *data) const struct disambiguate_state *ds = data; struct strbuf desc = STRBUF_INIT; int type; + const char *hash; if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data)) return 0; + hash = repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV); type = oid_object_info(ds->repo, oid, NULL); if (type < 0) { - strbuf_addstr(&desc, "[bad object]"); + /* + * TRANSLATORS: This is a line of ambiguous object + * output shown when we cannot look up or parse the + * object in question. E.g. "deadbeef [bad object]". + */ + strbuf_addf(&desc, _("%s [bad object]"), hash); goto out; } assert(type == OBJ_TREE || type == OBJ_COMMIT || type == OBJ_BLOB || type == OBJ_TAG); - strbuf_addstr(&desc, type_name(type)); if (type == OBJ_COMMIT) { + struct strbuf date = STRBUF_INIT; + struct strbuf msg = STRBUF_INIT; struct commit *commit = lookup_commit(ds->repo, oid); + if (commit) { struct pretty_print_context pp = {0}; pp.date_mode.type = DATE_SHORT; - format_commit_message(commit, " %ad - %s", &desc, &pp); + format_commit_message(commit, "%ad", &date, &pp); + format_commit_message(commit, "%s", &msg, &pp); } + + /* + * TRANSLATORS: This is a line of ambiguous commit + * object output. E.g.: + * + * "deadbeef commit 2021-01-01 - Some Commit Message" + */ + strbuf_addf(&desc, _("%s commit %s - %s"), + hash, date.buf, msg.buf); + + strbuf_release(&date); + strbuf_release(&msg); } else if (type == OBJ_TAG) { struct tag *tag = lookup_tag(ds->repo, oid); - if (!parse_tag(tag) && tag->tag) - strbuf_addf(&desc, " %s", tag->tag); - else - strbuf_addstr(&desc, " [tag could not be parsed]"); + + if (!parse_tag(tag) && tag->tag) { + /* + * TRANSLATORS: This is a line of ambiguous + * tag object output. E.g.: + * + * "deadbeef tag Some Tag Message" + * + * The second argument is the "tag" string + * from object.c. + */ + strbuf_addf(&desc, _("%s tag %s"), hash, tag->tag); + } else { + /* + * TRANSLATORS: This is a line of ambiguous + * tag object output where we couldn't parse + * the tag itself. E.g.: + * + * "deadbeef tag [bad tag, could not parse it]" + */ + strbuf_addf(&desc, _("%s [bad tag, could not parse it]"), + hash); + } + } else if (type == OBJ_TREE) { + /* + * TRANSLATORS: This is a line of ambiguous + * object output. E.g. "deadbeef tree". + */ + strbuf_addf(&desc, _("%s tree"), hash); + } else if (type == OBJ_BLOB) { + /* + * TRANSLATORS: This is a line of ambiguous + * object output. E.g. "deadbeef blob". + */ + strbuf_addf(&desc, _("%s blob"), hash); } + out: - advise(" %s %s", - repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV), - desc.buf); + /* + * TRANSLATORS: This is line item of ambiguous object output + * from describe_ambiguous_object() above. For RTL languages + * you'll probably want to swap the "%s" and leading " " space + * around. + */ + advise(_(" %s"), desc.buf); strbuf_release(&desc); return 0; From patchwork Thu Jan 27 05:26:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12726285 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6CFC7C433EF for ; Thu, 27 Jan 2022 05:27:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236229AbiA0F1I (ORCPT ); Thu, 27 Jan 2022 00:27:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236166AbiA0F1A (ORCPT ); Thu, 27 Jan 2022 00:27:00 -0500 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4575C06161C for ; Wed, 26 Jan 2022 21:26:59 -0800 (PST) Received: by mail-wr1-x431.google.com with SMTP id w11so2631979wra.4 for ; Wed, 26 Jan 2022 21:26:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hQ5h/lD9ocJdPAkDpgDWI0K6o6E0R0dg6EXEJKKX9WM=; b=fbqJZM1ExBtt1i1kUKnnOnDBQ8+NoJToPdofekq1l+Eiuezf1fHIate8yjg/SUUQwi Bsh2LR1hc9tzookOf0Ev1x+EOB8auis9L1AP+eFj9azCT4y4wS0NjFibgYKiVQ46pTgR 93w4Tm7LtZpN0A68a8LqMfdhQ+FCz/d1whGG0var8730tPqZLxCancLHZSaQSs3tcMbP ChR9ljPWrPF3oThCRoXCdA2h8Cx1OGEIMJl2OAUhwtcPqZAjimoFo+Mr8d2kHCA1eyMA Wb1UUksFiqpGEAjPwQElcd51vvjIwJ9cYee7g75r/8WrwM3KxtYbxxxuiVSfrvNw7vMG 9PEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hQ5h/lD9ocJdPAkDpgDWI0K6o6E0R0dg6EXEJKKX9WM=; b=PAfWoJ9TVf7JbzAboTk5fKPxCDJoDrpW1HSY6GQWpd1wXyb7lF+IsHypAc2jNVaNZo eU14zBm2/T6PSvUQ7LsCbOyxtwNQODTuMupMFZNpaHAo/WPN6CQn1BWQfHX5TOQ7Os9q KnBQVDGyPFPQmXOnXMZ8jOd71a3D/jfYO1Y0/w9k/qD9tcM7iVnjY4zzm/xShrzDoA+Z iywb7e1NfasjsPnMMyN55ENbYgpYPu0INS7xCJ8L46GSf7x9SHoEU/WRIgrLjx4tI2d0 6Lg2H1VgyBw2dXX7TOdKcC69cRj2c3/NVEe3y4yRCKlQw8cl11YmLbTXpVp6NSOhL8SG HeCQ== X-Gm-Message-State: AOAM533wLpP4p8V/3X86JAcHnAzTwP15H+1GJQhmbl2yZt0IWlE2dMql OxZW7Txv9z6EoFNofs+kcatrfqU6R6nUAw== X-Google-Smtp-Source: ABdhPJw2Z3JJSLv6chNPO5eVlVGvqDz9LKksO5pGaHciMhlP6rhu7BqggGmUmwZYJepbB4Y4HlBRuw== X-Received: by 2002:a05:6000:178d:: with SMTP id e13mr1466843wrg.317.1643261218097; Wed, 26 Jan 2022 21:26:58 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z5sm6017661wmp.10.2022.01.26.21.26.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 21:26:57 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Bagas Sanjaya , Josh Steadmon , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v8 5/7] object-name: show date for ambiguous tag objects Date: Thu, 27 Jan 2022 06:26:47 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.890.gd7e422415d9 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Make the ambiguous tag object output nicer in the case of tag objects such as ebf3c04b262 (Git 2.32, 2021-06-06) by including the date in the "tagger" header. I.e.: $ git rev-parse b7e68 error: short object ID b7e68 is ambiguous hint: The candidates are: hint: b7e68c41d92 tag 2021-06-06 - v2.32.0 hint: b7e68ae18e0 commit 2019-12-23 - bisect: use the standard 'if (!var)' way to check for 0 hint: b7e68f6b413 tree hint: b7e68490b97 blob b7e68 [...] Before this we'd emit a "tag" line without a date, e.g.: hint: b7e68c41d92 tag v2.32.0 Signed-off-by: Ævar Arnfjörð Bjarmason --- object-name.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/object-name.c b/object-name.c index f31b50bc315..cbf459f5664 100644 --- a/object-name.c +++ b/object-name.c @@ -408,19 +408,24 @@ static int show_ambiguous_object(const struct object_id *oid, void *data) * TRANSLATORS: This is a line of ambiguous * tag object output. E.g.: * - * "deadbeef tag Some Tag Message" + * "deadbeef tag 2022-01-01 - Some Tag Message" * - * The second argument is the "tag" string + * The second argument is the YYYY-MM-DD found + * in the tag. + * + * The third argument is the "tag" string * from object.c. */ - strbuf_addf(&desc, _("%s tag %s"), hash, tag->tag); + strbuf_addf(&desc, _("%s tag %s - %s"), hash, + show_date(tag->date, 0, DATE_MODE(SHORT)), + tag->tag); } else { /* * TRANSLATORS: This is a line of ambiguous * tag object output where we couldn't parse * the tag itself. E.g.: * - * "deadbeef tag [bad tag, could not parse it]" + * "deadbeef [bad tag, could not parse it]" */ strbuf_addf(&desc, _("%s [bad tag, could not parse it]"), hash); From patchwork Thu Jan 27 05:26:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12726284 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 93FC7C433F5 for ; Thu, 27 Jan 2022 05:27:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236223AbiA0F1G (ORCPT ); Thu, 27 Jan 2022 00:27:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236183AbiA0F1C (ORCPT ); Thu, 27 Jan 2022 00:27:02 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF307C061753 for ; Wed, 26 Jan 2022 21:27:00 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id q9-20020a7bce89000000b00349e697f2fbso5131479wmj.0 for ; Wed, 26 Jan 2022 21:27:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ai+ih22iBD5qC48HisB1B/JYAgHQ50xFUtHNTcqIMI8=; b=hCiNsZ61GaQnqfytdYFt5NTUbz7oMvMHQVFPeyZhK3wC6vGZMl2BP/bRzwMZoNIl1Q ROYFQCVsiQishAWK2y27yEshglPKfesUm4OEGTcmPv9k0jhuAsyxjAeAqFj4MbE/RP/x N0mi+sJY9Ne3LO2Eb0xKnQVy8+bQXULZ3rsMp8B6pJ0SU/Y4lPpNEkW19L3hv+IjfGuT jPx0QHpyiLziPC2xzSEO8iuAHe6viT4P882E0DWMPVKNRjLBJ6rEpETy+CSUSIyMNL9k p20whIVeFdF239a/IKjq0/BFo8FC5/iA79FEPQP6neXIUzXPYUdTZmh3XvjjvKEr6NxA qaJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ai+ih22iBD5qC48HisB1B/JYAgHQ50xFUtHNTcqIMI8=; b=2YqYSa7O9NKVME2TV31sfGj3YLCHufDBefWdlf+qGgIPYgCBiAnl3dFWIJnAnJr2Hh 5fYw2fgK8bu4PsTFILx0Yfr0D8M06Niq36GUPYUX0A5IO0pke8Z9sXAEvEQXoEMm9ome qtB6EoUhIWzV+UWasPYTQDsGVVaf8W9y9JDgLyCQemp/AMLeku23eZ1GGf8oc2BuXgPe hAdLO5YkIG58MZrgFWEgYr/EOlccLIduA3fPKGpsPFngIR1LKWXckmOCppyVWoB5J9JS /DiQ8JLqz44klscLDWoBYuC7u7yPSQs3xpn9MorQtbzbyWvpYFGfmo8hX38e2pSJrFTs YwzQ== X-Gm-Message-State: AOAM533NjN+FiEztfiABQXdOSXRgCzXwuX6UxXilNWWl+O7BBrVw66jQ UFtsuYeaj4pEAQbEtah1W0+5kvlpjcmqSQ== X-Google-Smtp-Source: ABdhPJw8+EVrZ5ERC/hBFwez8ixSFEKYetCah1vknepZJ9i31wx1m18/5izhXxTwlQmNsORqkLWwvQ== X-Received: by 2002:a7b:cd02:: with SMTP id f2mr10227115wmj.61.1643261219008; Wed, 26 Jan 2022 21:26:59 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z5sm6017661wmp.10.2022.01.26.21.26.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 21:26:58 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Bagas Sanjaya , Josh Steadmon , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v8 6/7] object-name: iterate ambiguous objects before showing header Date: Thu, 27 Jan 2022 06:26:48 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.890.gd7e422415d9 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the "The candidates are" header that's shown for ambiguous objects to be shown after we've iterated over all of the objects. If we get any errors while doing so we don't want to split up the the header and the list as a result. The two will now be printed together, as shown in the updated testcase. As we're accumulating the lines into as "struct strbuf" before emitting them we need to add a trailing newline to the call in show_ambiguous_object(). This and the change from "The candidates are:" to "The candidates are:\n%s" helps to give translators more context. Signed-off-by: Ævar Arnfjörð Bjarmason --- object-name.c | 27 +++++++++++++++++++++++---- t/t1512-rev-parse-disambiguation.sh | 3 +-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/object-name.c b/object-name.c index cbf459f5664..6154e1ec6f8 100644 --- a/object-name.c +++ b/object-name.c @@ -351,9 +351,16 @@ static int init_object_disambiguation(struct repository *r, return 0; } +struct ambiguous_output { + const struct disambiguate_state *ds; + struct strbuf advice; +}; + static int show_ambiguous_object(const struct object_id *oid, void *data) { - const struct disambiguate_state *ds = data; + struct ambiguous_output *state = data; + const struct disambiguate_state *ds = state->ds; + struct strbuf *advice = &state->advice; struct strbuf desc = STRBUF_INIT; int type; const char *hash; @@ -452,7 +459,7 @@ static int show_ambiguous_object(const struct object_id *oid, void *data) * you'll probably want to swap the "%s" and leading " " space * around. */ - advise(_(" %s"), desc.buf); + strbuf_addf(advice, _(" %s\n"), desc.buf); strbuf_release(&desc); return 0; @@ -551,6 +558,10 @@ static enum get_oid_result get_short_oid(struct repository *r, if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) { struct oid_array collect = OID_ARRAY_INIT; + struct ambiguous_output out = { + .ds = &ds, + .advice = STRBUF_INIT, + }; error(_("short object ID %s is ambiguous"), ds.hex_pfx); @@ -563,13 +574,21 @@ static enum get_oid_result get_short_oid(struct repository *r, if (!ds.ambiguous) ds.fn = NULL; - advise(_("The candidates are:")); repo_for_each_abbrev(r, ds.hex_pfx, collect_ambiguous, &collect); sort_ambiguous_oid_array(r, &collect); - if (oid_array_for_each(&collect, show_ambiguous_object, &ds)) + if (oid_array_for_each(&collect, show_ambiguous_object, &out)) BUG("show_ambiguous_object shouldn't return non-zero"); + + /* + * TRANSLATORS: The argument is the list of ambiguous + * objects composed in show_ambiguous_object(). See + * its "TRANSLATORS" comments for details. + */ + advise(_("The candidates are:\n%s"), out.advice.buf); + oid_array_clear(&collect); + strbuf_release(&out.advice); } return status; diff --git a/t/t1512-rev-parse-disambiguation.sh b/t/t1512-rev-parse-disambiguation.sh index 80102cc43a3..98cefe3b703 100755 --- a/t/t1512-rev-parse-disambiguation.sh +++ b/t/t1512-rev-parse-disambiguation.sh @@ -73,7 +73,6 @@ test_expect_success 'ambiguous loose bad object parsed as OBJ_BAD' ' test_cmp_failed_rev_parse blob.bad bad0 <<-\EOF error: short object ID bad0... is ambiguous - hint: The candidates are: fatal: invalid object type EOF ' @@ -94,11 +93,11 @@ test_expect_success POSIXPERM 'ambigous zlib corrupt loose blob' ' test_cmp_failed_rev_parse blob.corrupt cafe <<-\EOF error: short object ID cafe... is ambiguous - hint: The candidates are: error: inflate: data stream error (incorrect header check) error: unable to unpack cafe... header error: inflate: data stream error (incorrect header check) error: unable to unpack cafe... header + hint: The candidates are: hint: cafe... [bad object] hint: cafe... blob fatal: ambiguous argument '\''cafe...'\'': unknown revision or path not in the working tree. From patchwork Thu Jan 27 05:26:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12726286 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C4E2AC433EF for ; Thu, 27 Jan 2022 05:27:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236151AbiA0F1O (ORCPT ); Thu, 27 Jan 2022 00:27:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236191AbiA0F1C (ORCPT ); Thu, 27 Jan 2022 00:27:02 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5785C061756 for ; Wed, 26 Jan 2022 21:27:01 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id h21so2586161wrb.8 for ; Wed, 26 Jan 2022 21:27:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dwmKH+/tC/y3j1tXfXqbMnfEEdAmWmYKuabbCHRQdms=; b=AFB5HUwbgJzS2w4owAhN+dW4JwEAEHSWuqR+quWCYDbt2aMuQW9DLV1K7P7mzEeaPa fgpMKAN3EriSjwOd9iSW2ldEiaZEgqzYNPEVz3pND8IBcRFtLSEy1tUNSbdhalwIp3ww 6R87lmgs8c4Kn/LWSV/kRPl/ON6ntF8ttVnJ6M3sE41mg50OFEynYOmq7Te/B945QjOi L0hsex1xnePB5i+9qQLqqvVuMbn1LQKTYyGwSblx6dbzmznb0nzJEDGVYCGSUP3iRODB Ay7ZTvc0qBsbjbt6TDUor6YFOYT4+AxfW+ZMRv0sGQx1hcuTv1m87CGMeczGMtkkMS6i csUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dwmKH+/tC/y3j1tXfXqbMnfEEdAmWmYKuabbCHRQdms=; b=dtU9We8580sXcufUMyt8rG+4IqI27O0W5MYT02BrMtp+ImICX5A6P+P8DROFC12nqc +PC1BG28HiBN8mJuwplkUyGTTDGBUGG/qvhMfeMMKhiBLFh+R+Ww2F53nra0yW7Wq4Ve VejtTd2dWlQvAEVM0ftzOSSlwhiPnWWEx89DGyEdUSp+vn2B1b2rYfzJ8/OUKCLKTqd+ MG5PS8QxmpDBuXyiwtd42d0eUAItxmKY1KJyFJsTnl9XmbWhCLi+Pfh6DaiMrhl29hRF fZIJZlfhl1H29LRxSJyUPO2AHdA0Zw6ww/CuXSGYtZuNs9oCzCVM15+SxjcFKfPq2T39 0LoA== X-Gm-Message-State: AOAM533CKEzSfps7wQvUasZGpY96SFP7S02h2qBWOViUlgg10naG/+Mi BsCWUAY1Lxz1atkeVUbQ1Ql7k1swoDxPAg== X-Google-Smtp-Source: ABdhPJyFOqHWoF70ibtTmtfNtIp4LZYr4njesJNTdfm/09Gq9h6PJo6BxaPOrBhlj4w5+11rp5h2Nw== X-Received: by 2002:a5d:5589:: with SMTP id i9mr1591081wrv.246.1643261219983; Wed, 26 Jan 2022 21:26:59 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z5sm6017661wmp.10.2022.01.26.21.26.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 21:26:59 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Bagas Sanjaya , Josh Steadmon , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v8 7/7] object-name: re-use "struct strbuf" in show_ambiguous_object() Date: Thu, 27 Jan 2022 06:26:49 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.890.gd7e422415d9 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Reduce the allocations done by show_ambiguous_object() by moving the "desc" strbuf into the "struct ambiguous_output" introduced in the preceding commit. This doesn't matter for optimization purposes, but since we're accumulating a "struct strbuf advice" anyway let's follow that pattern and add a "struct strbuf sb", we can then strbuf_reset() it rather than calling strbuf_release() for each call to show_ambiguous_object(). Signed-off-by: Ævar Arnfjörð Bjarmason --- object-name.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/object-name.c b/object-name.c index 6154e1ec6f8..61b58a2f292 100644 --- a/object-name.c +++ b/object-name.c @@ -354,6 +354,7 @@ static int init_object_disambiguation(struct repository *r, struct ambiguous_output { const struct disambiguate_state *ds; struct strbuf advice; + struct strbuf sb; }; static int show_ambiguous_object(const struct object_id *oid, void *data) @@ -361,7 +362,7 @@ static int show_ambiguous_object(const struct object_id *oid, void *data) struct ambiguous_output *state = data; const struct disambiguate_state *ds = state->ds; struct strbuf *advice = &state->advice; - struct strbuf desc = STRBUF_INIT; + struct strbuf *sb = &state->sb; int type; const char *hash; @@ -377,7 +378,7 @@ static int show_ambiguous_object(const struct object_id *oid, void *data) * output shown when we cannot look up or parse the * object in question. E.g. "deadbeef [bad object]". */ - strbuf_addf(&desc, _("%s [bad object]"), hash); + strbuf_addf(sb, _("%s [bad object]"), hash); goto out; } @@ -402,8 +403,8 @@ static int show_ambiguous_object(const struct object_id *oid, void *data) * * "deadbeef commit 2021-01-01 - Some Commit Message" */ - strbuf_addf(&desc, _("%s commit %s - %s"), - hash, date.buf, msg.buf); + strbuf_addf(sb, _("%s commit %s - %s"), hash, date.buf, + msg.buf); strbuf_release(&date); strbuf_release(&msg); @@ -423,7 +424,7 @@ static int show_ambiguous_object(const struct object_id *oid, void *data) * The third argument is the "tag" string * from object.c. */ - strbuf_addf(&desc, _("%s tag %s - %s"), hash, + strbuf_addf(sb, _("%s tag %s - %s"), hash, show_date(tag->date, 0, DATE_MODE(SHORT)), tag->tag); } else { @@ -434,7 +435,7 @@ static int show_ambiguous_object(const struct object_id *oid, void *data) * * "deadbeef [bad tag, could not parse it]" */ - strbuf_addf(&desc, _("%s [bad tag, could not parse it]"), + strbuf_addf(sb, _("%s [bad tag, could not parse it]"), hash); } } else if (type == OBJ_TREE) { @@ -442,13 +443,13 @@ static int show_ambiguous_object(const struct object_id *oid, void *data) * TRANSLATORS: This is a line of ambiguous * object output. E.g. "deadbeef tree". */ - strbuf_addf(&desc, _("%s tree"), hash); + strbuf_addf(sb, _("%s tree"), hash); } else if (type == OBJ_BLOB) { /* * TRANSLATORS: This is a line of ambiguous * object output. E.g. "deadbeef blob". */ - strbuf_addf(&desc, _("%s blob"), hash); + strbuf_addf(sb, _("%s blob"), hash); } @@ -459,9 +460,9 @@ static int show_ambiguous_object(const struct object_id *oid, void *data) * you'll probably want to swap the "%s" and leading " " space * around. */ - strbuf_addf(advice, _(" %s\n"), desc.buf); + strbuf_addf(advice, _(" %s\n"), sb->buf); - strbuf_release(&desc); + strbuf_reset(sb); return 0; } @@ -560,6 +561,7 @@ static enum get_oid_result get_short_oid(struct repository *r, struct oid_array collect = OID_ARRAY_INIT; struct ambiguous_output out = { .ds = &ds, + .sb = STRBUF_INIT, .advice = STRBUF_INIT, }; @@ -589,6 +591,7 @@ static enum get_oid_result get_short_oid(struct repository *r, oid_array_clear(&collect); strbuf_release(&out.advice); + strbuf_release(&out.sb); } return status;