From patchwork Fri Feb 4 13:51:15 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: 12735098 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 7AD6EC433F5 for ; Fri, 4 Feb 2022 13:51:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359063AbiBDNvl (ORCPT ); Fri, 4 Feb 2022 08:51:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359038AbiBDNvd (ORCPT ); Fri, 4 Feb 2022 08:51:33 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 70D0EC061714 for ; Fri, 4 Feb 2022 05:51:32 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id s10so8888515wra.5 for ; Fri, 04 Feb 2022 05:51:32 -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=7tSCnKlilEMtmbvfnJOSly7qrkNkrxedFOl38ZcdJlw=; b=mGsO6CFD2NOrYPTXFu7n1NJ+lNwbpbjzl5pnIh+yNitGpafSAbpU1hUJSXo+C/6MoM CtEAWsCe+GkE8IpyrmpQtw2/pvEUmpBAh1Kr0XfWOFZ98xGvEqhgMUsa/axgROsEtlWE CrsDB3bmmS37z9fESNKuOp9bGV15MSl+6IyuyunOvGYQ77IWFpID0E0V2gs4zN5QIzJI Kho6RJxI8N2N9sf4u0Ny/hUBet9Qj1LbM06trnhL5wM+/3kWBlhWb37LICt7M9FUESj5 CDZBxjT3W2UeihF4ArrOnsKMYURyE5/4Jn/JEEj1QSD4Yqh8w/beutEYKM+IpjMYAoDC ZRww== 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=7tSCnKlilEMtmbvfnJOSly7qrkNkrxedFOl38ZcdJlw=; b=3lSo8CauTQtC9DSdLDyeaykbUWbUjZsFDFvfbj7eOa6kw66EDGq9vOzNHA0pc0BGOR UXKJnghaC/MrXLnWeFTQRtudUa1MNRZPZYWjdP+x+2PwQhA/QUcIgTvJAD0UAiVOwRmK 1Fv7W7YuVC4ZXh7XTfV6AuA5Srf92q5tXLWC0Wfl6IYd8+P5Mxl+iKlFjxH+tAbKDg5H HuZZ7wr9usJEviAHaD/L/A90rCcisUXXAOvd1/oOjjMjmfK/pFE8BlTb6W2vHCg4Zfl5 zspu44WDLbqPohe7UTUdAy/EWZ0fgmqaeYaC7GwcotN5K8pZP/GodLMU88L2mMhVSrzQ TSmQ== X-Gm-Message-State: AOAM530LmTtgbmwyN014SUKcxmH+vc3J9CrMPM1BE/aOrP3HkPqjdCsX BS1PTx3Mcw+3RaHXuEODDHZ1c3kRMxuoyQ== X-Google-Smtp-Source: ABdhPJzxqi+8KqIv1/nQ1lcC7AlZkrFjSwTihComKBkNcwkDWdLZFWJrmlUBBa5kPA07eLu3o3YOig== X-Received: by 2002:adf:dec3:: with SMTP id i3mr2453035wrn.691.1643982690623; Fri, 04 Feb 2022 05:51:30 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z2sm205963wmi.22.2022.02.04.05.51.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 05:51:30 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH v2 01/11] object-file.c: split up declaration of unrelated variables Date: Fri, 4 Feb 2022 14:51:15 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Split up the declaration of the "ret" and "re_allocated" variables. It's not our usual style to group variable declarations simply because they share a type, we'd only prefer to do so when the two are closely related (e.g. "int i, j"). This change makes a subsequent and meaningful change's diff smaller. Signed-off-by: Ævar Arnfjörð Bjarmason --- object-file.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/object-file.c b/object-file.c index 8be57f48de7..ecc77973292 100644 --- a/object-file.c +++ b/object-file.c @@ -2118,7 +2118,8 @@ static int index_mem(struct index_state *istate, enum object_type type, const char *path, unsigned flags) { - int ret, re_allocated = 0; + int ret; + int re_allocated = 0; int write_object = flags & HASH_WRITE_OBJECT; if (!type) From patchwork Fri Feb 4 13:51:16 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: 12735095 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 25AF1C4332F for ; Fri, 4 Feb 2022 13:51:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359082AbiBDNvh (ORCPT ); Fri, 4 Feb 2022 08:51:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359092AbiBDNvd (ORCPT ); Fri, 4 Feb 2022 08:51:33 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6610BC061744 for ; Fri, 4 Feb 2022 05:51:33 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id w11so11480811wra.4 for ; Fri, 04 Feb 2022 05:51:33 -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=dK5jNmVJ4rq+0btDdbLjErBgSAghCq4PC5Gnh2GK+tM=; b=JhXajN3FnheOljk8CnhsfeMScSOIqM63dib0Nw5MvtB5gGgtd15YFQ3jNQXmnr88fS wYafQ/620Xu1Hw0b4l1/443H3FXdhLXFEQR8/TM476YnM/4J3sH8Ut9eXn7AjtebaJJU 8v42+AFYum24cUFUGLe8GopUtmwLQr0/eBI6aBXyNr/kr1KsgtW8jlqxBfNagwK/0Vvz dz2W873G0VcWd1LqdjMqI0QdqBQ6HKxobWTf80IXKR2WH5dBOwZbs+mcvnj4klSL/YnM p6IwvMeFr920uAWHTsnjOhXRLtt2qLB0Z5OOu5uEHL6Pbwj4KvnHTu6srgkHTqxV5Z/2 7a/w== 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=dK5jNmVJ4rq+0btDdbLjErBgSAghCq4PC5Gnh2GK+tM=; b=A3ngM4TDBNhew8DWJr7QTS0pDqx5nFtFS2XCNfFD3h++EY8D2mDt1RfG3IR5N9dpaX L2JxLhnU7lcydRMyLy/BmkJB7EA3nkHt7ze31TffVUduN+Yd0Ivxu68DHX5cQimPYAT/ MJSSOHbsxWgPI6NGXyVP/DudPVjr3wb8lgqzCsEisS/yghAHT4u8AXUsYYf0HoqX0Te/ wdfH/+kB+IganCWYP1pULcOTAJxBWu0H6nXVIfsPhJiiifDu4EREZvjN/2VoLs2y2c2L 5FC/CkRQnhfeuDOB0jnIR70QpprzhNrDivuhMpL36wBc0Hhcm+rhC4qqk6O0cZ/1jl7f DH5A== X-Gm-Message-State: AOAM5316qtmqkeYqy0GEsFokULE6JbKifaXYNxgi5YWDRj6Qj4GcwBBO 7DJ7fb+TqzIR4VN3TkzL0g3yHE/kKGrYXg== X-Google-Smtp-Source: ABdhPJwUZKPeGuzYIZfNcQMM1Ol17w48qB+wzLgL+RrvDzWUYOAM1BuMMO/M58BBPAOFu6eWLRpN7w== X-Received: by 2002:adf:f905:: with SMTP id b5mr2571617wrr.173.1643982691754; Fri, 04 Feb 2022 05:51:31 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z2sm205963wmi.22.2022.02.04.05.51.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 05:51:30 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH v2 02/11] object-file API: return "void", not "int" from hash_object_file() Date: Fri, 4 Feb 2022 14:51:16 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The hash_object_file() function added in abdc3fc8421 (Add hash_sha1_file(), 2006-10-14) did not have a meaningful return value, and it never has. One was seemingly added to avoid adding braces to the "ret = " assignments being modified here. Let's instead assign "0" to the "ret" variables at the beginning of the relevant functions, and have them return "void". Signed-off-by: Ævar Arnfjörð Bjarmason --- object-file.c | 16 ++++++++-------- object-store.h | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/object-file.c b/object-file.c index ecc77973292..eeb6814780a 100644 --- a/object-file.c +++ b/object-file.c @@ -1836,14 +1836,13 @@ static int write_buffer(int fd, const void *buf, size_t len) return 0; } -int hash_object_file(const struct git_hash_algo *algo, const void *buf, +void hash_object_file(const struct git_hash_algo *algo, const void *buf, unsigned long len, const char *type, struct object_id *oid) { char hdr[MAX_HEADER_LEN]; int hdrlen = sizeof(hdr); write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen); - return 0; } /* Finalize a file on disk, and close it. */ @@ -2118,7 +2117,7 @@ static int index_mem(struct index_state *istate, enum object_type type, const char *path, unsigned flags) { - int ret; + int ret = 0; int re_allocated = 0; int write_object = flags & HASH_WRITE_OBJECT; @@ -2148,8 +2147,9 @@ static int index_mem(struct index_state *istate, if (write_object) ret = write_object_file(buf, size, type_name(type), oid); else - ret = hash_object_file(the_hash_algo, buf, size, - type_name(type), oid); + hash_object_file(the_hash_algo, buf, size, type_name(type), + oid); + if (re_allocated) free(buf); return ret; @@ -2161,7 +2161,7 @@ static int index_stream_convert_blob(struct index_state *istate, const char *path, unsigned flags) { - int ret; + int ret = 0; const int write_object = flags & HASH_WRITE_OBJECT; struct strbuf sbuf = STRBUF_INIT; @@ -2175,8 +2175,8 @@ static int index_stream_convert_blob(struct index_state *istate, ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB), oid); else - ret = hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, - type_name(OBJ_BLOB), oid); + hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, + type_name(OBJ_BLOB), oid); strbuf_release(&sbuf); return ret; } diff --git a/object-store.h b/object-store.h index 6f89482df03..44f6868cc9c 100644 --- a/object-store.h +++ b/object-store.h @@ -245,9 +245,9 @@ static inline void *repo_read_object_file(struct repository *r, /* Read and unpack an object file into memory, write memory to an object file */ int oid_object_info(struct repository *r, const struct object_id *, unsigned long *); -int hash_object_file(const struct git_hash_algo *algo, const void *buf, - unsigned long len, const char *type, - struct object_id *oid); +void hash_object_file(const struct git_hash_algo *algo, const void *buf, + unsigned long len, const char *type, + struct object_id *oid); int write_object_file_flags(const void *buf, unsigned long len, const char *type, struct object_id *oid, From patchwork Fri Feb 4 13:51:17 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: 12735096 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 5D7FBC433F5 for ; Fri, 4 Feb 2022 13:51:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359051AbiBDNvi (ORCPT ); Fri, 4 Feb 2022 08:51:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359057AbiBDNve (ORCPT ); Fri, 4 Feb 2022 08:51:34 -0500 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D51AC06173D for ; Fri, 4 Feb 2022 05:51:34 -0800 (PST) Received: by mail-wm1-x32c.google.com with SMTP id s22-20020a1ca916000000b0037b91599cd0so981229wme.5 for ; Fri, 04 Feb 2022 05:51:34 -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=xGNwgYaF7rBLtewDvY2Hit0j+5W6weuy8LmWr1ewfIo=; b=MdF52ctZ0ViqA33cJdYl8rMsokhQvpZtt4zW6LTmECr/iXm3oDuz82tnmmO+BZFvQi AA0WecSz98OnHydAAng785GM3w7F4r/MFEEwGL3SnfJVjx0TCHLGEu1iCiXi0aK6P3H3 hHkbttBT362eAXiixBDs/SsvUrOMhb0pbhjc4ffr0oNVwK4tg438ja+b3j5O7rclBqL6 bwtBkKMO12I/68rOHFfrtTVnZtx8zQHwkzrWYWNn7zqLH8Q/BU9VkXJV/SUs6mSQHQ9/ x6K3k9HXYXQYaT36GiATiZXEbfgpbMf3pMfcWBIYuRFetSPJT2G4AKSSA79ing76tLOj qIjg== 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=xGNwgYaF7rBLtewDvY2Hit0j+5W6weuy8LmWr1ewfIo=; b=nY27iJ5mARbYQkfBJbt3zo8nP5rE7Q27q6fVMfz9HmyS309PImhoxEY0o7j0Hl17WO M+SChQKjOZZjs/jUedFq8dABVYKKwwMv5ANk1SAHx3K9s2Gw/ShVvrWl8FyqpHahCOoz b+9Wrz3mvbiccbh+n+IUrTPi2vjt8EG4fJj4gR9AJ0jQ3WLIMHnivNbs4N8GnjnR05Ez REkrLp2rNEFDzNHQRUbpN5Q/XHiXwi0pXb7067aHiYoNZp2gZ3CrcQldKJ96kBGpEJyr L+Zk8wOLav9suPP8CJETITRtcD01ias1vPs8yxzylt+4GlQP709acUaLwlbWaBKtTeF/ zLaw== X-Gm-Message-State: AOAM531Z8LdAC9a2bMz24X3A0VNUbULSvVhPmwALtwbU10uQYARlQ1oI zSv6RsbGHwYozDfr2IEMigZfpq4zNT2Y8Q== X-Google-Smtp-Source: ABdhPJzP5nSqG3srGeCyQIC3iDhftN0mlpSlhFfheNlOV/EvZSrymG+Yvzt/8Ma+PCiLjsj8x4dDyQ== X-Received: by 2002:a05:600c:3049:: with SMTP id n9mr2316734wmh.121.1643982692686; Fri, 04 Feb 2022 05:51:32 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z2sm205963wmi.22.2022.02.04.05.51.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 05:51:32 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH v2 03/11] object-file API: add a format_object_header() function Date: Fri, 4 Feb 2022 14:51:17 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a convenience function to wrap the xsnprintf() command that generates loose object headers. This code was copy/pasted in various parts of the codebase, let's define it in one place and re-use it from there. All except one caller of it had a valid "enum object_type" for us, it's only write_object_file_prepare() which might need to deal with "git hash-object --literally" and a potential garbage type. Let's have the primary API use an "enum object_type", and define a *_literally() function that can take an arbitrary "const char *" for the type. See [1] for the discussion that prompted this patch, i.e. new code in object-file.c that wanted to copy/paste the xsnprintf() invocation. In the case of fast-import.c the callers unfortunately need to cast back & forth between "unsigned char *" and "char *", since format_object_header() ad encode_in_pack_object_header() take different signedness. 1. https://lore.kernel.org/git/211213.86bl1l9bfz.gmgdl@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/fast-import.c | 6 +++--- builtin/index-pack.c | 3 +-- bulk-checkin.c | 4 ++-- http-push.c | 2 +- object-file.c | 23 ++++++++++++++++++++--- object-store.h | 8 ++++++++ 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/builtin/fast-import.c b/builtin/fast-import.c index 2b2e28bad79..123df7d9a53 100644 --- a/builtin/fast-import.c +++ b/builtin/fast-import.c @@ -937,8 +937,8 @@ static int store_object( git_hash_ctx c; git_zstream s; - hdrlen = xsnprintf((char *)hdr, sizeof(hdr), "%s %lu", - type_name(type), (unsigned long)dat->len) + 1; + hdrlen = format_object_header((char *)hdr, sizeof(hdr), type, + dat->len); the_hash_algo->init_fn(&c); the_hash_algo->update_fn(&c, hdr, hdrlen); the_hash_algo->update_fn(&c, dat->buf, dat->len); @@ -1091,7 +1091,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark) hashfile_checkpoint(pack_file, &checkpoint); offset = checkpoint.offset; - hdrlen = xsnprintf((char *)out_buf, out_sz, "blob %" PRIuMAX, len) + 1; + hdrlen = format_object_header((char *)out_buf, out_sz, OBJ_BLOB, len); the_hash_algo->init_fn(&c); the_hash_algo->update_fn(&c, out_buf, hdrlen); diff --git a/builtin/index-pack.c b/builtin/index-pack.c index 3c2e6aee3cc..01574378ce2 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -449,8 +449,7 @@ static void *unpack_entry_data(off_t offset, unsigned long size, int hdrlen; if (!is_delta_type(type)) { - hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX, - type_name(type),(uintmax_t)size) + 1; + hdrlen = format_object_header(hdr, sizeof(hdr), type, size); the_hash_algo->init_fn(&c); the_hash_algo->update_fn(&c, hdr, hdrlen); } else diff --git a/bulk-checkin.c b/bulk-checkin.c index 8785b2ac806..85b3ebaf971 100644 --- a/bulk-checkin.c +++ b/bulk-checkin.c @@ -220,8 +220,8 @@ static int deflate_to_pack(struct bulk_checkin_state *state, if (seekback == (off_t) -1) return error("cannot find the current offset"); - header_len = xsnprintf((char *)obuf, sizeof(obuf), "%s %" PRIuMAX, - type_name(type), (uintmax_t)size) + 1; + header_len = format_object_header((char *)obuf, sizeof(obuf), + type, size); the_hash_algo->init_fn(&ctx); the_hash_algo->update_fn(&ctx, obuf, header_len); diff --git a/http-push.c b/http-push.c index 3309aaf004a..f0c044dcf76 100644 --- a/http-push.c +++ b/http-push.c @@ -363,7 +363,7 @@ static void start_put(struct transfer_request *request) git_zstream stream; unpacked = read_object_file(&request->obj->oid, &type, &len); - hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(type), (uintmax_t)len) + 1; + hdrlen = format_object_header(hdr, sizeof(hdr), type, len); /* Set it up */ git_deflate_init(&stream, zlib_compression_level); diff --git a/object-file.c b/object-file.c index eeb6814780a..3fcd46cf9ed 100644 --- a/object-file.c +++ b/object-file.c @@ -1049,6 +1049,23 @@ void *xmmap(void *start, size_t length, return ret; } +static int format_object_header_literally(char *str, size_t size, + const char *type, size_t objsize) +{ + return xsnprintf(str, size, "%s %"PRIuMAX, type, (uintmax_t)objsize) + 1; +} + +int format_object_header(char *str, size_t size, enum object_type type, + size_t objsize) +{ + const char *name = type_name(type); + + if (!name) + BUG("could not get a type name for 'enum object_type' value %d", type); + + return format_object_header_literally(str, size, name, objsize); +} + /* * With an in-core object data in "map", rehash it to make sure the * object name actually matches "oid" to detect object corruption. @@ -1077,7 +1094,7 @@ int check_object_signature(struct repository *r, const struct object_id *oid, return -1; /* Generate the header */ - hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(obj_type), (uintmax_t)size) + 1; + hdrlen = format_object_header(hdr, sizeof(hdr), obj_type, size); /* Sha1.. */ r->hash_algo->init_fn(&c); @@ -1777,7 +1794,7 @@ static void write_object_file_prepare(const struct git_hash_algo *algo, git_hash_ctx c; /* Generate the header */ - *hdrlen = xsnprintf(hdr, *hdrlen, "%s %"PRIuMAX , type, (uintmax_t)len)+1; + *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len); /* Sha1.. */ algo->init_fn(&c); @@ -2051,7 +2068,7 @@ int force_object_loose(const struct object_id *oid, time_t mtime) buf = read_object(the_repository, oid, &type, &len); if (!buf) return error(_("cannot read object for %s"), oid_to_hex(oid)); - hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(type), (uintmax_t)len) + 1; + hdrlen = format_object_header(hdr, sizeof(hdr), type, len); ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime, 0); free(buf); diff --git a/object-store.h b/object-store.h index 44f6868cc9c..8b4413d0ce7 100644 --- a/object-store.h +++ b/object-store.h @@ -331,6 +331,14 @@ int repo_has_object_file_with_flags(struct repository *r, */ int has_loose_object_nonlocal(const struct object_id *); +/** + * format_object_header() is a thin wrapper around s xsnprintf() that + * writes the initial " " part of the loose object + * header. It returns the size that snprintf() returns + 1. + */ +int format_object_header(char *str, size_t size, enum object_type type, + size_t objsize); + void assert_oid_type(const struct object_id *oid, enum object_type expect); /* From patchwork Fri Feb 4 13:51:18 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: 12735097 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 18EC6C433FE for ; Fri, 4 Feb 2022 13:51:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359075AbiBDNvk (ORCPT ); Fri, 4 Feb 2022 08:51:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359063AbiBDNvf (ORCPT ); Fri, 4 Feb 2022 08:51:35 -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 811E1C061714 for ; Fri, 4 Feb 2022 05:51:35 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id r17so2404563wrr.6 for ; Fri, 04 Feb 2022 05:51:35 -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=LRG38IldwmAs0wehCad3CZUrGjB7jmuoM2oFo8bz22o=; b=Oll2hwijXlRcmwDTBazSF3CobeAYWXw+5vfDyj61+fZlVAlHL5wQCgN6sps4VGxMji pVYJEFjBF4Uk4zublnn3Egcmvuwk8Cy9PxmodNgLPXiP6UdY6nVJFhI4tYFLoStDUgMx v5OwUFcEmGTFUKdoVB4GW+T0f08LMf33Gr9w7VDOa7NhkuZ4UGbevfZKOS8baYqSkL3y 9dXqP5k03cXfmEA1JKjF/uCCmVLoe0EewBlsbd9XfXW1auAzaNxXze7KDm+Ez7CwA4zz ESVWvW8vna7yl9Tl4t78Htz4wMI9mVdpRa/HNLOmVqRVHsvKDQ6wVfxiNzn+1Smc26iv Nzjw== 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=LRG38IldwmAs0wehCad3CZUrGjB7jmuoM2oFo8bz22o=; b=NTUtlQ4PVFQOR8QIpc8Dgtg/KWqDMZKLhe1oS3MmtOudntVj+VBMtFf8SARCm0CQVD F/MdVJ6GubGMPBqHwW0S9GW6tDzW41etKvXQSU8cDQYEX23quNRWIaTQTgdv/x+oYDRr sj5gx6t1Hxoi3hRZNojEDo40QwjUqBzTB8kpUUKaZjA3X+tg8FR7neWgPIi3fMwNbpot Qbz9wzOAzZdNjJvmMhFerOb+kYGWYqV0j7cCcWMXFOMEva65RoeiOLx/eTLhRB+BBezW AXYNu4wc5Vq83pHEJFxq4aP4Lqq5/nQ2AL148fuASStLSj6wZmLNwAwT7NsTiB7Thscz EDnQ== X-Gm-Message-State: AOAM532BYJTKrMG7lu5bqxiIeivy/XuLKdkcGMDdqFvxI05I6XoVhp4x +N7I9AMcFBFhvXiPb4cdlfl6RLo1Lda+9A== X-Google-Smtp-Source: ABdhPJwilsRJUtF/3+Lz8A2C5ILojX2D2mheIyKYkqtTBSrz/q2/rg7pixe+sXzHoblhksQviCxBRw== X-Received: by 2002:adf:f008:: with SMTP id j8mr2621332wro.131.1643982693681; Fri, 04 Feb 2022 05:51:33 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z2sm205963wmi.22.2022.02.04.05.51.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 05:51:32 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH v2 04/11] object-file API: have write_object_file() take "enum object_type" Date: Fri, 4 Feb 2022 14:51:18 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the write_object_file() function to take an "enum object_type" instead of a "const char *type". Its callers either passed {commit,tree,blob,tag}_type and can pass the corresponding OBJ_* type instead, or were hardcoding strings like "blob". This avoids the back & forth fragility where the callers of write_object_file() would have the enum type, and convert it themselves via type_name(). We do have to now do that conversion ourselves before calling write_object_file_prepare(), but those codepaths will be similarly adjusted in subsequent commits. Signed-off-by: Ævar Arnfjörð Bjarmason --- apply.c | 8 ++++---- builtin/checkout.c | 2 +- builtin/mktag.c | 2 +- builtin/mktree.c | 2 +- builtin/notes.c | 3 +-- builtin/receive-pack.c | 2 +- builtin/replace.c | 2 +- builtin/tag.c | 2 +- builtin/unpack-objects.c | 6 +++--- cache-tree.c | 2 +- commit.c | 2 +- match-trees.c | 2 +- merge-ort.c | 4 ++-- merge-recursive.c | 2 +- notes-cache.c | 2 +- notes.c | 8 ++++---- object-file.c | 10 +++++----- object-store.h | 4 ++-- read-cache.c | 2 +- 19 files changed, 33 insertions(+), 34 deletions(-) diff --git a/apply.c b/apply.c index 7ffadc3b17a..e82a7c30f01 100644 --- a/apply.c +++ b/apply.c @@ -3589,7 +3589,7 @@ static int try_threeway(struct apply_state *state, /* Preimage the patch was prepared for */ if (patch->is_new) - write_object_file("", 0, blob_type, &pre_oid); + write_object_file("", 0, OBJ_BLOB, &pre_oid); else if (get_oid(patch->old_oid_prefix, &pre_oid) || read_blob_object(&buf, &pre_oid, patch->old_mode)) return error(_("repository lacks the necessary blob to perform 3-way merge.")); @@ -3605,7 +3605,7 @@ static int try_threeway(struct apply_state *state, return -1; } /* post_oid is theirs */ - write_object_file(tmp_image.buf, tmp_image.len, blob_type, &post_oid); + write_object_file(tmp_image.buf, tmp_image.len, OBJ_BLOB, &post_oid); clear_image(&tmp_image); /* our_oid is ours */ @@ -3618,7 +3618,7 @@ static int try_threeway(struct apply_state *state, return error(_("cannot read the current contents of '%s'"), patch->old_name); } - write_object_file(tmp_image.buf, tmp_image.len, blob_type, &our_oid); + write_object_file(tmp_image.buf, tmp_image.len, OBJ_BLOB, &our_oid); clear_image(&tmp_image); /* in-core three-way merge between post and our using pre as base */ @@ -4346,7 +4346,7 @@ static int add_index_file(struct apply_state *state, } fill_stat_cache_info(state->repo->index, ce, &st); } - if (write_object_file(buf, size, blob_type, &ce->oid) < 0) { + if (write_object_file(buf, size, OBJ_BLOB, &ce->oid) < 0) { discard_cache_entry(ce); return error(_("unable to create backing store " "for newly created file %s"), path); diff --git a/builtin/checkout.c b/builtin/checkout.c index cc804ba8e1e..24cc235939f 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -298,7 +298,7 @@ static int checkout_merged(int pos, const struct checkout *state, * (it also writes the merge result to the object database even * when it may contain conflicts). */ - if (write_object_file(result_buf.ptr, result_buf.size, blob_type, &oid)) + if (write_object_file(result_buf.ptr, result_buf.size, OBJ_BLOB, &oid)) die(_("Unable to add merge result for '%s'"), path); free(result_buf.ptr); ce = make_transient_cache_entry(mode, &oid, path, 2, ce_mem_pool); diff --git a/builtin/mktag.c b/builtin/mktag.c index 3b2dbbb37e6..96a3686af53 100644 --- a/builtin/mktag.c +++ b/builtin/mktag.c @@ -100,7 +100,7 @@ int cmd_mktag(int argc, const char **argv, const char *prefix) if (verify_object_in_tag(&tagged_oid, &tagged_type)) die(_("tag on stdin did not refer to a valid object")); - if (write_object_file(buf.buf, buf.len, tag_type, &result) < 0) + if (write_object_file(buf.buf, buf.len, OBJ_TAG, &result) < 0) die(_("unable to write tag file")); strbuf_release(&buf); diff --git a/builtin/mktree.c b/builtin/mktree.c index ae78ca1c029..a1213aaa961 100644 --- a/builtin/mktree.c +++ b/builtin/mktree.c @@ -58,7 +58,7 @@ static void write_tree(struct object_id *oid) strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz); } - write_object_file(buf.buf, buf.len, tree_type, oid); + write_object_file(buf.buf, buf.len, OBJ_TREE, oid); strbuf_release(&buf); } diff --git a/builtin/notes.c b/builtin/notes.c index 05d60483e82..d81acaeaf78 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -199,9 +199,8 @@ static void prepare_note_data(const struct object_id *object, struct note_data * static void write_note_data(struct note_data *d, struct object_id *oid) { - if (write_object_file(d->buf.buf, d->buf.len, blob_type, oid)) { + if (write_object_file(d->buf.buf, d->buf.len, OBJ_BLOB, oid)) { int status = die_message(_("unable to write note object")); - if (d->edit_path) die_message(_("the note contents have been left in %s"), d->edit_path); diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 9f4a0b816cf..231d576927b 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -762,7 +762,7 @@ static void prepare_push_cert_sha1(struct child_process *proc) int bogs /* beginning_of_gpg_sig */; already_done = 1; - if (write_object_file(push_cert.buf, push_cert.len, "blob", + if (write_object_file(push_cert.buf, push_cert.len, OBJ_BLOB, &push_cert_oid)) oidclr(&push_cert_oid); diff --git a/builtin/replace.c b/builtin/replace.c index 6ff1734d587..995ca683302 100644 --- a/builtin/replace.c +++ b/builtin/replace.c @@ -474,7 +474,7 @@ static int create_graft(int argc, const char **argv, int force, int gentle) return -1; } - if (write_object_file(buf.buf, buf.len, commit_type, &new_oid)) { + if (write_object_file(buf.buf, buf.len, OBJ_COMMIT, &new_oid)) { strbuf_release(&buf); return error(_("could not write replacement commit for: '%s'"), old_ref); diff --git a/builtin/tag.c b/builtin/tag.c index 134b3f1edf0..31e6b155c04 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -238,7 +238,7 @@ static int build_tag_object(struct strbuf *buf, int sign, struct object_id *resu { if (sign && do_sign(buf) < 0) return error(_("unable to sign the tag")); - if (write_object_file(buf->buf, buf->len, tag_type, result) < 0) + if (write_object_file(buf->buf, buf->len, OBJ_TAG, result) < 0) return error(_("unable to write tag file")); return 0; } diff --git a/builtin/unpack-objects.c b/builtin/unpack-objects.c index 4a9466295ba..808041551fd 100644 --- a/builtin/unpack-objects.c +++ b/builtin/unpack-objects.c @@ -177,7 +177,7 @@ static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf) struct object_id oid; if (write_object_file(obj_buf->buffer, obj_buf->size, - type_name(obj->type), &oid) < 0) + obj->type, &oid) < 0) die("failed to write object %s", oid_to_hex(&obj->oid)); obj->flags |= FLAG_WRITTEN; } @@ -243,7 +243,7 @@ static void write_object(unsigned nr, enum object_type type, void *buf, unsigned long size) { if (!strict) { - if (write_object_file(buf, size, type_name(type), + if (write_object_file(buf, size, type, &obj_list[nr].oid) < 0) die("failed to write object"); added_object(nr, type, buf, size); @@ -251,7 +251,7 @@ static void write_object(unsigned nr, enum object_type type, obj_list[nr].obj = NULL; } else if (type == OBJ_BLOB) { struct blob *blob; - if (write_object_file(buf, size, type_name(type), + if (write_object_file(buf, size, type, &obj_list[nr].oid) < 0) die("failed to write object"); added_object(nr, type, buf, size); diff --git a/cache-tree.c b/cache-tree.c index 65ca9933613..fdbbd191d22 100644 --- a/cache-tree.c +++ b/cache-tree.c @@ -440,7 +440,7 @@ static int update_one(struct cache_tree *it, } else if (dryrun) { hash_object_file(the_hash_algo, buffer.buf, buffer.len, tree_type, &it->oid); - } else if (write_object_file_flags(buffer.buf, buffer.len, tree_type, + } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE, &it->oid, flags & WRITE_TREE_SILENT ? HASH_SILENT : 0)) { strbuf_release(&buffer); diff --git a/commit.c b/commit.c index a348f085b2b..6cc0ba9c11b 100644 --- a/commit.c +++ b/commit.c @@ -1567,7 +1567,7 @@ int commit_tree_extended(const char *msg, size_t msg_len, goto out; } - result = write_object_file(buffer.buf, buffer.len, commit_type, ret); + result = write_object_file(buffer.buf, buffer.len, OBJ_COMMIT, ret); out: strbuf_release(&buffer); return result; diff --git a/match-trees.c b/match-trees.c index df413989fa8..49398e599fe 100644 --- a/match-trees.c +++ b/match-trees.c @@ -235,7 +235,7 @@ static int splice_tree(const struct object_id *oid1, const char *prefix, rewrite_with = oid2; } hashcpy(rewrite_here, rewrite_with->hash); - status = write_object_file(buf, sz, tree_type, result); + status = write_object_file(buf, sz, OBJ_TREE, result); free(buf); return status; } diff --git a/merge-ort.c b/merge-ort.c index c3197970219..d4ba2955f11 100644 --- a/merge-ort.c +++ b/merge-ort.c @@ -1888,7 +1888,7 @@ static int handle_content_merge(struct merge_options *opt, if (!ret && write_object_file(result_buf.ptr, result_buf.size, - blob_type, &result->oid)) + OBJ_BLOB, &result->oid)) ret = err(opt, _("Unable to add %s to database"), path); @@ -3343,7 +3343,7 @@ static void write_tree(struct object_id *result_oid, } /* Write this object file out, and record in result_oid */ - write_object_file(buf.buf, buf.len, tree_type, result_oid); + write_object_file(buf.buf, buf.len, OBJ_TREE, result_oid); strbuf_release(&buf); } diff --git a/merge-recursive.c b/merge-recursive.c index d9457797dbb..e0cbbf2d022 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1373,7 +1373,7 @@ static int merge_mode_and_contents(struct merge_options *opt, if (!ret && write_object_file(result_buf.ptr, result_buf.size, - blob_type, &result->blob.oid)) + OBJ_BLOB, &result->blob.oid)) ret = err(opt, _("Unable to add %s to database"), a->path); diff --git a/notes-cache.c b/notes-cache.c index 2473314d686..9dfd251a815 100644 --- a/notes-cache.c +++ b/notes-cache.c @@ -92,7 +92,7 @@ int notes_cache_put(struct notes_cache *c, struct object_id *key_oid, { struct object_id value_oid; - if (write_object_file(data, size, "blob", &value_oid) < 0) + if (write_object_file(data, size, OBJ_BLOB, &value_oid) < 0) return -1; return add_note(&c->tree, key_oid, &value_oid, NULL); } diff --git a/notes.c b/notes.c index f87dac40684..7452e71cc8d 100644 --- a/notes.c +++ b/notes.c @@ -675,7 +675,7 @@ static int tree_write_stack_finish_subtree(struct tree_write_stack *tws) ret = tree_write_stack_finish_subtree(n); if (ret) return ret; - ret = write_object_file(n->buf.buf, n->buf.len, tree_type, &s); + ret = write_object_file(n->buf.buf, n->buf.len, OBJ_TREE, &s); if (ret) return ret; strbuf_release(&n->buf); @@ -836,7 +836,7 @@ int combine_notes_concatenate(struct object_id *cur_oid, free(new_msg); /* create a new blob object from buf */ - ret = write_object_file(buf, buf_len, blob_type, cur_oid); + ret = write_object_file(buf, buf_len, OBJ_BLOB, cur_oid); free(buf); return ret; } @@ -916,7 +916,7 @@ int combine_notes_cat_sort_uniq(struct object_id *cur_oid, string_list_join_lines_helper, &buf)) goto out; - ret = write_object_file(buf.buf, buf.len, blob_type, cur_oid); + ret = write_object_file(buf.buf, buf.len, OBJ_BLOB, cur_oid); out: strbuf_release(&buf); @@ -1192,7 +1192,7 @@ int write_notes_tree(struct notes_tree *t, struct object_id *result) ret = for_each_note(t, flags, write_each_note, &cb_data) || write_each_non_note_until(NULL, &cb_data) || tree_write_stack_finish_subtree(&root) || - write_object_file(root.buf.buf, root.buf.len, tree_type, result); + write_object_file(root.buf.buf, root.buf.len, OBJ_TREE, result); strbuf_release(&root.buf); return ret; } diff --git a/object-file.c b/object-file.c index 3fcd46cf9ed..59eb793e0ac 100644 --- a/object-file.c +++ b/object-file.c @@ -2014,7 +2014,7 @@ static int freshen_packed_object(const struct object_id *oid) } int write_object_file_flags(const void *buf, unsigned long len, - const char *type, struct object_id *oid, + enum object_type type, struct object_id *oid, unsigned flags) { char hdr[MAX_HEADER_LEN]; @@ -2023,7 +2023,7 @@ int write_object_file_flags(const void *buf, unsigned long len, /* Normally if we have it in the pack then we do not bother writing * it out into .git/objects/??/?{38} file. */ - write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr, + write_object_file_prepare(the_hash_algo, buf, len, type_name(type), oid, hdr, &hdrlen); if (freshen_packed_object(oid) || freshen_loose_object(oid)) return 0; @@ -2162,7 +2162,7 @@ static int index_mem(struct index_state *istate, } if (write_object) - ret = write_object_file(buf, size, type_name(type), oid); + ret = write_object_file(buf, size, type, oid); else hash_object_file(the_hash_algo, buf, size, type_name(type), oid); @@ -2189,7 +2189,7 @@ static int index_stream_convert_blob(struct index_state *istate, get_conv_flags(flags)); if (write_object) - ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB), + ret = write_object_file(sbuf.buf, sbuf.len, OBJ_BLOB, oid); else hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, @@ -2313,7 +2313,7 @@ int index_path(struct index_state *istate, struct object_id *oid, if (!(flags & HASH_WRITE_OBJECT)) hash_object_file(the_hash_algo, sb.buf, sb.len, blob_type, oid); - else if (write_object_file(sb.buf, sb.len, blob_type, oid)) + else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid)) rc = error(_("%s: failed to insert into database"), path); strbuf_release(&sb); break; diff --git a/object-store.h b/object-store.h index 8b4413d0ce7..eab1e2a967e 100644 --- a/object-store.h +++ b/object-store.h @@ -250,10 +250,10 @@ void hash_object_file(const struct git_hash_algo *algo, const void *buf, struct object_id *oid); int write_object_file_flags(const void *buf, unsigned long len, - const char *type, struct object_id *oid, + enum object_type type, struct object_id *oid, unsigned flags); static inline int write_object_file(const void *buf, unsigned long len, - const char *type, struct object_id *oid) + enum object_type type, struct object_id *oid) { return write_object_file_flags(buf, len, type, oid, 0); } diff --git a/read-cache.c b/read-cache.c index cbe73f14e5e..b8ae30a0e71 100644 --- a/read-cache.c +++ b/read-cache.c @@ -735,7 +735,7 @@ static struct cache_entry *create_alias_ce(struct index_state *istate, void set_object_name_for_intent_to_add_entry(struct cache_entry *ce) { struct object_id oid; - if (write_object_file("", 0, blob_type, &oid)) + if (write_object_file("", 0, OBJ_BLOB, &oid)) die(_("cannot create an empty blob in the object database")); oidcpy(&ce->oid, &oid); } From patchwork Fri Feb 4 13:51:19 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: 12735099 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 916E2C433EF for ; Fri, 4 Feb 2022 13:51:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359136AbiBDNvp (ORCPT ); Fri, 4 Feb 2022 08:51:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244829AbiBDNvg (ORCPT ); Fri, 4 Feb 2022 08:51:36 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87675C061714 for ; Fri, 4 Feb 2022 05:51:36 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id s18so11457608wrv.7 for ; Fri, 04 Feb 2022 05:51:36 -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=xNOJUJlkCD8qpcw0wiZZtHtfKdSJJ3TMF/8hSuye8dk=; b=f+gt/ydQOiGJJSZQhRamTyzqONP89iwETkSV9oPhHvT6e04GL2k0oWZ8HkjbO4YaaM 0PfhWklEkCBz8ijVM5F3b1F9Un1yxqlaw6fF5OPaArssvtf8j+wxA82rOQVouCxjrBPq y0cOJHjRA+yYsdLwVd2xWTrWOD1hhL4rxwPmW9Ulsfsr/V31m9i8p+Of9eLGclAdCfvU pZa4thtQvUxouNrpsq0ShRQRyY9MCBaqk4ch9gH/JvUdTzX8q4ILQhTtYqVETxoKPlRe /Iky5fpPZqYNqQqOkhy7Nv1aSh2GLwMq1W2vJw9/X4iTv5ILiCEotm7vI4SeTGKgw8c3 7xeQ== 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=xNOJUJlkCD8qpcw0wiZZtHtfKdSJJ3TMF/8hSuye8dk=; b=JxN4A63UdcMCYUlmqUj/n6KUV62Rmmvkw6i/NZeUGUkONyN2bLEhATsbwNOoIORadn IW2Grk/Q6cfAGJnX4BBKtBjcacEj3/GEWfqNef7zEiZXowBv+3CYrS0RQHJM2Hn3GaNc pNVPR5g4oaZU0Y2tTnWFXa6tK2sj+nhyeLsYfllfBVxi1QouvQLHaCkhbHQGxsj/Dcb5 hCd56E3XW0f8gmizihos+meAv7P0vdTF6l/qpjoaVez7sxcjO3uiRTCsJORQa4P40Z03 RI3Y70dXS8ZD7Z9HlZx2AKDA48WRWyVPXZroSrCsO0OZ4vUGE9CJNk01LHF6cF4pRwWL fI6Q== X-Gm-Message-State: AOAM531oKpWP7JoCPVD92KStKs/aepCCt2F74PeUzt+k4DFqf/rErHfp O20mkMt8lZOB427I3dDbuB7GuQ3GzbQMzg== X-Google-Smtp-Source: ABdhPJycvz4VT3K8FLjc/XVNyP6+hbXjbBzdnPPNa+BTWjSPLAW3HYMacWf0uI4eBGxWVLAhBP57Zg== X-Received: by 2002:a5d:5052:: with SMTP id h18mr2681740wrt.350.1643982694780; Fri, 04 Feb 2022 05:51:34 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z2sm205963wmi.22.2022.02.04.05.51.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 05:51:34 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH v2 05/11] object API: correct "buf" v.s. "map" mismatch in *.c and *.h Date: Fri, 4 Feb 2022 14:51:19 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the name of the second argument to check_object_signature() to be "buf" in object-file.c, making it consistent with the prototype in cache..h This fixes an inconsistency that's been with us since 2ade9340262 (Add "check_sha1_signature()" helper function, 2005-04-08), and makes a subsequent commit's diff smaller, as we'll move these API docs to cache.h. While we're at it fix a small grammar error in the documentation, dropping an "an" before "in-core object-data". Signed-off-by: Ævar Arnfjörð Bjarmason --- object-file.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/object-file.c b/object-file.c index 59eb793e0ac..271acf4dd15 100644 --- a/object-file.c +++ b/object-file.c @@ -1067,13 +1067,13 @@ int format_object_header(char *str, size_t size, enum object_type type, } /* - * With an in-core object data in "map", rehash it to make sure the + * With in-core object data in "buf", rehash it to make sure the * object name actually matches "oid" to detect object corruption. - * With "map" == NULL, try reading the object named with "oid" using + * With "buf" == NULL, try reading the object named with "oid" using * the streaming interface and rehash it to do the same. */ int check_object_signature(struct repository *r, const struct object_id *oid, - void *map, unsigned long size, const char *type, + void *buf, unsigned long size, const char *type, struct object_id *real_oidp) { struct object_id tmp; @@ -1084,8 +1084,8 @@ int check_object_signature(struct repository *r, const struct object_id *oid, char hdr[MAX_HEADER_LEN]; int hdrlen; - if (map) { - hash_object_file(r->hash_algo, map, size, type, real_oid); + if (buf) { + hash_object_file(r->hash_algo, buf, size, type, real_oid); return !oideq(oid, real_oid) ? -1 : 0; } From patchwork Fri Feb 4 13:51:20 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: 12735100 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 F05F5C433F5 for ; Fri, 4 Feb 2022 13:51:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359142AbiBDNvr (ORCPT ); Fri, 4 Feb 2022 08:51:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359091AbiBDNvi (ORCPT ); Fri, 4 Feb 2022 08:51:38 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9083BC06173D for ; Fri, 4 Feb 2022 05:51:37 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id j25so10763273wrb.2 for ; Fri, 04 Feb 2022 05:51:37 -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=C3ei4Ewu2EZROseYDP4edKEuj5ZPhDL+Wzjsg1FbPdc=; b=bWNr9c/fbfoTL1KymWeUJba5V3e2O7JPkpUW4/8X1/cmRg3H6Wze3TBRO/a4d/kgVv eFtccNQ1tQwTPwc/vfrfgm3cMCQxueSwc4YAfnZJYEiTNG9j80P633MF9yX+UFU8t+6e N7D+U1WpBWAXMLw0jQytXirIYJwQcWoW1/dex5Hv7/euZOkGHmROkmJuiXsjnPFre/IN nFrnmAcM2I3JctPkg4kmExI3uVJzC/W3TUqnvx3DGF5gYOlGl00BLpBr9+XYnRkuTkRt oz73DOc01WlFFVdnkVb4EQSazjzvldDOsNPLP0lDpTrM+TFjDJiV5meAS6Y2irhdTDtY YkDg== 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=C3ei4Ewu2EZROseYDP4edKEuj5ZPhDL+Wzjsg1FbPdc=; b=H26hWBkGoo3JRZBdE1t/LVEwlrzm0zNjgNN35v+y46uutZNbBWmgYjE28bWGDjx3ne UDlIJO951PGHZdzWn4o1nUvDojvz5KrrZFu3XxWB2sA5vU5QBqZppAgxvR1FKZw4bp7m xLFMRzlhmhJOdYPsDqbSKY1Xp/UjgL24v42cYqzFIgfxV9Ffo3JoMff6IK0i51Sz6x+B rV9wEskGEl3LCqPWtjvs/4crCibW8fn4S02cInYl7VgsiMvaZrzImohbchZPiCaeTqkw BJXkgPESk8iSaOKcWzd9+mAe1H5XeVNPuLEUUDY2bk2SArzTROidWhcU29iqmFa2Dlet UL8Q== X-Gm-Message-State: AOAM530TtLvneVWZblwEUIBMeCn8sZKJ9ALSDUW67OnrJcjHPd9HXF8A 3sIV/WyhUvxSXdS7YUSt8MCnHy00bXMFWg== X-Google-Smtp-Source: ABdhPJw30IgeuIzBuhgxC8BfUJuwhnmiaQfBGg0drlpJ4w6js/wLjkVbACfwA2Ldr+cSkMqSml7L7A== X-Received: by 2002:a5d:4d50:: with SMTP id a16mr2596492wru.182.1643982695858; Fri, 04 Feb 2022 05:51:35 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z2sm205963wmi.22.2022.02.04.05.51.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 05:51:35 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH v2 06/11] object API: make check_object_signature() oideq()-like, move docs Date: Fri, 4 Feb 2022 14:51:20 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Make the return value of check_object_signature() behave like oideq() and memcmp() instead of returning negative values on failure. This reduces the boilerplate required when calling the function, and makes the calling code behave the same is if though we'd called oideq(), which is basically what we're doing here. We already had some callers using "f() < 0", with others using "!f()". Instead of declaring the latter a bug let's convert all callers to it. It is unfortunate that there's also cases where we "return -1" on various errors, and we can't tell those apart from the expected OID being less than the real OID, but this was the case already. This change is rather dangerous stand-alone as we're changing the return semantics, but not changing the prototype. Therefore any out-of-tree code rebased on this change would start doing the opposite of what it was meant to do. In a subsequent commit we'll make that a non-issue by changing the signature of the function, ensuring that the compiler will catch any such misuse. While we're at it let's re-flow some of the callers to wrap at 79 characters, and move the API documentation to cache.h, where the prototype of this function lives. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/fast-export.c | 4 ++-- builtin/index-pack.c | 5 ++--- builtin/mktag.c | 5 ++--- cache.h | 9 +++++++++ object-file.c | 16 +++++----------- object.c | 6 +++--- pack-check.c | 4 ++-- 7 files changed, 25 insertions(+), 24 deletions(-) diff --git a/builtin/fast-export.c b/builtin/fast-export.c index 9f1c730e587..7a79cb186b1 100644 --- a/builtin/fast-export.c +++ b/builtin/fast-export.c @@ -299,8 +299,8 @@ static void export_blob(const struct object_id *oid) buf = read_object_file(oid, &type, &size); if (!buf) die("could not read blob %s", oid_to_hex(oid)); - if (check_object_signature(the_repository, oid, buf, size, - type_name(type), NULL) < 0) + if (!check_object_signature(the_repository, oid, buf, size, + type_name(type), NULL)) die("oid mismatch in blob %s", oid_to_hex(oid)); object = parse_object_buffer(the_repository, oid, type, size, buf, &eaten); diff --git a/builtin/index-pack.c b/builtin/index-pack.c index 01574378ce2..6db3e728ff4 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -1412,9 +1412,8 @@ static void fix_unresolved_deltas(struct hashfile *f) if (!data) continue; - if (check_object_signature(the_repository, &d->oid, - data, size, - type_name(type), NULL)) + if (!check_object_signature(the_repository, &d->oid, data, + size, type_name(type), NULL)) die(_("local object %s is corrupt"), oid_to_hex(&d->oid)); /* diff --git a/builtin/mktag.c b/builtin/mktag.c index 96a3686af53..a715bf53cf0 100644 --- a/builtin/mktag.c +++ b/builtin/mktag.c @@ -61,9 +61,8 @@ static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type) type_name(*tagged_type), type_name(type)); repl = lookup_replace_object(the_repository, tagged_oid); - ret = check_object_signature(the_repository, repl, - buffer, size, type_name(*tagged_type), - NULL); + ret = !check_object_signature(the_repository, repl, buffer, size, + type_name(*tagged_type), NULL); free(buffer); return ret; diff --git a/cache.h b/cache.h index 281f00ab1b1..3a156dcb37b 100644 --- a/cache.h +++ b/cache.h @@ -1319,6 +1319,15 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream, struct object_info; int parse_loose_header(const char *hdr, struct object_info *oi); +/** + * With in-core object data in "buf", rehash it to make sure the + * object name actually matches "oid" to detect object corruption. + * With "buf" == NULL, try reading the object named with "oid" using + * the streaming interface and rehash it to do the same. + * + * Treat the return value like oideq() (which is like memcmp()), + * except that negative values might also indicate a generic error. + */ int check_object_signature(struct repository *r, const struct object_id *oid, void *buf, unsigned long size, const char *type, struct object_id *real_oidp); diff --git a/object-file.c b/object-file.c index 271acf4dd15..4c38ddad5dc 100644 --- a/object-file.c +++ b/object-file.c @@ -1066,12 +1066,6 @@ int format_object_header(char *str, size_t size, enum object_type type, return format_object_header_literally(str, size, name, objsize); } -/* - * With in-core object data in "buf", rehash it to make sure the - * object name actually matches "oid" to detect object corruption. - * With "buf" == NULL, try reading the object named with "oid" using - * the streaming interface and rehash it to do the same. - */ int check_object_signature(struct repository *r, const struct object_id *oid, void *buf, unsigned long size, const char *type, struct object_id *real_oidp) @@ -1086,7 +1080,7 @@ int check_object_signature(struct repository *r, const struct object_id *oid, if (buf) { hash_object_file(r->hash_algo, buf, size, type, real_oid); - return !oideq(oid, real_oid) ? -1 : 0; + return oideq(oid, real_oid); } st = open_istream(r, oid, &obj_type, &size, NULL); @@ -1113,7 +1107,7 @@ int check_object_signature(struct repository *r, const struct object_id *oid, } r->hash_algo->final_oid_fn(real_oid, &c); close_istream(st); - return !oideq(oid, real_oid) ? -1 : 0; + return oideq(oid, real_oid); } int git_open_cloexec(const char *name, int flags) @@ -2617,9 +2611,9 @@ int read_loose_object(const char *path, git_inflate_end(&stream); goto out; } - if (check_object_signature(the_repository, expected_oid, - *contents, *size, - oi->type_name->buf, real_oid)) + if (!check_object_signature(the_repository, expected_oid, + *contents, *size, + oi->type_name->buf, real_oid)) goto out; } diff --git a/object.c b/object.c index c37501fc120..b778b32407d 100644 --- a/object.c +++ b/object.c @@ -279,7 +279,7 @@ struct object *parse_object(struct repository *r, const struct object_id *oid) if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) || (!obj && repo_has_object_file(r, oid) && oid_object_info(r, oid, NULL) == OBJ_BLOB)) { - if (check_object_signature(r, repl, NULL, 0, NULL, NULL) < 0) { + if (!check_object_signature(r, repl, NULL, 0, NULL, NULL)) { error(_("hash mismatch %s"), oid_to_hex(oid)); return NULL; } @@ -289,8 +289,8 @@ struct object *parse_object(struct repository *r, const struct object_id *oid) buffer = repo_read_object_file(r, oid, &type, &size); if (buffer) { - if (check_object_signature(r, repl, buffer, size, - type_name(type), NULL) < 0) { + if (!check_object_signature(r, repl, buffer, size, + type_name(type), NULL)) { free(buffer); error(_("hash mismatch %s"), oid_to_hex(repl)); return NULL; diff --git a/pack-check.c b/pack-check.c index 3f418e3a6af..35cca10057c 100644 --- a/pack-check.c +++ b/pack-check.c @@ -142,8 +142,8 @@ static int verify_packfile(struct repository *r, err = error("cannot unpack %s from %s at offset %"PRIuMAX"", oid_to_hex(&oid), p->pack_name, (uintmax_t)entries[i].offset); - else if (check_object_signature(r, &oid, data, size, - type_name(type), NULL)) + else if (!check_object_signature(r, &oid, data, size, + type_name(type), NULL)) err = error("packed %s from %s is corrupt", oid_to_hex(&oid), p->pack_name); else if (fn) { From patchwork Fri Feb 4 13:51:21 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: 12735101 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 ED3B6C433FE for ; Fri, 4 Feb 2022 13:51:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231415AbiBDNvt (ORCPT ); Fri, 4 Feb 2022 08:51:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359057AbiBDNvi (ORCPT ); Fri, 4 Feb 2022 08:51:38 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CE99C061741 for ; Fri, 4 Feb 2022 05:51:38 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id r17so2404846wrr.6 for ; Fri, 04 Feb 2022 05:51:38 -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=+OX4VvC6TZdA7i9Mo5h/ubMaXxxcsZN+A+0j5WjZ564=; b=U7t1TdD5Pi1d9ZJd8MxiR3ln11t11uzALE1AolNgHOTJq9lhtINaLJUHrOMo5pYeyp CDeiAvFcwjZQETbKwpzqFGHxUohcyUkBDw5mwVm2Iqi15SqJ5Oc9Bv4LylDkIJWD6il6 d5284zULSoOTmXCqt7sk2SzPYnb/UX53W1vPJ0MBSF+HqfMbeKTTDAtg5+VQcP4czvaF odk0k8MACNFQpniu2nKdK5RcZMUpUjercE+1/b+FAKz51qx/e870KDp0JSDhsr/tvFzt sIBQL7ZgoEmEoMHwzkQorcF9dSe2QUnY/qpGb2YJJZGI8/EufFtKSigZ9lrFAsCdoyVD khwQ== 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=+OX4VvC6TZdA7i9Mo5h/ubMaXxxcsZN+A+0j5WjZ564=; b=jQwAX1NdTKTFznok1YIgumbiGKZt7/DzXsfaCsp6ns1GMIOLs8+xU6TLRQV0VmG9lA iG0h3Lnvyi1RGTXC+kJD4Ejm33VZVMu8ALs/H3ZNTYcMYU61w0AYIhTKpxhU+G5ey0DE +C82lskyw/mUqcdTH5qoVqPZw8SHwqGppxw6ADRlZ69C440VuAJ0H4Oi9wJo5eUi+9Vr BvGSuazUyu8lUIkF2hDz5IihX66/ndMuJg6VXYLuKZRNmRjwOQLKNUqSDzSrV+hSn5O/ 59J6GaLqfaUPs6qZn9BDgDBCdXOc1i5pt3gzB4En4K7k0XyvkEyrHG5GtPI3QcmJ+dVC EWWg== X-Gm-Message-State: AOAM530xyajIo/6C/t1NCCT4f+f27Up6uEfl/QTaUCB4DMMkoxHoaQcz +u8Qd1o879UFAKfNR8YRtVbIf9QjB0BHxQ== X-Google-Smtp-Source: ABdhPJwz7khHlSOQhWqGVSFP30y9RcS4xqSiV9McN2QSqAXlsREWpsgeII9/RoGK4xudIViN9CJkYw== X-Received: by 2002:adf:802a:: with SMTP id 39mr231366wrk.533.1643982696825; Fri, 04 Feb 2022 05:51:36 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z2sm205963wmi.22.2022.02.04.05.51.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 05:51:36 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH v2 07/11] object-file API: split up and simplify check_object_signature() Date: Fri, 4 Feb 2022 14:51:21 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Split up the check_object_signature() function into that non-streaming version (it accepts an already filled "buf"), and a new stream_object_signature() which will retrieve the object from storage, and hash it on-the-fly. All of the callers of check_object_signature() were effectively calling two different functions, if we go by cyclomatic complexity. I.e. they'd either take the early "if (map)" branch and return early, or not. This has been the case since the "if (map)" condition was added in 090ea12671b (parse_object: avoid putting whole blob in core, 2012-03-07). We can then further simplify the resulting check_object_signature() function since only one caller wanted to pass a non-NULL "buf" and a non-NULL "real_oidp". That "read_loose_object()" codepath used by "git fsck" can instead use hash_object_file() followed by oideq(). Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/fast-export.c | 2 +- builtin/index-pack.c | 2 +- builtin/mktag.c | 2 +- cache.h | 12 ++++++++---- object-file.c | 33 +++++++++++++++++++-------------- object.c | 4 ++-- pack-check.c | 9 ++++++--- 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/builtin/fast-export.c b/builtin/fast-export.c index 7a79cb186b1..1878ec34960 100644 --- a/builtin/fast-export.c +++ b/builtin/fast-export.c @@ -300,7 +300,7 @@ static void export_blob(const struct object_id *oid) if (!buf) die("could not read blob %s", oid_to_hex(oid)); if (!check_object_signature(the_repository, oid, buf, size, - type_name(type), NULL)) + type_name(type))) die("oid mismatch in blob %s", oid_to_hex(oid)); object = parse_object_buffer(the_repository, oid, type, size, buf, &eaten); diff --git a/builtin/index-pack.c b/builtin/index-pack.c index 6db3e728ff4..eaa7b6698d1 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -1413,7 +1413,7 @@ static void fix_unresolved_deltas(struct hashfile *f) continue; if (!check_object_signature(the_repository, &d->oid, data, - size, type_name(type), NULL)) + size, type_name(type))) die(_("local object %s is corrupt"), oid_to_hex(&d->oid)); /* diff --git a/builtin/mktag.c b/builtin/mktag.c index a715bf53cf0..1282e8cd9e2 100644 --- a/builtin/mktag.c +++ b/builtin/mktag.c @@ -62,7 +62,7 @@ static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type) repl = lookup_replace_object(the_repository, tagged_oid); ret = !check_object_signature(the_repository, repl, buffer, size, - type_name(*tagged_type), NULL); + type_name(*tagged_type)); free(buffer); return ret; diff --git a/cache.h b/cache.h index 3a156dcb37b..5a13bbfd904 100644 --- a/cache.h +++ b/cache.h @@ -1322,15 +1322,19 @@ int parse_loose_header(const char *hdr, struct object_info *oi); /** * With in-core object data in "buf", rehash it to make sure the * object name actually matches "oid" to detect object corruption. - * With "buf" == NULL, try reading the object named with "oid" using - * the streaming interface and rehash it to do the same. * * Treat the return value like oideq() (which is like memcmp()), * except that negative values might also indicate a generic error. */ int check_object_signature(struct repository *r, const struct object_id *oid, - void *buf, unsigned long size, const char *type, - struct object_id *real_oidp); + void *buf, unsigned long size, const char *type); + +/** + * A streaming version of check_object_signature(). + * Try reading the object named with "oid" using + * the streaming interface and rehash it to do the same. + */ +int stream_object_signature(struct repository *r, const struct object_id *oid); int finalize_object_file(const char *tmpfile, const char *filename); diff --git a/object-file.c b/object-file.c index 4c38ddad5dc..937a107c5ab 100644 --- a/object-file.c +++ b/object-file.c @@ -1067,22 +1067,25 @@ int format_object_header(char *str, size_t size, enum object_type type, } int check_object_signature(struct repository *r, const struct object_id *oid, - void *buf, unsigned long size, const char *type, - struct object_id *real_oidp) + void *buf, unsigned long size, const char *type) { - struct object_id tmp; - struct object_id *real_oid = real_oidp ? real_oidp : &tmp; + struct object_id real_oid; + + hash_object_file(r->hash_algo, buf, size, type, &real_oid); + + return oideq(oid, &real_oid); +} + +int stream_object_signature(struct repository *r, const struct object_id *oid) +{ + struct object_id real_oid; + unsigned long size; enum object_type obj_type; struct git_istream *st; git_hash_ctx c; char hdr[MAX_HEADER_LEN]; int hdrlen; - if (buf) { - hash_object_file(r->hash_algo, buf, size, type, real_oid); - return oideq(oid, real_oid); - } - st = open_istream(r, oid, &obj_type, &size, NULL); if (!st) return -1; @@ -1105,9 +1108,9 @@ int check_object_signature(struct repository *r, const struct object_id *oid, break; r->hash_algo->update_fn(&c, buf, readlen); } - r->hash_algo->final_oid_fn(real_oid, &c); + r->hash_algo->final_oid_fn(&real_oid, &c); close_istream(st); - return oideq(oid, real_oid); + return oideq(oid, &real_oid); } int git_open_cloexec(const char *name, int flags) @@ -2611,9 +2614,11 @@ int read_loose_object(const char *path, git_inflate_end(&stream); goto out; } - if (!check_object_signature(the_repository, expected_oid, - *contents, *size, - oi->type_name->buf, real_oid)) + + hash_object_file(the_repository->hash_algo, + *contents, *size, oi->type_name->buf, + real_oid); + if (!oideq(expected_oid, real_oid)) goto out; } diff --git a/object.c b/object.c index b778b32407d..b3f9cd5c8cd 100644 --- a/object.c +++ b/object.c @@ -279,7 +279,7 @@ struct object *parse_object(struct repository *r, const struct object_id *oid) if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) || (!obj && repo_has_object_file(r, oid) && oid_object_info(r, oid, NULL) == OBJ_BLOB)) { - if (!check_object_signature(r, repl, NULL, 0, NULL, NULL)) { + if (!stream_object_signature(r, repl)) { error(_("hash mismatch %s"), oid_to_hex(oid)); return NULL; } @@ -290,7 +290,7 @@ struct object *parse_object(struct repository *r, const struct object_id *oid) buffer = repo_read_object_file(r, oid, &type, &size); if (buffer) { if (!check_object_signature(r, repl, buffer, size, - type_name(type), NULL)) { + type_name(type))) { free(buffer); error(_("hash mismatch %s"), oid_to_hex(repl)); return NULL; diff --git a/pack-check.c b/pack-check.c index 35cca10057c..4a27e77704b 100644 --- a/pack-check.c +++ b/pack-check.c @@ -127,7 +127,7 @@ static int verify_packfile(struct repository *r, if (type == OBJ_BLOB && big_file_threshold <= size) { /* - * Let check_object_signature() check it with + * Let stream_object_signature() check it with * the streaming interface; no point slurping * the data in-core only to discard. */ @@ -142,8 +142,11 @@ static int verify_packfile(struct repository *r, err = error("cannot unpack %s from %s at offset %"PRIuMAX"", oid_to_hex(&oid), p->pack_name, (uintmax_t)entries[i].offset); - else if (!check_object_signature(r, &oid, data, size, - type_name(type), NULL)) + else if (data && !check_object_signature(r, &oid, data, size, + type_name(type))) + err = error("packed %s from %s is corrupt", + oid_to_hex(&oid), p->pack_name); + else if (!data && !stream_object_signature(r, &oid)) err = error("packed %s from %s is corrupt", oid_to_hex(&oid), p->pack_name); else if (fn) { From patchwork Fri Feb 4 13:51:22 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: 12735102 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 31267C433EF for ; Fri, 4 Feb 2022 13:51:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359162AbiBDNvv (ORCPT ); Fri, 4 Feb 2022 08:51:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359112AbiBDNvn (ORCPT ); Fri, 4 Feb 2022 08:51:43 -0500 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 677FAC061748 for ; Fri, 4 Feb 2022 05:51:39 -0800 (PST) Received: by mail-wr1-x436.google.com with SMTP id s10so8889185wra.5 for ; Fri, 04 Feb 2022 05:51:39 -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=0Uv4S67Xcz3inlJE07/KCN9TAUKGS7aObC8hVWLuWOY=; b=WMB4jxaZRXmcyOy9BNAH431WOeWSvc5dDYUcAmY/cxlcHGmpdV55B1CSc1yNSml7Q6 8+3FGR3cNWKXs9KuoaGhJfaVz5pAmPWnj5Zd9Dr0R8LzpJCjY95RyjqavOEYcS3ai9D9 bgSd57Zkrw0LfB5P2FEv2ZqJjV1/ouFDtN/Ync4KCUAwOVuZt72RHep2yGKBDo4y9c3C t/QIXd2lgs/QKA1Ecq2JSmZt1WAneZSwhp6isUBlAAY8CJpQUDeXl1tibeceoZL1ELVQ Q6cNkVlICp/Jh/Plb0DivjgysMXFK86NuIjkwqHirtdVz2oU+5MuyCV+FLPp8yVzxMTV p42Q== 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=0Uv4S67Xcz3inlJE07/KCN9TAUKGS7aObC8hVWLuWOY=; b=iSTHrSGLhcvTRt2acftubOSuvbe9ZsbiWNKiy6cPyIZrksHAAnJntxvvQDJVuo327j EpZjCjM3UPOM7bzHkX4KwGNFAgDSn/gbUwggM4Lgoio6crG7r5e0E7RgIHY6OYM/cqXF dnKlMbbjBpr3HNvjPuaw0zaZ9iGZXs7fQ/Y+CcxcjC4DSntXr8U+vnz44Wxb3hS2uI1m s4tdOf4V6eth6AAIZlZoo6c8Qk4t1qYpgfg1Ej/y4q32uVkNgESYGOApijm7P6Oa7IFS ohLqpj7xBOEIlSRd+bW3P95T3TnHIwkIlKqJu/gS4aE2CDHZow/Jz2OyhZAt73G83GlC fysg== X-Gm-Message-State: AOAM531TJ5ezjvqKFSeBcexaglsYudqiqj5ZLbDR8PzMBEuCS6Xwfbf5 qBxB3QbDIzmcCht6jcWm9c1XPxiu3IxDUg== X-Google-Smtp-Source: ABdhPJxcfS0ykzlPLXUyCU7QqKLqOWiihj2hBQLvNQnEt4+Wkz7SAytNMNa9/snHVIuXJWKLGogSaA== X-Received: by 2002:a5d:67cf:: with SMTP id n15mr2544541wrw.673.1643982697818; Fri, 04 Feb 2022 05:51:37 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z2sm205963wmi.22.2022.02.04.05.51.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 05:51:37 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH v2 08/11] object API: rename hash_object_file_literally() to write_*() Date: Fri, 4 Feb 2022 14:51:22 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Before 0c3db67cc81 (hash-object --literally: fix buffer overrun with extra-long object type, 2015-05-04) the hash-object code being changed here called write_sha1_file() to both hash and write a loose object. Before that we'd use hash_sha1_file() to if "-w" wasn't provided, and otherwise call write_sha1_file(). Now we'll always call the same function for both writing. Let's rename it from hash_*_literally() to write_*_literally(). Even though the write_*() might not actually write if HASH_WRITE_OBJECT isn't in "flags", having it be more similar to write_object_file_flags() than hash_object_file(), but carrying a name that would suggest that it's a variant of the latter is confusing. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/hash-object.c | 2 +- object-file.c | 6 +++--- object-store.h | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/builtin/hash-object.c b/builtin/hash-object.c index c7b3ad74c60..f4cbd9c4ae3 100644 --- a/builtin/hash-object.c +++ b/builtin/hash-object.c @@ -25,7 +25,7 @@ static int hash_literally(struct object_id *oid, int fd, const char *type, unsig if (strbuf_read(&buf, fd, 4096) < 0) ret = -1; else - ret = hash_object_file_literally(buf.buf, buf.len, type, oid, + ret = write_object_file_literally(buf.buf, buf.len, type, oid, flags); strbuf_release(&buf); return ret; diff --git a/object-file.c b/object-file.c index 937a107c5ab..a68af32308e 100644 --- a/object-file.c +++ b/object-file.c @@ -2027,9 +2027,9 @@ int write_object_file_flags(const void *buf, unsigned long len, return write_loose_object(oid, hdr, hdrlen, buf, len, 0, flags); } -int hash_object_file_literally(const void *buf, unsigned long len, - const char *type, struct object_id *oid, - unsigned flags) +int write_object_file_literally(const void *buf, unsigned long len, + const char *type, struct object_id *oid, + unsigned flags) { char *header; int hdrlen, status = 0; diff --git a/object-store.h b/object-store.h index eab1e2a967e..29169d117f7 100644 --- a/object-store.h +++ b/object-store.h @@ -258,9 +258,9 @@ static inline int write_object_file(const void *buf, unsigned long len, return write_object_file_flags(buf, len, type, oid, 0); } -int hash_object_file_literally(const void *buf, unsigned long len, - const char *type, struct object_id *oid, - unsigned flags); +int write_object_file_literally(const void *buf, unsigned long len, + const char *type, struct object_id *oid, + unsigned flags); /* * Add an object file to the in-memory object store, without writing it From patchwork Fri Feb 4 13:51:23 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: 12735103 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 5AD99C433F5 for ; Fri, 4 Feb 2022 13:51:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359185AbiBDNv5 (ORCPT ); Fri, 4 Feb 2022 08:51:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359122AbiBDNvo (ORCPT ); Fri, 4 Feb 2022 08:51:44 -0500 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA5E5C06174A for ; Fri, 4 Feb 2022 05:51:40 -0800 (PST) Received: by mail-wr1-x42f.google.com with SMTP id j16so11400645wrd.8 for ; Fri, 04 Feb 2022 05:51:40 -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=pZQMwPnTZDN/Pw0gAQDUSvBv7s6CzIEJb1hG0W17144=; b=Y6xn91nqLtpAf2dXl0r5FILl9jBEjviM2iKu69/3kY7eZxJQYHBZcI7p6Ysyf4/24D vVycv6p0/vlN3KiAC3NVfYI+k2vQdkDDhfDng3JIv+7eMQAI3RcB+nJ54GftmlAXVphc m6xq+yrJkVv/9f0KBbmgCUOZ+1G44NY2zxwN1Xmui3qVK2nGdUF+46Lxi33amGNQt5MK tN/LbbwkVsy+zcJqkPPtjINHpWw9l13FCZI+UJhtysAtP7skbPecx4FKAI0X/qA/XD/J kANTDClLdSxL3SboR9RCWmlqdk62UfK4GdyK8ON3GO4zP3qDK63hqCXHneUIR1SypExG YL1w== 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=pZQMwPnTZDN/Pw0gAQDUSvBv7s6CzIEJb1hG0W17144=; b=udtOwanCVk5afIect5jENRdSgCP1BynoNztYV5UljsxiBN3B16Lsvrwy6iG+ooGs0a nCqEJCB0cDIJiuMeXsGps2eNYJwMOnEA6/OiTRfGVEzd3a2EiFUbIPaaWkITeE1/j7vN RFf/U+kNJuqU3MY07Wk/xSiQanuHSG3OaLgIDQxmwnNlXKdTVNVKnPU/shfBG5tegXBB shhCTMbwwJHekVKuk5kXw13Cxcyw/PC1QpbLBRfZC2Azb87knbLfbQ63jEkf0J3INulZ MxQ4PyPEXlgtuoLov9m2hCaRR/dGyGgYX6CJRApENZeW2c5tEgcjj/c4nGw2nopwtWFB YDtA== X-Gm-Message-State: AOAM5329LkWwJbjJ2ddjAtwOn2hPQoQ2uQZ43Tt7YHOXBTIHj+89c13f WAxO6SiW1UvTB20xyNPpqfSFqqxqX8t4Sw== X-Google-Smtp-Source: ABdhPJyJXpEsNsM1f74JNPmJaoRn02KvzC4WUuHtyFLypO6olpTtTQtuFKMV9dtjGQzg7W1fGKd+7g== X-Received: by 2002:a05:6000:18af:: with SMTP id b15mr2464400wri.589.1643982699056; Fri, 04 Feb 2022 05:51:39 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z2sm205963wmi.22.2022.02.04.05.51.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 05:51:38 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH v2 09/11] object-file API: have hash_object_file() take "enum object_type" Date: Fri, 4 Feb 2022 14:51:23 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the hash_object_file() function to take an "enum object_type". Since a preceding commit all of its callers are passing either "{commit,tree,blob,tag}_type", or the result of a call to type_name(), the parse_object() caller that would pass NULL is now using stream_object_signature(). Signed-off-by: Ævar Arnfjörð Bjarmason --- apply.c | 4 ++-- builtin/fast-export.c | 2 +- builtin/index-pack.c | 4 ++-- builtin/mktag.c | 2 +- builtin/replace.c | 2 +- builtin/unpack-objects.c | 2 +- cache-tree.c | 6 +++--- cache.h | 3 ++- convert.c | 2 +- diffcore-rename.c | 2 +- dir.c | 2 +- log-tree.c | 2 +- object-file.c | 35 +++++++++++++++++++++-------------- object-store.h | 2 +- object.c | 3 +-- pack-check.c | 2 +- 16 files changed, 41 insertions(+), 34 deletions(-) diff --git a/apply.c b/apply.c index e82a7c30f01..8513755e254 100644 --- a/apply.c +++ b/apply.c @@ -3157,7 +3157,7 @@ static int apply_binary(struct apply_state *state, * See if the old one matches what the patch * applies to. */ - hash_object_file(the_hash_algo, img->buf, img->len, blob_type, + hash_object_file(the_hash_algo, img->buf, img->len, OBJ_BLOB, &oid); if (strcmp(oid_to_hex(&oid), patch->old_oid_prefix)) return error(_("the patch applies to '%s' (%s), " @@ -3203,7 +3203,7 @@ static int apply_binary(struct apply_state *state, name); /* verify that the result matches */ - hash_object_file(the_hash_algo, img->buf, img->len, blob_type, + hash_object_file(the_hash_algo, img->buf, img->len, OBJ_BLOB, &oid); if (strcmp(oid_to_hex(&oid), patch->new_oid_prefix)) return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"), diff --git a/builtin/fast-export.c b/builtin/fast-export.c index 1878ec34960..187985666e7 100644 --- a/builtin/fast-export.c +++ b/builtin/fast-export.c @@ -300,7 +300,7 @@ static void export_blob(const struct object_id *oid) if (!buf) die("could not read blob %s", oid_to_hex(oid)); if (!check_object_signature(the_repository, oid, buf, size, - type_name(type))) + type)) die("oid mismatch in blob %s", oid_to_hex(oid)); object = parse_object_buffer(the_repository, oid, type, size, buf, &eaten); diff --git a/builtin/index-pack.c b/builtin/index-pack.c index eaa7b6698d1..1c0d3971074 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -970,7 +970,7 @@ static struct base_data *resolve_delta(struct object_entry *delta_obj, if (!result_data) bad_object(delta_obj->idx.offset, _("failed to apply delta")); hash_object_file(the_hash_algo, result_data, result_size, - type_name(delta_obj->real_type), &delta_obj->idx.oid); + delta_obj->real_type, &delta_obj->idx.oid); sha1_object(result_data, NULL, result_size, delta_obj->real_type, &delta_obj->idx.oid); @@ -1413,7 +1413,7 @@ static void fix_unresolved_deltas(struct hashfile *f) continue; if (!check_object_signature(the_repository, &d->oid, data, - size, type_name(type))) + size, type)) die(_("local object %s is corrupt"), oid_to_hex(&d->oid)); /* diff --git a/builtin/mktag.c b/builtin/mktag.c index 1282e8cd9e2..bebc88faed0 100644 --- a/builtin/mktag.c +++ b/builtin/mktag.c @@ -62,7 +62,7 @@ static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type) repl = lookup_replace_object(the_repository, tagged_oid); ret = !check_object_signature(the_repository, repl, buffer, size, - type_name(*tagged_type)); + *tagged_type); free(buffer); return ret; diff --git a/builtin/replace.c b/builtin/replace.c index 995ca683302..53a3b246570 100644 --- a/builtin/replace.c +++ b/builtin/replace.c @@ -409,7 +409,7 @@ static int check_one_mergetag(struct commit *commit, int i; hash_object_file(the_hash_algo, extra->value, extra->len, - type_name(OBJ_TAG), &tag_oid); + OBJ_TAG, &tag_oid); tag = lookup_tag(the_repository, &tag_oid); if (!tag) return error(_("bad mergetag in commit '%s'"), ref); diff --git a/builtin/unpack-objects.c b/builtin/unpack-objects.c index 808041551fd..dbeb0680a58 100644 --- a/builtin/unpack-objects.c +++ b/builtin/unpack-objects.c @@ -266,7 +266,7 @@ static void write_object(unsigned nr, enum object_type type, } else { struct object *obj; int eaten; - hash_object_file(the_hash_algo, buf, size, type_name(type), + hash_object_file(the_hash_algo, buf, size, type, &obj_list[nr].oid); added_object(nr, type, buf, size); obj = parse_object_buffer(the_repository, &obj_list[nr].oid, diff --git a/cache-tree.c b/cache-tree.c index fdbbd191d22..6752f69d515 100644 --- a/cache-tree.c +++ b/cache-tree.c @@ -432,14 +432,14 @@ static int update_one(struct cache_tree *it, if (repair) { struct object_id oid; hash_object_file(the_hash_algo, buffer.buf, buffer.len, - tree_type, &oid); + OBJ_TREE, &oid); if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT)) oidcpy(&it->oid, &oid); else to_invalidate = 1; } else if (dryrun) { hash_object_file(the_hash_algo, buffer.buf, buffer.len, - tree_type, &it->oid); + OBJ_TREE, &it->oid); } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE, &it->oid, flags & WRITE_TREE_SILENT ? HASH_SILENT : 0)) { @@ -948,7 +948,7 @@ static int verify_one(struct repository *r, strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0'); strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz); } - hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, tree_type, + hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE, &new_oid); if (!oideq(&new_oid, &it->oid)) BUG("cache-tree for path %.*s does not match. " diff --git a/cache.h b/cache.h index 5a13bbfd904..42761bb12cb 100644 --- a/cache.h +++ b/cache.h @@ -1327,7 +1327,8 @@ int parse_loose_header(const char *hdr, struct object_info *oi); * except that negative values might also indicate a generic error. */ int check_object_signature(struct repository *r, const struct object_id *oid, - void *buf, unsigned long size, const char *type); + void *map, unsigned long size, + enum object_type type); /** * A streaming version of check_object_signature(). diff --git a/convert.c b/convert.c index df7186bd813..d88072fe320 100644 --- a/convert.c +++ b/convert.c @@ -1159,7 +1159,7 @@ static int ident_to_worktree(const char *src, size_t len, /* are we "faking" in place editing ? */ if (src == buf->buf) to_free = strbuf_detach(buf, NULL); - hash_object_file(the_hash_algo, src, len, "blob", &oid); + hash_object_file(the_hash_algo, src, len, OBJ_BLOB, &oid); strbuf_grow(buf, len + cnt * (the_hash_algo->hexsz + 3)); for (;;) { diff --git a/diffcore-rename.c b/diffcore-rename.c index bebd4ed6a42..c0422d9e709 100644 --- a/diffcore-rename.c +++ b/diffcore-rename.c @@ -261,7 +261,7 @@ static unsigned int hash_filespec(struct repository *r, if (diff_populate_filespec(r, filespec, NULL)) return 0; hash_object_file(r->hash_algo, filespec->data, filespec->size, - "blob", &filespec->oid); + OBJ_BLOB, &filespec->oid); } return oidhash(&filespec->oid); } diff --git a/dir.c b/dir.c index d91295f2bcd..5674e8a6256 100644 --- a/dir.c +++ b/dir.c @@ -1113,7 +1113,7 @@ static int add_patterns(const char *fname, const char *base, int baselen, &istate->cache[pos]->oid); else hash_object_file(the_hash_algo, buf, size, - "blob", &oid_stat->oid); + OBJ_BLOB, &oid_stat->oid); fill_stat_data(&oid_stat->stat, &st); oid_stat->valid = 1; } diff --git a/log-tree.c b/log-tree.c index d3e7a40b648..27a5b0bd61a 100644 --- a/log-tree.c +++ b/log-tree.c @@ -561,7 +561,7 @@ static int show_one_mergetag(struct commit *commit, struct strbuf signature = STRBUF_INIT; hash_object_file(the_hash_algo, extra->value, extra->len, - type_name(OBJ_TAG), &oid); + OBJ_TAG, &oid); tag = lookup_tag(the_repository, &oid); if (!tag) return -1; /* error message already given */ diff --git a/object-file.c b/object-file.c index a68af32308e..97a3493f5fb 100644 --- a/object-file.c +++ b/object-file.c @@ -1067,7 +1067,8 @@ int format_object_header(char *str, size_t size, enum object_type type, } int check_object_signature(struct repository *r, const struct object_id *oid, - void *buf, unsigned long size, const char *type) + void *buf, unsigned long size, + enum object_type type) { struct object_id real_oid; @@ -1676,7 +1677,7 @@ int pretend_object_file(void *buf, unsigned long len, enum object_type type, { struct cached_object *co; - hash_object_file(the_hash_algo, buf, len, type_name(type), oid); + hash_object_file(the_hash_algo, buf, len, type, oid); if (has_object_file_with_flags(oid, OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT) || find_cached_object(oid)) return 0; @@ -1850,15 +1851,23 @@ static int write_buffer(int fd, const void *buf, size_t len) return 0; } -void hash_object_file(const struct git_hash_algo *algo, const void *buf, - unsigned long len, const char *type, - struct object_id *oid) +static void hash_object_file_literally(const struct git_hash_algo *algo, + const void *buf, unsigned long len, + const char *type, struct object_id *oid) { char hdr[MAX_HEADER_LEN]; int hdrlen = sizeof(hdr); + write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen); } +void hash_object_file(const struct git_hash_algo *algo, const void *buf, + unsigned long len, enum object_type type, + struct object_id *oid) +{ + hash_object_file_literally(algo, buf, len, type_name(type), oid); +} + /* Finalize a file on disk, and close it. */ static void close_loose_object(int fd) { @@ -2161,9 +2170,7 @@ static int index_mem(struct index_state *istate, if (write_object) ret = write_object_file(buf, size, type, oid); else - hash_object_file(the_hash_algo, buf, size, type_name(type), - oid); - + hash_object_file(the_hash_algo, buf, size, type, oid); if (re_allocated) free(buf); return ret; @@ -2189,8 +2196,8 @@ static int index_stream_convert_blob(struct index_state *istate, ret = write_object_file(sbuf.buf, sbuf.len, OBJ_BLOB, oid); else - hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, - type_name(OBJ_BLOB), oid); + hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, OBJ_BLOB, + oid); strbuf_release(&sbuf); return ret; } @@ -2309,7 +2316,7 @@ int index_path(struct index_state *istate, struct object_id *oid, return error_errno("readlink(\"%s\")", path); if (!(flags & HASH_WRITE_OBJECT)) hash_object_file(the_hash_algo, sb.buf, sb.len, - blob_type, oid); + OBJ_BLOB, oid); else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid)) rc = error(_("%s: failed to insert into database"), path); strbuf_release(&sb); @@ -2615,9 +2622,9 @@ int read_loose_object(const char *path, goto out; } - hash_object_file(the_repository->hash_algo, - *contents, *size, oi->type_name->buf, - real_oid); + hash_object_file_literally(the_repository->hash_algo, + *contents, *size, + oi->type_name->buf, real_oid); if (!oideq(expected_oid, real_oid)) goto out; } diff --git a/object-store.h b/object-store.h index 29169d117f7..bd2322ed8ce 100644 --- a/object-store.h +++ b/object-store.h @@ -246,7 +246,7 @@ static inline void *repo_read_object_file(struct repository *r, int oid_object_info(struct repository *r, const struct object_id *, unsigned long *); void hash_object_file(const struct git_hash_algo *algo, const void *buf, - unsigned long len, const char *type, + unsigned long len, enum object_type type, struct object_id *oid); int write_object_file_flags(const void *buf, unsigned long len, diff --git a/object.c b/object.c index b3f9cd5c8cd..2cc9edb7512 100644 --- a/object.c +++ b/object.c @@ -289,8 +289,7 @@ struct object *parse_object(struct repository *r, const struct object_id *oid) buffer = repo_read_object_file(r, oid, &type, &size); if (buffer) { - if (!check_object_signature(r, repl, buffer, size, - type_name(type))) { + if (!check_object_signature(r, repl, buffer, size, type)) { free(buffer); error(_("hash mismatch %s"), oid_to_hex(repl)); return NULL; diff --git a/pack-check.c b/pack-check.c index 4a27e77704b..f1df16ef9a7 100644 --- a/pack-check.c +++ b/pack-check.c @@ -143,7 +143,7 @@ static int verify_packfile(struct repository *r, oid_to_hex(&oid), p->pack_name, (uintmax_t)entries[i].offset); else if (data && !check_object_signature(r, &oid, data, size, - type_name(type))) + type)) err = error("packed %s from %s is corrupt", oid_to_hex(&oid), p->pack_name); else if (!data && !stream_object_signature(r, &oid)) From patchwork Fri Feb 4 13:51:24 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: 12735104 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 C6F3BC433EF for ; Fri, 4 Feb 2022 13:52:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359095AbiBDNwO (ORCPT ); Fri, 4 Feb 2022 08:52:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359106AbiBDNvr (ORCPT ); Fri, 4 Feb 2022 08:51:47 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7532C061758 for ; Fri, 4 Feb 2022 05:51:41 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id j25so10763650wrb.2 for ; Fri, 04 Feb 2022 05:51:41 -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=7IMVPDMfqLzYQtQ0XCwJw9tM7y3TAfj6XmtBZusbCdw=; b=PaoDvCpCHk9bkRL8VxUjW1gFG++fGILsnDDiMjJLoL8X4H09fjkED6TGmVBIPHGyO9 NYjGJGcT9gpPfxxbYYqURpKVtOr1jHXfqv3bdcV2Y8/I5CAch4vgmu3QZjUg08ICoEw2 yGISXkJpUZJDS2+zLugyNNzAF0XGw6rFTt75LigVD59Ix3ciV6pY+9ajIfeU4POghSVp ecwckFG7wueCgk8m0V56S2uWWlyxRRbtdtCvP/UUvPhgwzYI5va9wVfGLq4aX6/uausU kelx5ZcIcRDOrN1P0gPhuxJkj4Xr5rNLEtK/eif+HocIuXTZVQjv/u3lZ5EpvEM4nEgI GR8w== 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=7IMVPDMfqLzYQtQ0XCwJw9tM7y3TAfj6XmtBZusbCdw=; b=pKrMwMYmFVcY+5ShEy7MU3dOk1EJE5lTPhdKHnOc74d3IMsoBGXfwEF4xABcjx3bxD D/mISNBKcGSF3n1+w8Dy6Iu5acG7Le4BIFo7AXxNfzxT/KssKWRPcpDdDUGoE37Hud8o g4v/kuMBBPLEvIt9EGhRD8jIia1DCdxgSlJMqY6Btocs9ZfzuAUFW0n1DfB7tkSG9P2v LpND1RyG5a4/ZeGb1ZqoJElDCx/Q5yd+TcaRCnIjVi2N0yX2SuT79AeYYKakrZP9Of6p qWmz34knZoM2OwWYWze83QT1ozCK3BBTHmQVYglor3+u39AC+08ZsvB9jk4kYxdSz6Ao mAEQ== X-Gm-Message-State: AOAM532RS/u965WnNxf0Xd8xzcgbHZYU7JqnP9XNcEqjfb8y+BwFirLZ i9CuhsU8JAeJFIg9BIPTzJZFuUa2QI7SwQ== X-Google-Smtp-Source: ABdhPJyCq9n/aVcNU2O4z4Z12Kj5cbcfVzwDMVl5S9esu+T5qFpXwpONK3kphvx9p1sd9ccymWuHIA== X-Received: by 2002:adf:ed4c:: with SMTP id u12mr2539861wro.110.1643982700061; Fri, 04 Feb 2022 05:51:40 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z2sm205963wmi.22.2022.02.04.05.51.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 05:51:39 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH v2 10/11] object-file.c: add a literal version of write_object_file_prepare() Date: Fri, 4 Feb 2022 14:51:24 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Split off a *_literally() variant of the write_object_file_prepare() function. To do this create a new "hash_object_body()" static helper. We now defer the type_name() call until the very last moment in format_object_header() for those callers that aren't "hash-object --literally". Signed-off-by: Ævar Arnfjörð Bjarmason --- object-file.c | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/object-file.c b/object-file.c index 97a3493f5fb..cba33b25197 100644 --- a/object-file.c +++ b/object-file.c @@ -1784,21 +1784,40 @@ void *read_object_with_reference(struct repository *r, } } +static void hash_object_body(const struct git_hash_algo *algo, git_hash_ctx *c, + const void *buf, unsigned long len, + struct object_id *oid, + char *hdr, int *hdrlen) +{ + algo->init_fn(c); + algo->update_fn(c, hdr, *hdrlen); + algo->update_fn(c, buf, len); + algo->final_oid_fn(oid, c); +} + static void write_object_file_prepare(const struct git_hash_algo *algo, const void *buf, unsigned long len, - const char *type, struct object_id *oid, + enum object_type type, struct object_id *oid, char *hdr, int *hdrlen) { git_hash_ctx c; /* Generate the header */ - *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len); + *hdrlen = format_object_header(hdr, *hdrlen, type, len); /* Sha1.. */ - algo->init_fn(&c); - algo->update_fn(&c, hdr, *hdrlen); - algo->update_fn(&c, buf, len); - algo->final_oid_fn(oid, &c); + hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen); +} + +static void write_object_file_prepare_literally(const struct git_hash_algo *algo, + const void *buf, unsigned long len, + const char *type, struct object_id *oid, + char *hdr, int *hdrlen) +{ + git_hash_ctx c; + + *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len); + hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen); } /* @@ -1858,7 +1877,7 @@ static void hash_object_file_literally(const struct git_hash_algo *algo, char hdr[MAX_HEADER_LEN]; int hdrlen = sizeof(hdr); - write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen); + write_object_file_prepare_literally(algo, buf, len, type, oid, hdr, &hdrlen); } void hash_object_file(const struct git_hash_algo *algo, const void *buf, @@ -2029,7 +2048,7 @@ int write_object_file_flags(const void *buf, unsigned long len, /* Normally if we have it in the pack then we do not bother writing * it out into .git/objects/??/?{38} file. */ - write_object_file_prepare(the_hash_algo, buf, len, type_name(type), oid, hdr, + write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr, &hdrlen); if (freshen_packed_object(oid) || freshen_loose_object(oid)) return 0; @@ -2046,8 +2065,8 @@ int write_object_file_literally(const void *buf, unsigned long len, /* type string, SP, %lu of the length plus NUL must fit this */ hdrlen = strlen(type) + MAX_HEADER_LEN; header = xmalloc(hdrlen); - write_object_file_prepare(the_hash_algo, buf, len, type, oid, header, - &hdrlen); + write_object_file_prepare_literally(the_hash_algo, buf, len, type, + oid, header, &hdrlen); if (!(flags & HASH_WRITE_OBJECT)) goto cleanup; From patchwork Fri Feb 4 13:51:25 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: 12735105 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 B8B90C433F5 for ; Fri, 4 Feb 2022 13:52:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359160AbiBDNwQ (ORCPT ); Fri, 4 Feb 2022 08:52:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359167AbiBDNvw (ORCPT ); Fri, 4 Feb 2022 08:51:52 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD285C06175B for ; Fri, 4 Feb 2022 05:51:42 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id r17so2405243wrr.6 for ; Fri, 04 Feb 2022 05:51:42 -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=JOimjhwmoTcmb2V7uJh0NmPA7fHISSl36Xxa56pE7pg=; b=hztoMRprPv+cozEJmILCqXKXXrIh6bKuWHUdX9g/3N+7DwmcnABgIzpTPfM/3ueO5H 6Fbm7yYNKjtmsfenvn+qPtXPxb34aXbdTRwF31pVBQQBraZ4/i4iZcwWcNupDKjKuCEx H/akAhriA7NmwyyH53G4pfFtZQGA0C5NOMoVdT+R486EMeeqzs5zo6pDvdUK3XzQsp9X gdcye0zSmnvrZ7M82oDLeIzAE2dbcktf78dUQyTYUjqnV+PSz5SSI6bH9aW9r9CdKhtp Pa/hvr4tFAC7AuOGcTo++hDe0ywvgY36Py2DOaprrLuKpT4SKLMFedXj9ZXgU7MTbxla UWZQ== 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=JOimjhwmoTcmb2V7uJh0NmPA7fHISSl36Xxa56pE7pg=; b=7lIslbOF0Li7gdrapQEeNViBbfrYkhM1PgYvyL6dF11fNAufsTaGs0uswfyDqAoAhL xT2mtp6ZLaPGhFLkxszcHEowyIA5cUXNOSCUdc38pFq5PDi2UqcBPz2StQsai5E58Iwt AK2nEsN2J0rU0/Abm2cDn5KjLxoFBjnUKGuunaGonv+H9irxv6jDNPk/57YSaI7zTgg9 9oDbqs1lc0yppj9sj+ytY0TTpGBLtbWOk5sbNGnrfy+bsxveGbDxgyFOne2nd78d4I7G nBGk4i+ZdZnD2RhaMW4siTspHfNqLk38e6OJNjWIAihz9TBh5GeliUVF2jm1sQnwgE8b mlIQ== X-Gm-Message-State: AOAM532dfNebRx838472nyDQvs4f9H5IAWshccVTxPNW7AJRJlGRyPW9 vm7MryK+3l2dHF0Wsa7WtgJ9keFeuzCNaA== X-Google-Smtp-Source: ABdhPJypvtsFh3dmaDc6bv1io8KtclCqiXjqVBUnSI1tVlCle7ssjZbWTVOqyMKo7Thkhug/dBX5eA== X-Received: by 2002:a5d:5262:: with SMTP id l2mr2493987wrc.698.1643982701161; Fri, 04 Feb 2022 05:51:41 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id z2sm205963wmi.22.2022.02.04.05.51.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 05:51:40 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH v2 11/11] object-file API: pass an enum to read_object_with_reference() Date: Fri, 4 Feb 2022 14:51:25 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the read_object_with_reference() function to take an "enum object_type". It was not prepared to handle an arbitrary "const char *type", as it was itself calling type_from_string(). Let's change the only caller that passes in user data to use type_from_string(), and convert the rest to use e.g. "OBJ_TREE" instead of "tree_type". The "cat-file" caller is not on the codepath that handles"--allow-unknown", so the type_from_string() there is safe. Its use of type_from_string() doesn't functionally differ from that of the pre-image. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/cat-file.c | 11 +++++++---- builtin/fast-import.c | 6 +++--- builtin/grep.c | 4 ++-- builtin/pack-objects.c | 2 +- cache.h | 2 +- object-file.c | 5 ++--- tree-walk.c | 6 +++--- 7 files changed, 19 insertions(+), 17 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index d94050e6c18..3c5bc505e0a 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -154,7 +154,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, break; case 0: - if (type_from_string(exp_type) == OBJ_BLOB) { + { + enum object_type exp_type_id = type_from_string(exp_type); + + if (exp_type_id == OBJ_BLOB) { struct object_id blob_oid; if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) { char *buffer = read_object_file(&oid, &type, @@ -176,10 +179,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, * fall-back to the usual case. */ } - buf = read_object_with_reference(the_repository, - &oid, exp_type, &size, NULL); + buf = read_object_with_reference(the_repository, &oid, + exp_type_id, &size, NULL); break; - + } default: die("git cat-file: unknown option: %s", exp_type); } diff --git a/builtin/fast-import.c b/builtin/fast-import.c index 123df7d9a53..c52e807f56e 100644 --- a/builtin/fast-import.c +++ b/builtin/fast-import.c @@ -2483,7 +2483,7 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa unsigned long size; char *buf = read_object_with_reference(the_repository, &commit_oid, - commit_type, &size, + OBJ_COMMIT, &size, &commit_oid); if (!buf || size < the_hash_algo->hexsz + 6) die("Not a valid commit: %s", p); @@ -2555,7 +2555,7 @@ static void parse_from_existing(struct branch *b) char *buf; buf = read_object_with_reference(the_repository, - &b->oid, commit_type, &size, + &b->oid, OBJ_COMMIT, &size, &b->oid); parse_from_commit(b, buf, size); free(buf); @@ -2651,7 +2651,7 @@ static struct hash_list *parse_merge(unsigned int *count) unsigned long size; char *buf = read_object_with_reference(the_repository, &n->oid, - commit_type, + OBJ_COMMIT, &size, &n->oid); if (!buf || size < the_hash_algo->hexsz + 6) die("Not a valid commit: %s", from); diff --git a/builtin/grep.c b/builtin/grep.c index 9e34a820ad4..75e07b5623a 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -482,7 +482,7 @@ static int grep_submodule(struct grep_opt *opt, object_type = oid_object_info(subrepo, oid, NULL); obj_read_unlock(); data = read_object_with_reference(subrepo, - oid, tree_type, + oid, OBJ_TREE, &size, NULL); if (!data) die(_("unable to read tree (%s)"), oid_to_hex(oid)); @@ -651,7 +651,7 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec, int hit, len; data = read_object_with_reference(opt->repo, - &obj->oid, tree_type, + &obj->oid, OBJ_TREE, &size, NULL); if (!data) die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid)); diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c index ba2006f2212..c4df3df3141 100644 --- a/builtin/pack-objects.c +++ b/builtin/pack-objects.c @@ -1802,7 +1802,7 @@ static void add_preferred_base(struct object_id *oid) return; data = read_object_with_reference(the_repository, oid, - tree_type, &size, &tree_oid); + OBJ_TREE, &size, &tree_oid); if (!data) return; diff --git a/cache.h b/cache.h index 42761bb12cb..357f4cb642f 100644 --- a/cache.h +++ b/cache.h @@ -1561,7 +1561,7 @@ int cache_name_stage_compare(const char *name1, int len1, int stage1, const char void *read_object_with_reference(struct repository *r, const struct object_id *oid, - const char *required_type, + enum object_type required_type, unsigned long *size, struct object_id *oid_ret); diff --git a/object-file.c b/object-file.c index cba33b25197..5c9525479c2 100644 --- a/object-file.c +++ b/object-file.c @@ -1737,16 +1737,15 @@ void *read_object_file_extended(struct repository *r, void *read_object_with_reference(struct repository *r, const struct object_id *oid, - const char *required_type_name, + enum object_type required_type, unsigned long *size, struct object_id *actual_oid_return) { - enum object_type type, required_type; + enum object_type type; void *buffer; unsigned long isize; struct object_id actual_oid; - required_type = type_from_string(required_type_name); oidcpy(&actual_oid, oid); while (1) { int ref_length = -1; diff --git a/tree-walk.c b/tree-walk.c index 3a94959d64a..506234b4b81 100644 --- a/tree-walk.c +++ b/tree-walk.c @@ -89,7 +89,7 @@ void *fill_tree_descriptor(struct repository *r, void *buf = NULL; if (oid) { - buf = read_object_with_reference(r, oid, tree_type, &size, NULL); + buf = read_object_with_reference(r, oid, OBJ_TREE, &size, NULL); if (!buf) die("unable to read tree %s", oid_to_hex(oid)); } @@ -605,7 +605,7 @@ int get_tree_entry(struct repository *r, unsigned long size; struct object_id root; - tree = read_object_with_reference(r, tree_oid, tree_type, &size, &root); + tree = read_object_with_reference(r, tree_oid, OBJ_TREE, &size, &root); if (!tree) return -1; @@ -677,7 +677,7 @@ enum get_oid_result get_tree_entry_follow_symlinks(struct repository *r, unsigned long size; tree = read_object_with_reference(r, ¤t_tree_oid, - tree_type, &size, + OBJ_TREE, &size, &root); if (!tree) goto done;