From patchwork Wed Jan 8 18:37:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13931458 Received: from mail-qv1-f51.google.com (mail-qv1-f51.google.com [209.85.219.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A08281FFC55 for ; Wed, 8 Jan 2025 18:37:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361481; cv=none; b=fZXfdtT0y0dNwPnG+tdZkdBdt79I78y04cm7kiQGFXKFUnXJbFuWyenyvO3eV8w0cH6bgwX6jQ0lhzT9wF3CI+SnIVIFvMYMeFS26Q/c00WHtNmaSyjqSqv9dwxhtz4GoXzc0L/zpRL4VOHHh/8Xg8V8Z7Vpyb+P8B4y82mdMfo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361481; c=relaxed/simple; bh=vgTuS8o56AKp5VzJUizsWjJ9OmeEG1Tdy/sYeNbaRME=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oK6F36hKm7tnASIWZZsbKOyZY+Dha0MScWqjAUfv7MbVskFbXAC+XUFhtoGuKScMre9+0xPhOZoj+5SrzpYZ5Yw+1dz91mYs0E+aWwyLa28IvUld82i8rMeP9en/rILsqVCRxzkQlDBfp0lhTUL3Wef2O233hiuG3jrzOdWEAz0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cpRCiDNi; arc=none smtp.client-ip=209.85.219.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cpRCiDNi" Received: by mail-qv1-f51.google.com with SMTP id 6a1803df08f44-6d8e773ad77so933356d6.2 for ; Wed, 08 Jan 2025 10:37:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736361478; x=1736966278; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZQm4MKMfp3Qk8eTKP3e/CJATv1QEIOwm5F3grdNGwVw=; b=cpRCiDNiClfFg5lnDLxPi+jn7X5ixEqBZlHa5If+BXZVEZbyoVxU//8YFp2PzHMdwr 4UjVnvkODjWpbC5oDOswQqAjT8zeBlZJTDOHgVgLtdcstOAqSaZb1Uq6L5sic7aVKJwJ X3COl7f0Sr0Stn/sYPdz7+p/YQcuOoz/eyfxSa51f9raZBMvJsxdUKH/UlwxMgI0xMah TYjTsWDw0lQvcAygOtaUp+Bg2Qm3pyyrdWKjFTHGD6zVmMtJm2YejG1tWOrZU5dHCopi RQmmEY8Hl78/kDZIoB8mtp0WWc2EH4uZkOglF7hPWeoF2Zkj7u9F6KU5r5LT1DQ6pI07 VLNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736361478; x=1736966278; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZQm4MKMfp3Qk8eTKP3e/CJATv1QEIOwm5F3grdNGwVw=; b=Ci3caOfsMhEsZtjg2KLYCB/6E5SDvRTr0tWBFJn+ujCAt9fcMa6iiLxpUcbKOFPNYa xco2tsnYuIrJjQ0+CrlPXj9oQJISp5hR2pwV3gAY4MSAXvsMay2DajJL0wkHQZ9MDqE6 1oUV5cnl5OJXhgCRjEpvOV7R3JE9gOE6FZI4rnt3elygGVe9V1njaFtFR6jCSv7zj/rt NuvuJDBvpYwmUqFtZIh14ck5KAjjoEFw8lGTu6mAqffMp5GfucCFKCjRxUlgXHahpQ/L EiEP/a6m9gP8v1uJOtU+/BGRBUGdSzyrhb+3RW6/Z6xOjd981jO7pnAL1ompMRmjk0RT 0Jcg== X-Gm-Message-State: AOJu0YwHTCBCcLdCRO3JPgXxVRfme6O0EXCiFPygc2A7GH8w1wRaFpSy uBnGAeM/ExftMBiFbwPN9uEs9tzXBrPLao1r1kFyYcmb7I/IuU/KhpJMD6zKPGI= X-Gm-Gg: ASbGncueeCEtMPUwqDYwEfDvyK3fJ+6eYd7Xxda4IuAOLdrthCPU2buVe/FGsXMTn4d 9hMqXHOOZK9ue/yFpwAqSnXekcqr+sqmmD27BNtFukVeK5bGU4cnpPxadqeDmneQek2QeplNrLj 7wo9FtKvo00IMeZxLSBcT6cHNaKBwvZ1PiIC+dET9WYQIt7KO0fX3KL7JpiFYjow13HTsqW5pfx ZOKL/WWAsWRWXb0XSxX83Xf/GnK5l50tjUtoWGiKryipCg8fenDKcN6qwU5wn/Vkm31EwxER1bD mIWHgfaN2Q== X-Google-Smtp-Source: AGHT+IGDY3puPNEGfSxdv8k5uGznUhYnrB/0ryaj/OxHGFISDvUPywOy5Z0PWQNIdlbi1bx3b7QcoA== X-Received: by 2002:a05:6214:27cc:b0:6d8:9f61:de8c with SMTP id 6a1803df08f44-6df9b24eaeemr63811126d6.27.1736361478391; Wed, 08 Jan 2025 10:37:58 -0800 (PST) Received: from localhost.localdomain ([184.148.194.219]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6dd180ea74bsm193228036d6.25.2025.01.08.10.37.57 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 08 Jan 2025 10:37:58 -0800 (PST) From: Eric Ju To: git@vger.kernel.org Cc: calvinwan@google.com, jonathantanmy@google.com, chriscool@tuxfamily.org, eric.peijian@gmail.com, karthik.188@gmail.com, toon@iotcl.com, jltobler@gmail.com Subject: [PATCH v9 1/8] git-compat-util: add strtoul_ul() with error handling Date: Wed, 8 Jan 2025 13:37:32 -0500 Message-ID: <20250108183740.67022-2-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250108183740.67022-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250108183740.67022-1-eric.peijian@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 We already have strtoul_ui() and similar functions that provide proper error handling using strtoul from the standard library. However, there isn't currently a variant that returns an unsigned long. This commit introduces strtoul_ul() to address this gap, enabling the return of an unsigned long with proper error handling. --- git-compat-util.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/git-compat-util.h b/git-compat-util.h index e283c46c6f..3bdb085624 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1351,6 +1351,24 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result) return 0; } +// Converts a string to an unsigned long using the standard library's strtoul, +// with additional error handling to ensure robustness. +static inline int strtoul_ul(char const *s, int base, unsigned long *result) +{ + unsigned long ul; + char *p; + + errno = 0; + /* negative values would be accepted by strtoul */ + if (strchr(s, '-')) + return -1; + ul = strtoul(s, &p, base); + if (errno || *p || p == s ) + return -1; + *result = ul; + return 0; +} + static inline int strtol_i(char const *s, int base, int *result) { long ul; From patchwork Wed Jan 8 18:37:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13931459 Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9073B2594BE for ; Wed, 8 Jan 2025 18:38:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361482; cv=none; b=slF11RIfYqOX61Nk3LnSPQKnMYgwTYF0MqKG/UzbFxUy2+u5/d8e7x01g5hRM0Z5atRP25YBn9DA4lQ90Sp2lzXJedCzO9bR+mDN7SstrjUwj1im/D5g4bF075FmGLuhltWupK0QtYxV92rYy/4HT3c+3h4y+lDIxMRFBhG5Ay4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361482; c=relaxed/simple; bh=xVmXQsTEZGelVYVwoQ0smDbmLEyqsmDsmPQVmVysR/A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Twvyr0dPog69+AKu7j2xjswWwm4g/0Sje74P9eqpNtmk9vGIwpbp7CTaC/7Ck+YZuWjDk6BhbQ3e5KoVqaACkuxLH7YTFd3XMDYEfQ/HYL/2OfdWWCpRcw/nODNDUJ9t3YptOdlW7TZ6Tg1jJ/rxRfdpHdRXh5r43z4Ec8nxiKg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=WpD9DSG/; arc=none smtp.client-ip=209.85.222.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WpD9DSG/" Received: by mail-qk1-f173.google.com with SMTP id af79cd13be357-7b6f7d7e128so103324985a.1 for ; Wed, 08 Jan 2025 10:38:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736361479; x=1736966279; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JY57OZwsj0U2BlrFEaC+IbbqHQ5k77T5ok4hCgHPWH4=; b=WpD9DSG/+I9avMlARSZJmSxC5wyUJ4TWtzLNiAVKfk6RgbXD2ISyegmDX/xmrUc8QN QRhDS+VoVjd8wjBQAgL2No7zmySTCfHCSRKfp+ID5dVgqAYgYE2j8kpAFOBm0e+93Eor xP+5O3TgHJfoVqNgGjpaL9p6Mz8Y+rilyc1LKaji2CZ9vnrXqwa2WALr+EDtYNmOw0ys U2g7ziwhGTEl8kdTvLY473Yy8d65/9qeJddP5kR0ndFP8BUrkunfxGAEt9Ypnu9JNoys E54s/RI+m65gd6rjAK+V3KCgrXYyNd0aHsuE381or2bkncDSGW8pbWEHU/ht8S1nRp5J vHzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736361479; x=1736966279; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JY57OZwsj0U2BlrFEaC+IbbqHQ5k77T5ok4hCgHPWH4=; b=PmoO0swOxwzYbXAUC76eSRngZYn7YJ974HeGfoKl9UHprgJGt5bGAn7kf9z3fHC8N0 l4mlSPymWTMAxzj+x5+483CqM0ywjUOqNG7dmHGf3MapK/pDb8KyVomdMGnko0FwoSq+ kx3MzWtfSOQ6KIdfvmaFIdtqW9Zd/h67dU/QavG8B9FgUQAwn5Mx95eYLrBTahZO+WdN NjGRLNfnnE0pd2AKDDUC3Ee73gyCWHWOvquQyAvHrkRIcES46E5NinlXq9nweqtlpyj8 vyiVTrhN1f1SL/s9zSecwHg1ydsq3Nqxcxt6ghL1EXyUjc4SiqwHFSQigZpjD1/8yMjX RzKA== X-Gm-Message-State: AOJu0Yw1dTSgF1hMBAYCyXY6UKAt8zpTn96lQtQTbUn1zfyEyPad0Ckm cL9mDGSMd8GjCWHO2e/d5YERQwQLBhYdz0YMybbUKRr2lbBDJBAdMBRtFhdRM6I= X-Gm-Gg: ASbGncsRFMCiXPlq/nSvPP2BSYr0klYlxW8nkOkLDDrBoCLj3+MwGwJohu/Gd6a0I/h adeGGJm30bItOiJK5CyFzWEDPZvhWs0FG6ZmI3yPo6RP1+Uwo6wwMs/anhlL+i0D6QwyXmQ1CBo O7lGw7AHI8sK9+6UhG4y8JL4ublnorGXalX8sxEZH8PGWU88Xv/5Argy8UiuJcbO6/8KhEKPgGI Ws+E+6zmS6esBwQaRnqZT4yHflpGKqs5IBWf3kwOSMMdMP3fv3B9HEAzQnzgHOZ0V2Z5t5DrAHf U1g32bMVRg== X-Google-Smtp-Source: AGHT+IFbxR5OXNc/uLIPFuRYBvkcxZDAolP6kWC4V+rOvZzlWznBowG3e1Gslm0UmXC7HMQ24W/g0w== X-Received: by 2002:a05:620a:2614:b0:7b6:dd22:31f0 with SMTP id af79cd13be357-7bce0f7c846mr79029185a.0.1736361479314; Wed, 08 Jan 2025 10:37:59 -0800 (PST) Received: from localhost.localdomain ([184.148.194.219]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6dd180ea74bsm193228036d6.25.2025.01.08.10.37.58 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 08 Jan 2025 10:37:59 -0800 (PST) From: Eric Ju To: git@vger.kernel.org Cc: calvinwan@google.com, jonathantanmy@google.com, chriscool@tuxfamily.org, eric.peijian@gmail.com, karthik.188@gmail.com, toon@iotcl.com, jltobler@gmail.com Subject: [PATCH v9 2/8] cat-file: add declaration of variable i inside its for loop Date: Wed, 8 Jan 2025 13:37:33 -0500 Message-ID: <20250108183740.67022-3-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250108183740.67022-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250108183740.67022-1-eric.peijian@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Some code used in this series declares variable i and only uses it in a for loop, not in any other logic outside the loop. Change the declaration of i to be inside the for loop for readability. Helped-by: Christian Couder Signed-off-by: Eric Ju --- builtin/cat-file.c | 11 +++-------- fetch-pack.c | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index b13561cf73..69ea642dc6 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -676,12 +676,10 @@ static void dispatch_calls(struct batch_options *opt, struct queued_cmd *cmd, int nr) { - int i; - if (!opt->buffer_output) die(_("flush is only for --buffer mode")); - for (i = 0; i < nr; i++) + for (size_t i = 0; i < nr; i++) cmd[i].fn(opt, cmd[i].line, output, data); fflush(stdout); @@ -689,9 +687,7 @@ static void dispatch_calls(struct batch_options *opt, static void free_cmds(struct queued_cmd *cmd, size_t *nr) { - size_t i; - - for (i = 0; i < *nr; i++) + for (size_t i = 0; i < *nr; i++) FREE_AND_NULL(cmd[i].line); *nr = 0; @@ -717,7 +713,6 @@ static void batch_objects_command(struct batch_options *opt, size_t alloc = 0, nr = 0; while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) { - int i; const struct parse_cmd *cmd = NULL; const char *p = NULL, *cmd_end; struct queued_cmd call = {0}; @@ -727,7 +722,7 @@ static void batch_objects_command(struct batch_options *opt, if (isspace(*input.buf)) die(_("whitespace before command: '%s'"), input.buf); - for (i = 0; i < ARRAY_SIZE(commands); i++) { + for (size_t i = 0; i < ARRAY_SIZE(commands); i++) { if (!skip_prefix(input.buf, commands[i].name, &cmd_end)) continue; diff --git a/fetch-pack.c b/fetch-pack.c index 3a227721ed..f5a63f12cd 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -1329,9 +1329,8 @@ static void write_fetch_command_and_capabilities(struct strbuf *req_buf, if (advertise_sid && server_supports_v2("session-id")) packet_buf_write(req_buf, "session-id=%s", trace2_session_id()); if (server_options && server_options->nr) { - int i; ensure_server_supports_v2("server-option"); - for (i = 0; i < server_options->nr; i++) + for (size_t i = 0; i < server_options->nr; i++) packet_buf_write(req_buf, "server-option=%s", server_options->items[i].string); } From patchwork Wed Jan 8 18:37:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13931460 Received: from mail-qv1-f46.google.com (mail-qv1-f46.google.com [209.85.219.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7BF0E201259 for ; Wed, 8 Jan 2025 18:38:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361483; cv=none; b=YsXJzJAsPPXgtdPXqKXxOqDXMfq+Hj4qfuI21hZ9ZfqgJQcYqoRBQzv+KV+yKwTD9Pp9p3N4f75NQFQn17f1FofaOFcy+wIcLoh8vmvFdhFykhSMPr0o9iay5J6c5k5NFDahNuG0VeXBe8/mQpyUmjJazdDkBTPPnwY3hAkpeQ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361483; c=relaxed/simple; bh=015UkzzgBVeqRa8x5NRYEtBCc9wEI939Rdn0MoJ7X3Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KgY9c3v0m6o4q2JmhNXo/r6k4M2IrIRRHTQHRl91mxdkwrOiTkLma+ag196edKble+BIvyUP9gqkvNqsdhpnImE3wCrbv+LZKHBaDQEhHwjc1cf2rKodqT/Sc1n81mjDNAlSHgSFlhb+jzlfpc5K6PqLI6R0i62NyaquzOBKGa4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kRFarm/r; arc=none smtp.client-ip=209.85.219.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kRFarm/r" Received: by mail-qv1-f46.google.com with SMTP id 6a1803df08f44-6dd43aa1558so1635646d6.0 for ; Wed, 08 Jan 2025 10:38:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736361480; x=1736966280; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DMSfX9lzqqlF66vCHXp6Kv9pQ2HeL1c1zZ7Uoud4Myo=; b=kRFarm/rNUgsY2LvYUdPfYNmbYKyWtDLtvaqrJlbnmT9wn86JEl8C8gH1cLDQ9x8lK /wRyVa1s77TYbZS+5om3+Zk9s09tk9s9nl0vfj6DToAYdhOhRNK3558Y2vXTX0yYn2W5 D2TNZ18aNPn21qVs9eGyp+Dy1RNo4JbcUjwLNBHZcCTfQNJzsYY2mqdX2N/nYINid4Ca MSmmQI7f7KsrmTJgJKcbZ9DkcqefYQ5/c0hP1nCibVyK3bN4RHGtZmP+IdilYzs41H8y z4XJGzmuPvQTulVBhT+ReAv9gaRMu5TzMVoIkIHymLQ2TdYZen/OWGDFroPlklSDbTLX JnLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736361480; x=1736966280; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DMSfX9lzqqlF66vCHXp6Kv9pQ2HeL1c1zZ7Uoud4Myo=; b=RLDuTqXuz2JWGKCOb+nMIVQY3G4z/ARnFe9Nz7XQ9SN8L1/UwV+Z4E9No7UM/oLSNy IvXs0wLHkMUNrEOU6mnQXdnXOChTUNS6s7KQ2FWl32MgxKS6f0OjiKXcnJRusHqcwjdQ NF3HsOgOyYzqHot0ZUeL8y3YABJZ4KBPfvODIyIhXHBd1R3ByQJTXfZ4KynN6Dy6covC 7K4Llzt8cPnSgmz4z4lb2yDdLfg1VobT43XQns9fuJc+klbK7giezEolbZhA6zXI0wD5 /MGcUNGaslqbMrhQeNlkBO8+btCbYN3ZI4RFX2ZLkoj8R1hUjdlKjm8QpUmnO6UDJOxR DLmA== X-Gm-Message-State: AOJu0YxFB6bfDivevfjZS4R1w/baTUJy2iAk0GY/ZygpUNJwOVMbcvTh uDHlIW9g0Z2JwValhzv6GomVdlBgVatWMTieYHpeN8C3jAHiqDrIcedUjaNcso8= X-Gm-Gg: ASbGncvoX/Rs/Pc9j6YMDnHG86mZco2JGMqGh3bvEgt51wqoAObEictqrFUQhGBvgBo xTcYBZ/U35GIhTgonpJxk5OVt6Oq9V96qK3361+Gc2r6h9D1vFOTEGg06NB5TUgaIowJWbFVctj mm7UgXYRU2GAvYrpMb9O/Nw2aWr6E8tbPxrwvaN9uOlkDldoQkPoMm62aCabA/QHN+PVYy+PbT6 7dN3FPr6WCDzS+ts4r/31yh+uoBDORdHuDRljkTcRVSY8cDauacFjwFKmaWr8raH0rnJlgTziTn gPpoWIwj2g== X-Google-Smtp-Source: AGHT+IFeSOjdFDTnnfwFg49RJCtSBq7De3q7H3HUnw65iZwZUrYZ/QGORZlb6+GW7z10c19VGBf5WA== X-Received: by 2002:a05:6214:dc4:b0:6d3:fa03:23f1 with SMTP id 6a1803df08f44-6df9b1ef368mr70147266d6.13.1736361480346; Wed, 08 Jan 2025 10:38:00 -0800 (PST) Received: from localhost.localdomain ([184.148.194.219]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6dd180ea74bsm193228036d6.25.2025.01.08.10.37.59 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 08 Jan 2025 10:38:00 -0800 (PST) From: Eric Ju To: git@vger.kernel.org Cc: calvinwan@google.com, jonathantanmy@google.com, chriscool@tuxfamily.org, eric.peijian@gmail.com, karthik.188@gmail.com, toon@iotcl.com, jltobler@gmail.com Subject: [PATCH v9 3/8] cat-file: split test utility functions into a separate library file Date: Wed, 8 Jan 2025 13:37:34 -0500 Message-ID: <20250108183740.67022-4-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250108183740.67022-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250108183740.67022-1-eric.peijian@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This refactor extracts utility functions from the cat-file's test t1006-cat-file.sh into a dedicated library file. The goal is to improve code reuse and readability, enabling future tests to leverage these utilities without duplicating code --- t/lib-cat-file.sh | 16 ++++++++++++++++ t/t1006-cat-file.sh | 13 +------------ 2 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 t/lib-cat-file.sh diff --git a/t/lib-cat-file.sh b/t/lib-cat-file.sh new file mode 100644 index 0000000000..9fb20be308 --- /dev/null +++ b/t/lib-cat-file.sh @@ -0,0 +1,16 @@ +# Library of git-cat-file related tests. + +# Print a string without a trailing newline +echo_without_newline () { + printf '%s' "$*" +} + +# Print a string without newlines and replaces them with a NULL character (\0). +echo_without_newline_nul () { + echo_without_newline "$@" | tr '\n' '\0' +} + +# Calculate the length of a string removing any leading spaces. +strlen () { + echo_without_newline "$1" | wc -c | sed -e 's/^ *//' +} diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index ff9bf213aa..5c7d581ea2 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -3,6 +3,7 @@ test_description='git cat-file' . ./test-lib.sh +. "$TEST_DIRECTORY"/lib-cat-file.sh test_cmdmode_usage () { test_expect_code 129 "$@" 2>err && @@ -98,18 +99,6 @@ do ' done -echo_without_newline () { - printf '%s' "$*" -} - -echo_without_newline_nul () { - echo_without_newline "$@" | tr '\n' '\0' -} - -strlen () { - echo_without_newline "$1" | wc -c | sed -e 's/^ *//' -} - run_tests () { type=$1 oid=$2 From patchwork Wed Jan 8 18:37:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13931461 Received: from mail-qt1-f176.google.com (mail-qt1-f176.google.com [209.85.160.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 93D51202C43 for ; Wed, 8 Jan 2025 18:38:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361484; cv=none; b=EqsJh7SBqsddNW/hg1t4bUaa6tdAfhumITDaagQz1mfAq1K/d5W6OSqXB800khRPAp5bi6PJHGo8hBnXk7gsa/aaj8Ejt9+dxRuI4B3CqORvgi3ZFiLdpeeqB311dsYpw29OiVUfC7H1hvJ3YdOFkSrUcgeGpzQVsTQaIDABSXo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361484; c=relaxed/simple; bh=U9/DyBi+NftKREWco74mHRab48WeC/WAjVhbOLWAFek=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JtCOSKD13UUw0hJDkwytSESaZ8j0lg8Skb13Tt2RDwjGgDoMz7I3a8OwbTeCbLTHoCiA+X1zLXQxMX5S8u5qTNzBH2Dq/zQu2jqbLbooz5bw+ye3l6XNKCF3m9WNJne6TBDYeK3qj+g/CE7JhNAgLNBpBhqHKzCa5GMIpBj664U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mtNt24NA; arc=none smtp.client-ip=209.85.160.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mtNt24NA" Received: by mail-qt1-f176.google.com with SMTP id d75a77b69052e-467a37a2a53so189021cf.2 for ; Wed, 08 Jan 2025 10:38:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736361481; x=1736966281; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=taJPcW8QU2LxZOovWt2m4TysQWnS8cJZw2EpQbtPYEU=; b=mtNt24NAmOmNopTfjJLP6PUZy2+wEhpwVebJ/2ueDi791KjvGTmcd5hLLfFvQ3fxOB t8qNQ4MHMZRAHDD+dg9RBox8g3oZ2fC72IiPdCzfgJZMK933P6LSSwrk2pI4vlGj/1dH xlBsUe594vTHQuet8+lrROr5xpR7D4/uUqd7wEEYuAQeCz10kQvjD5NrxwBHPKmYAzqp mNlcyswdg0rXs0LTjyfPpxblu7EJMpSKRLJf5Yq8iS7fT43P6NXcOXSwiORDi2px1p4s WRzrozivNHoQy0nzgvTIANQk34ft2e6zOmXw35msv0BsxEeRS68t0iQm4tqWofjdN/QM HCxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736361481; x=1736966281; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=taJPcW8QU2LxZOovWt2m4TysQWnS8cJZw2EpQbtPYEU=; b=oq8mpIiUOCnVBbVXDIFTGp1yDCTzdhCQEI6vRpsthpavPP+n5KSA4O9buY8aDegFFT 3Qqdw44uFJeYtwtVmh+4JpL8jtN7vm7lRHf5nsO4oWU1maJ+Rr5y43K7LN6ycn5c3pcs W1zMmcXG9YDM2g5k0xvm45kHKToJJdwLoTloiAOlIM/IHJs0EwpHqkaY1WwjaKqUlGrj 0fy5OaSQPJmDXCcw8afbC2SD5mfwnwdNBjFyIHxJDIfYsjKbaUZSlIIoYiIo1f8elCQs JSnHUw1tY+AjS3zg84nFbj9TU+ZrXXdQN7eSaNKskEDdDlpRuAtK7MvQRuB18Yby3ViA r48w== X-Gm-Message-State: AOJu0YxvH2E/wrg/03Hx3XQ2nMQp114S2BupExhQQQB3HWM+2/G3kJLO f63Or3Jpi2lCCeq3qfgIg2NdvWgbbWS9koz8gB/G/OAvEatU3g/HBGUkdxpd5Zs= X-Gm-Gg: ASbGncvhXMbTcPH9rnodMeNSklZ8DsSlPnqAZdru5GKqjIw9HsDPu3iGU9uaDIMxtUh UHbG57CUoUfOdEvNy8B1Xy/DEojZSetrAhL01DPe8qUXnDSjRECXlkwS81jbJpBXBxrpNbXnZNB YLZyY07VRU9oY4iIL8IfPxGzEDbsYEUVxiIgsl5fd74wHeIWYnXP5q2VjkO6A016VAxSeqhhQ0k hd89osnFpjV7Uz2zzwIzPVyC4n2exwHxtO0G3tMN7zmxeLZmL4jzD1jb4mYm/2WQdGrAvukpEGs 6kLplOHcug== X-Google-Smtp-Source: AGHT+IFBpXbbDlnZUysVsxTAHMkPbuNgAsxABkvujN3k7fHJxyo9RpQe3wZjXOYrVwQ4XD9KTNIlYg== X-Received: by 2002:a05:6214:e6a:b0:6d8:9872:adc1 with SMTP id 6a1803df08f44-6df9b2d0aa1mr57818306d6.38.1736361481254; Wed, 08 Jan 2025 10:38:01 -0800 (PST) Received: from localhost.localdomain ([184.148.194.219]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6dd180ea74bsm193228036d6.25.2025.01.08.10.38.00 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 08 Jan 2025 10:38:01 -0800 (PST) From: Eric Ju To: git@vger.kernel.org Cc: calvinwan@google.com, jonathantanmy@google.com, chriscool@tuxfamily.org, eric.peijian@gmail.com, karthik.188@gmail.com, toon@iotcl.com, jltobler@gmail.com Subject: [PATCH v9 4/8] fetch-pack: refactor packet writing Date: Wed, 8 Jan 2025 13:37:35 -0500 Message-ID: <20250108183740.67022-5-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250108183740.67022-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250108183740.67022-1-eric.peijian@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Calvin Wan Refactor write_fetch_command_and_capabilities() to a more general-purpose function, write_command_and_capabilities(), enabling it to serve both fetch and additional commands. In this context, "command" refers to the "operations" supported by Git's wire protocol https://git-scm.com/docs/protocol-v2, such as a Git subcommand (e.g., git-fetch(1)) or a server-side operation like "object-info" as implemented in commit a2ba162c (object-info: support for retrieving object info, 2021-04-20). Furthermore, write_command_and_capabilities() is moved to connect.c, making it accessible to additional commands in the future. To move write_command_and_capabilities() to connect.c, we need to adjust how `advertise_sid` is managed. Previously, in fetch_pack.c, `advertise_sid` was a static variable, modified using git_config_get_bool(). In connect.c, we now initialize `advertise_sid` at the beginning by directly using git_config_get_bool(). This change is safe because: In the original fetch-pack.c code, there are only two places that write `advertise_sid` : 1. In function do_fetch_pack: if (!server_supports("session-id")) advertise_sid = 0; 2. In function fetch_pack_config(): git_config_get_bool("transfer.advertisesid", &advertise_sid); About 1, since do_fetch_pack() is only relevant for protocol v1, this assignment can be ignored in our refactor, as write_command_and_capabilities() is only used in protocol v2. About 2, git_config_get_bool() is from config.h and it is an out-of-box dependency of connect.c, so we can reuse it directly. Helped-by: Jonathan Tan Helped-by: Christian Couder Signed-off-by: Calvin Wan Signed-off-by: Eric Ju --- connect.c | 34 ++++++++++++++++++++++++++++++++++ connect.h | 8 ++++++++ fetch-pack.c | 35 ++--------------------------------- 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/connect.c b/connect.c index 10fad43e98..d89591f043 100644 --- a/connect.c +++ b/connect.c @@ -689,6 +689,40 @@ int server_supports(const char *feature) return !!server_feature_value(feature, NULL); } +void write_command_and_capabilities(struct strbuf *req_buf, const char *command, + const struct string_list *server_options) +{ + const char *hash_name; + int advertise_sid; + + git_config_get_bool("transfer.advertisesid", &advertise_sid); + + ensure_server_supports_v2(command); + packet_buf_write(req_buf, "command=%s", command); + if (server_supports_v2("agent")) + packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized()); + if (advertise_sid && server_supports_v2("session-id")) + packet_buf_write(req_buf, "session-id=%s", trace2_session_id()); + if (server_options && server_options->nr) { + ensure_server_supports_v2("server-option"); + for (size_t i = 0; i < server_options->nr; i++) + packet_buf_write(req_buf, "server-option=%s", + server_options->items[i].string); + } + + if (server_feature_v2("object-format", &hash_name)) { + const int hash_algo = hash_algo_by_name(hash_name); + if (hash_algo_by_ptr(the_hash_algo) != hash_algo) + die(_("mismatched algorithms: client %s; server %s"), + the_hash_algo->name, hash_name); + packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name); + } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) { + die(_("the server does not support algorithm '%s'"), + the_hash_algo->name); + } + packet_buf_delim(req_buf); +} + enum protocol { PROTO_LOCAL = 1, PROTO_FILE, diff --git a/connect.h b/connect.h index 1645126c17..d904c73a85 100644 --- a/connect.h +++ b/connect.h @@ -30,4 +30,12 @@ void check_stateless_delimiter(int stateless_rpc, struct packet_reader *reader, const char *error); +/* + * Writes a command along with the requested + * server capabilities/features into a request buffer. + */ +struct string_list; +void write_command_and_capabilities(struct strbuf *req_buf, const char *command, + const struct string_list *server_options); + #endif diff --git a/fetch-pack.c b/fetch-pack.c index f5a63f12cd..78e7d38c47 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -1317,37 +1317,6 @@ static int add_haves(struct fetch_negotiator *negotiator, return haves_added; } -static void write_fetch_command_and_capabilities(struct strbuf *req_buf, - const struct string_list *server_options) -{ - const char *hash_name; - - ensure_server_supports_v2("fetch"); - packet_buf_write(req_buf, "command=fetch"); - if (server_supports_v2("agent")) - packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized()); - if (advertise_sid && server_supports_v2("session-id")) - packet_buf_write(req_buf, "session-id=%s", trace2_session_id()); - if (server_options && server_options->nr) { - ensure_server_supports_v2("server-option"); - for (size_t i = 0; i < server_options->nr; i++) - packet_buf_write(req_buf, "server-option=%s", - server_options->items[i].string); - } - - if (server_feature_v2("object-format", &hash_name)) { - int hash_algo = hash_algo_by_name(hash_name); - if (hash_algo_by_ptr(the_hash_algo) != hash_algo) - die(_("mismatched algorithms: client %s; server %s"), - the_hash_algo->name, hash_name); - packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name); - } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) { - die(_("the server does not support algorithm '%s'"), - the_hash_algo->name); - } - packet_buf_delim(req_buf); -} - static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, struct fetch_pack_args *args, const struct ref *wants, struct oidset *common, @@ -1358,7 +1327,7 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, int done_sent = 0; struct strbuf req_buf = STRBUF_INIT; - write_fetch_command_and_capabilities(&req_buf, args->server_options); + write_command_and_capabilities(&req_buf, "fetch", args->server_options); if (args->use_thin_pack) packet_buf_write(&req_buf, "thin-pack"); @@ -2186,7 +2155,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_tips, the_repository, "%d", negotiation_round); strbuf_reset(&req_buf); - write_fetch_command_and_capabilities(&req_buf, server_options); + write_command_and_capabilities(&req_buf, "fetch", server_options); packet_buf_write(&req_buf, "wait-for-done"); From patchwork Wed Jan 8 18:37:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13931462 Received: from mail-qv1-f50.google.com (mail-qv1-f50.google.com [209.85.219.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42258202C50 for ; Wed, 8 Jan 2025 18:38:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361485; cv=none; b=KxphfuK9H7gBCR0DUzKGknTvaYuCCvbaBPB6TgQiTTLt0IpWtQPw1Sml82fst2sY71tbh7926J13QrW51+dm4Ys9JWQAceIbBzF2cMjZe0PWXcY9kWeCQHHd5bJ1je51hwbBdfznNBPrZ0lQafNgoiwlINSf5pThVCHrywT8SgI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361485; c=relaxed/simple; bh=6v6kmj6IoJTTVenBvDN0ySpPKhvA1nmYwnT31qsuLH8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AQCWWVOfLJNo426QgHSGrcoNorpYpRkvYsQJ7kDlEP015CLAg703eaEj9Z4LywCid9T3W63KfOoz1j9p3VAB6Z8RiAfLm82JligNIA6Xb1nOES+Zxg3m+odtLRlHC/ZVbm5XFM9v/D+T2Byy3RNykdhfPp4InhoVjhMkViOjTg4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CiDTg47+; arc=none smtp.client-ip=209.85.219.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CiDTg47+" Received: by mail-qv1-f50.google.com with SMTP id 6a1803df08f44-6d8f99cb0d9so1477216d6.0 for ; Wed, 08 Jan 2025 10:38:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736361482; x=1736966282; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BuvuFUMBdM9FyTWZ9IdBTNsQLl3Xz4xTvWGs2nwwYrg=; b=CiDTg47+/ZTE45GQiqrM8TB2I70gCl1AHLuc3E0IfjLtIu+F9HKAqXr8zsn7k4bILw qBZLzfdlHW6BppCza80eYshDyGefQgMJqinE762cd3Kmk3OSvmZWiCeYUbAXbjcj4OA+ Xl6ijPd9On7Fn+4HypQmRf1ADZ3hSfpMRZMWieOVBUTwEkaE2sk/RmEDc7CUUQ2Ia//F LfwJaWBLfQqTzF6Mf0adbxESDGJKqbcS63CUrnZ+3s8n3yMb57cDdKfpFLOg7eUg81ow V1zxgOsMNDZv9FCrMrImf/aBdcr+hSpExoA2KGRzC9iPcGaJfux4mDVImY9k3w5TPLUx tf1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736361482; x=1736966282; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BuvuFUMBdM9FyTWZ9IdBTNsQLl3Xz4xTvWGs2nwwYrg=; b=BEGsducB5eJnmM3g222eMBZBPLW7Qnk+MHQ298MCsj7RZyoXz3+E9DeVqiCQJFJufo jeNwLVA++pi+oiNcDyc7cApUA/oHHWrmh3U9vgjgoSlmZPJLdDF8FmX0CMlEbs+5LMsl E0z4q8kjn3fSdWOLanROJ0lTRJZfdS9nAVPhsddxkofKXRRdiLNyA6oDA3PGNaxNjnDd JXWrow7uUawoivsyIa3cQIcJFg48Uwqzhh3ED2AhEj6WEe/gLUUlgstOPsx1hhfKsSob GML7vDPgsrVmR2LIeTI7210BeAMunCFy0WQB+ZMHNPEhuDTDtszV5aefc95gKv2h/Bzj EV0w== X-Gm-Message-State: AOJu0Yyj2irsxeAHey2JboKMeZhczHu0gvRWre6uJQwsjqyIzal/74jC phT+RUspiAbBlUEPHx/QVOrS3UOEjVJ4ZSJUBktqo3CQD/vbCB6jCu9KmLfnqJk= X-Gm-Gg: ASbGncvo2UqvBGbySnqYDWuuYnKMJZ0l3qIj50DjcE9GsXhTLDJ7k9E16FNF+dTpICq N2eQ5JCimals+CNb6RR8bfUxKXKdSEahnCeLTYI+p3j2MpHzsQ3e2093dVQtCCXeE1upZXmEsgc UAprIdQCyjESz9GdAYfMq+ZX5ZbWGtHqQMjOLSgvGeesRA6tp1INd/wu0aZiihsF02JuF7Lo1sj 4uqvb1yX7JpMWtP8XLJaUiqwSYW1eQzsn+rC8ZZhoqk8SUEjohDO7T1EVP6UMKaX0tKJrAZYtJY rwU0Pvj4jA== X-Google-Smtp-Source: AGHT+IG3TmdGZzGCuFHkQ7HzE6WQjyJsYG80HF4GGjKL1g8ImxxxjrQLu0KlTxRoFLJJPg2xh7TH/A== X-Received: by 2002:a05:6214:4903:b0:6d8:8e27:5a45 with SMTP id 6a1803df08f44-6df9b2ddc3cmr71416526d6.39.1736361482150; Wed, 08 Jan 2025 10:38:02 -0800 (PST) Received: from localhost.localdomain ([184.148.194.219]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6dd180ea74bsm193228036d6.25.2025.01.08.10.38.01 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 08 Jan 2025 10:38:01 -0800 (PST) From: Eric Ju To: git@vger.kernel.org Cc: calvinwan@google.com, jonathantanmy@google.com, chriscool@tuxfamily.org, eric.peijian@gmail.com, karthik.188@gmail.com, toon@iotcl.com, jltobler@gmail.com Subject: [PATCH v9 5/8] fetch-pack: move fetch initialization Date: Wed, 8 Jan 2025 13:37:36 -0500 Message-ID: <20250108183740.67022-6-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250108183740.67022-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250108183740.67022-1-eric.peijian@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Calvin Wan There are some variables initialized at the start of the do_fetch_pack_v2() state machine. Currently, they are initialized in FETCH_CHECK_LOCAL, which is the initial state set at the beginning of the function. However, a subsequent patch will allow for another initial state, while still requiring these initialized variables. Move the initialization to be before the state machine, so that they are set regardless of the initial state. Note that there is no change in behavior, because we're moving code from the beginning of the first state to just before the execution of the state machine. Helped-by: Jonathan Tan Helped-by: Christian Couder Signed-off-by: Calvin Wan Signed-off-by: Eric Ju --- fetch-pack.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fetch-pack.c b/fetch-pack.c index 78e7d38c47..51de82e414 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -1648,18 +1648,18 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args, reader.me = "fetch-pack"; } + /* v2 supports these by default */ + allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; + use_sideband = 2; + if (args->depth > 0 || args->deepen_since || args->deepen_not) + args->deepen = 1; + while (state != FETCH_DONE) { switch (state) { case FETCH_CHECK_LOCAL: sort_ref_list(&ref, ref_compare_name); QSORT(sought, nr_sought, cmp_ref_by_name); - /* v2 supports these by default */ - allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; - use_sideband = 2; - if (args->depth > 0 || args->deepen_since || args->deepen_not) - args->deepen = 1; - /* Filter 'ref' by 'sought' and those that aren't local */ mark_complete_and_common_ref(negotiator, args, &ref); filter_refs(args, &ref, sought, nr_sought); From patchwork Wed Jan 8 18:37:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13931463 Received: from mail-qv1-f47.google.com (mail-qv1-f47.google.com [209.85.219.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 44D2B202F8E for ; Wed, 8 Jan 2025 18:38:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361486; cv=none; b=F9xKYt6n5bxtlait4TP/2eIQ5h93jk47/x/ayks9EX60eTPoCUP7y+3DyEdL1IaGS4XwIoKPGUm1O8TGcSgHugITtNuvynkoj2mG2HPlljhVfr7WkfvipvTDzQsY4oSKByzgJweFLbSjhzjlZzrZBrU1rG9GSbMhTkhIgwhV1So= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361486; c=relaxed/simple; bh=DgBA291+j6ejZVIrtQoPG7txaLVr23L8sLBk2ll5bUw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=euOuTabTd8GwoGTge1Va2qQ1YKmtY+GrfyM3wqL6prUC8KIj9zfrFO0F9qM0PQsERFhQyC/+9UFymna6t5LCmt6jKgb/d3gai+zO5mptTDlma/m7NfpDA5JYkUD15lA3UDuULhnevgJIzcYzideBiaPmL7o0oIQ6uZog8f1h9ZA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IZRhzlQF; arc=none smtp.client-ip=209.85.219.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IZRhzlQF" Received: by mail-qv1-f47.google.com with SMTP id 6a1803df08f44-6d8fd060e27so1239896d6.1 for ; Wed, 08 Jan 2025 10:38:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736361484; x=1736966284; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nq2fqDypuUUjO63eoWLT+d8CeGIKnq0xPOyYXXa21L0=; b=IZRhzlQFb7igf8z/Hi9lKWlU92kKlyGkQGp2tG8h+YlC/HLtkSckciOdGp8zLISfZ4 BANBeD6qBVpLztU+9K/UESUuiKHCTornRUfxq+V0ZThWrKl1aivjKETUlMqpMtLL9I7Q 8U7KCqAGJcNoKaCQKWpdMqGY7QVjpIEdOlq8YRCeME9EVgsU/w9U8aJDUfh5ONPel1zt kNGFIILbwTO9PhoM2IM9AIrDchxBdLZCqxjBxaHZs3r2LchzpOFeUKCj88vFvdAx4MJq v28JKaUYKgoVJ3kXD3mROy51atkudPg/5RCJy03KFVbBdnzex61nCMCHXftujdOjcmpj DSMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736361484; x=1736966284; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nq2fqDypuUUjO63eoWLT+d8CeGIKnq0xPOyYXXa21L0=; b=NPhS8tV9sN9bNoXYqQvG/YeTWrtn7rYcRyUr2C8hIgfpAo4HH3yl3D9dUxucjtHrZx TlqnyuQn61A2iLjbxdExtwNJdbcHrxuqEufp05qQ89Ub9HRMoTdQtc2NZTpW55LdRuA9 3YVZnD3Yg5LbhLxk4w+XQMzQtnMEEKEEYPaO7Od8Hlkuio3UAo3W5IsD5jW7CcsGjWnt 5PC81ddvfWEU32kod22aCm62Izl2f+kXmFiOuvpP/JrnQjf523jqPuiNeLoc7mGa9GPG ocgZYild3+mR6NfVihCJFKZ89TeIcbA5trwD4rsBBK2YCI3FhodZthP0v0GRPyvNgBEA lWbg== X-Gm-Message-State: AOJu0YzZ1dEAgxJdHF6pQ1ZhoWZYFJwe372Z7bZpm+MVLwZR68CqpeFo /ChcAhtm/zX4G08QN0J1H97D7u87Tk4ZHHPtn7cWpr3CckHLANJzBPWsznUHRpo= X-Gm-Gg: ASbGncu9VwiQUqhBstdzALbvdZJ8RnoZUrXpVTwlJ1BNFPhQuA8IvtoIxnba8eii9IS OhkDrA9cfDw9NHxIcTuR8PqBXRpwLC1uSZU+MsLbBpbaFrpTPLznSmTiJ1iKOSRcPOXudE1/d8C EJg1kSIGC83sjwFTsPPjI8F7SDJAuD9B7BmsESbDAaGykWtA9yeLfngSfCKMBJKMjpgrebHafdQ 2ro0iylwPR0pkgUDfBsE7B0rCRkslwrLNvOOflYHGi+LLwU9VQv7uNspnNk1DYCMgqj9e20N3I4 TUneRhwBOQ== X-Google-Smtp-Source: AGHT+IE+kkXqk1TgZL7iW6VajxLG3Exu6hKhellc/pBHXFm3Qa3JaCDt9KUvV0zZc699rdjU2oyhHQ== X-Received: by 2002:ad4:5747:0:b0:6d8:8fdd:9797 with SMTP id 6a1803df08f44-6df9b2b1a16mr66386626d6.36.1736361484137; Wed, 08 Jan 2025 10:38:04 -0800 (PST) Received: from localhost.localdomain ([184.148.194.219]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6dd180ea74bsm193228036d6.25.2025.01.08.10.38.02 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 08 Jan 2025 10:38:03 -0800 (PST) From: Eric Ju To: git@vger.kernel.org Cc: calvinwan@google.com, jonathantanmy@google.com, chriscool@tuxfamily.org, eric.peijian@gmail.com, karthik.188@gmail.com, toon@iotcl.com, jltobler@gmail.com Subject: [PATCH v9 6/8] serve: advertise object-info feature Date: Wed, 8 Jan 2025 13:37:37 -0500 Message-ID: <20250108183740.67022-7-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250108183740.67022-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250108183740.67022-1-eric.peijian@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Calvin Wan In order for a client to know what object-info components a server can provide, advertise supported object-info features. This will allow a client to decide whether to query the server for object-info or fetch as a fallback. Helped-by: Jonathan Tan Helped-by: Christian Couder Signed-off-by: Calvin Wan Signed-off-by: Eric Ju --- serve.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/serve.c b/serve.c index c8694e3751..7a388d26d9 100644 --- a/serve.c +++ b/serve.c @@ -70,7 +70,7 @@ static void session_id_receive(struct repository *r UNUSED, trace2_data_string("transfer", NULL, "client-sid", client_sid); } -static int object_info_advertise(struct repository *r, struct strbuf *value UNUSED) +static int object_info_advertise(struct repository *r, struct strbuf *value) { if (advertise_object_info == -1 && repo_config_get_bool(r, "transfer.advertiseobjectinfo", @@ -78,6 +78,8 @@ static int object_info_advertise(struct repository *r, struct strbuf *value UNUS /* disabled by default */ advertise_object_info = 0; } + if (value && advertise_object_info) + strbuf_addstr(value, "size"); return advertise_object_info; } From patchwork Wed Jan 8 18:37:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13931464 Received: from mail-qt1-f180.google.com (mail-qt1-f180.google.com [209.85.160.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3BE2F2036ED for ; Wed, 8 Jan 2025 18:38:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361490; cv=none; b=o8CBj0ozMB8m4EYqveyCMsdGJ7V6eNQiJ+SC3Q4KdJoDMdBoYgBa4GyDybUUCYQGJX9VxLJFk2KN0CLJ7dBbAwRPeG/EEVjPWygA41gGfuLq4JGwzZKpvF/0gNs7gDqMIMrINUTt6CowVcs72te/HKSevE0hI/NetKc7M2X9k+Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361490; c=relaxed/simple; bh=IWyXelcEckC1DOmq0T819MfQ0FOT8ihTv77d7OjwHhU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CM9bqgmVpM3tontEJD4uQvuORjT+O3OmQil4gBE/qdMU6hynnuDQ8/xkDNHGkDFbQnG358FVczO52MqisO8Pax07fwB95pZb9Xufcsg3xU8wD50uAH684fh9YLLmJILFi6RyGWd5fYy9mPZINy6x7M1Ln2BMGRpZlRWOvXpDdts= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XPBu1HJe; arc=none smtp.client-ip=209.85.160.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XPBu1HJe" Received: by mail-qt1-f180.google.com with SMTP id d75a77b69052e-46785fbb949so86641cf.3 for ; Wed, 08 Jan 2025 10:38:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736361486; x=1736966286; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=32o2mCpyTFJSE1jfDUNvvNp78Xy4w0ZNmCe+x0QzmzI=; b=XPBu1HJeZq7uJ8ZUI1FziTOIKrAoljCTJZWzML1aUP7eZUj552ZPRbzq/Nlo6DWIVf pOgPmxNvXeZXW5gkdud1ckB9kQr4+zQSZbaRn8wf4uK0yD9T1aBn7bzMr6fsf7NtUgfv ROK+bzoqX4UYeWaa9xjhmrSa1msHdhJA8wvwryLMiUQcxK/IUj+5Gx2ss3oT5dBHppuW j0lTcnL5aLWh7XxzKQqycZK+mSC6qkM5hQvyiZ5AowRQWBlnZNKscNHi7osoO5vGq9TR +uhtiKrnAlXDfPnV2O62C7BwCcgfs3/9vigy4ngK0upzpN7LJw2sUSDZUAyRhTHAVSiD IfDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736361486; x=1736966286; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=32o2mCpyTFJSE1jfDUNvvNp78Xy4w0ZNmCe+x0QzmzI=; b=Bts8HtQAeA6UKNsdQo7XhX3i1AX1IYsqeaXu+RRABzC9+o1dGDcchPzkXg3aFF1Ud+ fshhMQzpnflPYYNJyHrBfL/eCRJNi1btNn9Fw0Pg44vjvUDP7SUlh+U4Mbvxr+vimgof L678Xg4XauAfklnJzUxxGpQZgRqbQtBFFttOgaFLdx28cO7Ascx830rT5zpIY7cN3gZ1 L9xyn7fxcuZE4BjlRJGMxuk8O0eRS0rFZYpbuQO2YaGbV2qGmjxzNLuiDquY1d2cQcvw H3kPpzOPYc6BrqlONGpS+8o9+p0tM8ch2qiO75dyXMrDn1J2vzuGIKdBIi3SjN64N3HV ms6Q== X-Gm-Message-State: AOJu0YzngFEP17zp61zElzQwtjF1CPjVkEj//VD7qonIfvXW90V7C1LL MeWDTB0FijYrRXT6d/S7JW8hfe/SQh/dpbyfRq0xKfCnNr3vfW0oTJUcog5vIsU= X-Gm-Gg: ASbGncspfqpVhlvgtXjZ91LEwM60Wbxn2BME0rbT74eytoRGRjwphwlF3rHpJcVkwzt nu8H5unSN88KZWjsQ2FiexgLObwOAWV5OMICd2dzP4kEAfPU5GwyjRMzewDDeZBOnEs5Z8IONrO eHwNxL1WUzdG+A6rOF6E3YicyxOAttEDadIEzc9hRLhNPDnHjr8ByM2MhYmCsaKCAdamlFspg/1 rVdNd5Vk4c4m4LXVgAi/g2HyckITvZgnqwKtEaeaVAMIiQDs937vd0z+vJwhpzX5fRWsvl1mvMf D1iYFHPe3A== X-Google-Smtp-Source: AGHT+IGxyaXkbUtqVY7nQ91ifyMFlaILm/0xSX08r/RdGnh3NrXl4qmeINeE8CNPIEp0ixsj/QtkVw== X-Received: by 2002:a05:6214:495:b0:6d8:a76d:b66c with SMTP id 6a1803df08f44-6df9b2b33b0mr55480716d6.30.1736361485997; Wed, 08 Jan 2025 10:38:05 -0800 (PST) Received: from localhost.localdomain ([184.148.194.219]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6dd180ea74bsm193228036d6.25.2025.01.08.10.38.04 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 08 Jan 2025 10:38:05 -0800 (PST) From: Eric Ju To: git@vger.kernel.org Cc: calvinwan@google.com, jonathantanmy@google.com, chriscool@tuxfamily.org, eric.peijian@gmail.com, karthik.188@gmail.com, toon@iotcl.com, jltobler@gmail.com Subject: [PATCH v9 7/8] transport: add client support for object-info Date: Wed, 8 Jan 2025 13:37:38 -0500 Message-ID: <20250108183740.67022-8-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250108183740.67022-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250108183740.67022-1-eric.peijian@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Calvin Wan Sometimes, it is beneficial to retrieve information about an object without downloading it entirely. The server-side logic for this functionality was implemented in commit "a2ba162cda (object-info: support for retrieving object info, 2021-04-20)." And the wire format is documented at https://git-scm.com/docs/protocol-v2#_object_info. This commit introduces client functions to interact with the server. Currently, the client supports requesting a list of object IDs with the 'size' feature from a v2 server. If the server does not advertise this feature (i.e., transfer.advertiseobjectinfo is set to false), the client will return an error and exit. Notice that the entire request is written into req_buf before being sent to the remote. This approach follows the pattern used in the `send_fetch_request()` logic within fetch-pack.c. Streaming the request is not addressed in this patch. Helped-by: Jonathan Tan Helped-by: Christian Couder Signed-off-by: Calvin Wan Signed-off-by: Eric Ju --- Makefile | 1 + fetch-object-info.c | 85 +++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 22 ++++++++++++ fetch-pack.c | 3 ++ fetch-pack.h | 2 ++ transport-helper.c | 11 ++++-- transport.c | 28 ++++++++++++++- transport.h | 11 ++++++ 8 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 fetch-object-info.c create mode 100644 fetch-object-info.h diff --git a/Makefile b/Makefile index 97e8385b66..e8c6702b32 100644 --- a/Makefile +++ b/Makefile @@ -1020,6 +1020,7 @@ LIB_OBJS += ewah/ewah_rlw.o LIB_OBJS += exec-cmd.o LIB_OBJS += fetch-negotiator.o LIB_OBJS += fetch-pack.o +LIB_OBJS += fetch-object-info.o LIB_OBJS += fmt-merge-msg.o LIB_OBJS += fsck.o LIB_OBJS += fsmonitor.o diff --git a/fetch-object-info.c b/fetch-object-info.c new file mode 100644 index 0000000000..b279e06dc8 --- /dev/null +++ b/fetch-object-info.c @@ -0,0 +1,85 @@ +#include "git-compat-util.h" +#include "gettext.h" +#include "hex.h" +#include "pkt-line.h" +#include "connect.h" +#include "oid-array.h" +#include "object-store-ll.h" +#include "fetch-object-info.h" +#include "string-list.h" + +/* Sends git-cat-file object-info command and its arguments into the request buffer. */ +static void send_object_info_request(const int fd_out, struct object_info_args *args) +{ + struct strbuf req_buf = STRBUF_INIT; + + write_command_and_capabilities(&req_buf, "object-info", args->server_options); + + if (unsorted_string_list_has_string(args->object_info_options, "size")) + packet_buf_write(&req_buf, "size"); + + if (args->oids) + for (size_t i = 0; i < args->oids->nr; i++) + packet_buf_write(&req_buf, "oid %s", oid_to_hex(&args->oids->oid[i])); + + packet_buf_flush(&req_buf); + if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0) + die_errno(_("unable to write request to remote")); + + strbuf_release(&req_buf); +} + +int fetch_object_info(const enum protocol_version version, struct object_info_args *args, + struct packet_reader *reader, struct object_info *object_info_data, + const int stateless_rpc, const int fd_out) +{ + int size_index = -1; + + switch (version) { + case protocol_v2: + if (!server_supports_v2("object-info")) + die(_("object-info capability is not enabled on the server")); + send_object_info_request(fd_out, args); + break; + case protocol_v1: + case protocol_v0: + die(_("unsupported protocol version. expected v2")); + case protocol_unknown_version: + BUG("unknown protocol version"); + } + + for (size_t i = 0; i < args->object_info_options->nr; i++) { + if (packet_reader_read(reader) != PACKET_READ_NORMAL) { + check_stateless_delimiter(stateless_rpc, reader, "stateless delimiter expected"); + return -1; + } + if (!string_list_has_string(args->object_info_options, reader->line)) + return -1; + if (!strcmp(reader->line, "size")) { + size_index = i; + for (size_t j = 0; j < args->oids->nr; j++) + object_info_data[j].sizep = xcalloc(1, sizeof(*object_info_data[j].sizep)); + } + } + + for (size_t i = 0; packet_reader_read(reader) == PACKET_READ_NORMAL && i < args->oids->nr; i++){ + struct string_list object_info_values = STRING_LIST_INIT_DUP; + + string_list_split(&object_info_values, reader->line, ' ', -1); + if (0 <= size_index) { + if (!strcmp(object_info_values.items[1 + size_index].string, "")) + die("object-info: not our ref %s", + object_info_values.items[0].string); + + if (strtoul_ul(object_info_values.items[1 + size_index].string, 10, object_info_data[i].sizep)) + die("object-info: ref %s has invalid size %s", + object_info_values.items[0].string, + object_info_values.items[1 + size_index].string); + } + + string_list_clear(&object_info_values, 0); + } + check_stateless_delimiter(stateless_rpc, reader, "stateless delimiter expected"); + + return 0; +} diff --git a/fetch-object-info.h b/fetch-object-info.h new file mode 100644 index 0000000000..6184d04d72 --- /dev/null +++ b/fetch-object-info.h @@ -0,0 +1,22 @@ +#ifndef FETCH_OBJECT_INFO_H +#define FETCH_OBJECT_INFO_H + +#include "pkt-line.h" +#include "protocol.h" +#include "object-store-ll.h" + +struct object_info_args { + struct string_list *object_info_options; + const struct string_list *server_options; + struct oid_array *oids; +}; + +/* + * Sends git-cat-file object-info command into the request buf and read the + * results from packets. + */ +int fetch_object_info(enum protocol_version version, struct object_info_args *args, + struct packet_reader *reader, struct object_info *object_info_data, + int stateless_rpc, int fd_out); + +#endif /* FETCH_OBJECT_INFO_H */ diff --git a/fetch-pack.c b/fetch-pack.c index 51de82e414..704bc21b47 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -1654,6 +1654,9 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args, if (args->depth > 0 || args->deepen_since || args->deepen_not) args->deepen = 1; + if (args->object_info) + state = FETCH_SEND_REQUEST; + while (state != FETCH_DONE) { switch (state) { case FETCH_CHECK_LOCAL: diff --git a/fetch-pack.h b/fetch-pack.h index 9d3470366f..119d3369f1 100644 --- a/fetch-pack.h +++ b/fetch-pack.h @@ -16,6 +16,7 @@ struct fetch_pack_args { const struct string_list *deepen_not; struct list_objects_filter_options filter_options; const struct string_list *server_options; + struct object_info *object_info_data; /* * If not NULL, during packfile negotiation, fetch-pack will send "have" @@ -42,6 +43,7 @@ struct fetch_pack_args { unsigned reject_shallow_remote:1; unsigned deepen:1; unsigned refetch:1; + unsigned object_info:1; /* * Indicate that the remote of this request is a promisor remote. The diff --git a/transport-helper.c b/transport-helper.c index d457b42550..9da1547b2c 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -710,8 +710,8 @@ static int fetch_refs(struct transport *transport, /* * If we reach here, then the server, the client, and/or the transport - * helper does not support protocol v2. --negotiate-only requires - * protocol v2. + * helper does not support protocol v2. --negotiate-only and cat-file + * remote-object-info require protocol v2. */ if (data->transport_options.acked_commits) { warning(_("--negotiate-only requires protocol v2")); @@ -727,6 +727,13 @@ static int fetch_refs(struct transport *transport, free_refs(dummy); } + /* fail the command explicitly to avoid further commands input. */ + if (transport->smart_options->object_info) + die(_("remote-object-info requires protocol v2")); + + if (!data->get_refs_list_called) + get_refs_list_using_list(transport, 0); + count = 0; for (i = 0; i < nr_heads; i++) if (!(to_fetch[i]->status & REF_STATUS_UPTODATE)) diff --git a/transport.c b/transport.c index 10d820c333..b6a2052908 100644 --- a/transport.c +++ b/transport.c @@ -9,6 +9,7 @@ #include "hook.h" #include "pkt-line.h" #include "fetch-pack.h" +#include "fetch-object-info.h" #include "remote.h" #include "connect.h" #include "send-pack.h" @@ -464,8 +465,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.server_options = transport->server_options; args.negotiation_tips = data->options.negotiation_tips; args.reject_shallow_remote = transport->smart_options->reject_shallow; + args.object_info = transport->smart_options->object_info; + + if (transport->smart_options && transport->smart_options->object_info + && transport->smart_options->object_info_oids->nr > 0) { + struct packet_reader reader; + struct object_info_args obj_info_args = { 0 }; + + obj_info_args.server_options = transport->server_options; + obj_info_args.oids = transport->smart_options->object_info_oids; + obj_info_args.object_info_options = transport->smart_options->object_info_options; + string_list_sort(obj_info_args.object_info_options); + + connect_setup(transport, 0); + packet_reader_init(&reader, data->fd[0], NULL, 0, + PACKET_READ_CHOMP_NEWLINE | + PACKET_READ_GENTLE_ON_EOF | + PACKET_READ_DIE_ON_ERR_PACKET); + + data->version = discover_version(&reader); + transport->hash_algo = reader.hash_algo; + + ret = fetch_object_info(data->version, &obj_info_args, &reader, + data->options.object_info_data, transport->stateless_rpc, + data->fd[1]); + goto cleanup; - if (!data->finished_handshake) { + } else if (!data->finished_handshake) { int i; int must_list_refs = 0; for (i = 0; i < nr_heads; i++) { diff --git a/transport.h b/transport.h index 44100fa9b7..e61e931863 100644 --- a/transport.h +++ b/transport.h @@ -5,6 +5,7 @@ #include "remote.h" #include "list-objects-filter-options.h" #include "string-list.h" +#include "object-store.h" struct git_transport_options { unsigned thin : 1; @@ -30,6 +31,12 @@ struct git_transport_options { */ unsigned connectivity_checked:1; + /* + * Transport will attempt to retrieve only object-info. + * If object-info is not supported, the operation will error and exit. + */ + unsigned object_info : 1; + int depth; const char *deepen_since; const struct string_list *deepen_not; @@ -53,6 +60,10 @@ struct git_transport_options { * common commits to this oidset instead of fetching any packfiles. */ struct oidset *acked_commits; + + struct oid_array *object_info_oids; + struct object_info *object_info_data; + struct string_list *object_info_options; }; enum transport_family { From patchwork Wed Jan 8 18:37:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13931465 Received: from mail-qv1-f51.google.com (mail-qv1-f51.google.com [209.85.219.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6F87201256 for ; Wed, 8 Jan 2025 18:38:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361491; cv=none; b=uCtiZvxVWSKbIw+bGaQD/1Gqkz/ynA4yTeOCNf3aDwAm8hEXhlQJwn5AAS4+fQDY6jM//aeYfMWAZAfBKBaPbq0gK5wXHH3ZBAc137qJgTFsXnCXH0uM5fbeNH9RE2PZ6CD1ac7ImTj3DI69PbMfQG46BTGJbZ5WEx6HJTL6rPQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736361491; c=relaxed/simple; bh=TUSpBreY/FM34aVBg/BJzZ/wQknS1jnSV0Mll6MAKXE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TH8uiCDqYFoBGnFfdC/9rgUBgj+o8yFxkwS6DbstukIGMWP+U5s/3eU2W5IQHhnsOFTVsjsDDcCMq9LtP51H18smotli5RPo1ss2ncxtKi3ZGax7ArVPUY5nVjHSgR0/xiQIYTfU4kYvl+8mAm5OQnIReHMoMepHbcqNXwcGFnA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AYAgZhoQ; arc=none smtp.client-ip=209.85.219.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AYAgZhoQ" Received: by mail-qv1-f51.google.com with SMTP id 6a1803df08f44-6dcc42b5e30so1870516d6.1 for ; Wed, 08 Jan 2025 10:38:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736361487; x=1736966287; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bVPWpS8Bqn153DF9PaFCW9fQuwgXoHmbsRMFIz6Qn3g=; b=AYAgZhoQ1tyPAHlGpDBW2DpiOr9mLX71Z/vxy/AwNwN7So+HzsyT9Ysw+VRYwKNJUw g1FCu+AfXZFQBEQGF3lcpLfjD8UtBpQYaAEscoUySKONBjNvtKAYJh4/TNKkYAOdhIWh NV2MnA5FYHiou/iB+h/k2q2HdpqSqDB2zujzbKq/n8GVjh1bLP6LUtkFPkSwfkKEE/e+ DKsK/Im8XUpikhpaNsEZLPMVpcuoE11sm2y1QQJfk4jFyUinvvFTmL9UNvlqvMBPB80b ACLT0vm3HRCAHzIxjVtbqfKWDlVWH9VPbgHmbBV0D168ays0A7UT8NhYIFz36VRbkBsa rRkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736361487; x=1736966287; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bVPWpS8Bqn153DF9PaFCW9fQuwgXoHmbsRMFIz6Qn3g=; b=nFEIsvgNKcz3ERHQSt2sEiBBO8wxkWkjq62tMEO3yxFMiSUzeeed7MZPx32RPcF5Q0 ZoLrSlmYHycQirpceDKkq1w/S7YvoAjWnQ7BTr7xr6s4zghFV7AKs0WDTGNM74bZxxrm qAeMImNUyGpbJqnc5ElsdhFaYyVng8xzG1Sy3/D9+JobM/TK+gBMmTZY3Xf/NT1TyI6q XQyUJtQDVPV7KVYNFy0PY2fcP37Ajzpnz958WnGVgtLn8QASvZRjBIriQkRJZmC07gpm DOLPxhr/3+hZniVRzKl2ceFO7elYG/E0UNLSL/8rPJGPjQjg1W2MLN0HZQUSFEIKqZ7t 0Hpg== X-Gm-Message-State: AOJu0YxRCDC/G++XAjbLgsrOkwYHGiYkqpjA3q1jS36ofdmuuOvV7rVv 2V6ftl8/GOfdYsZHgpJ5I7DP2KtmlW2Vay1f2P5/pXoH0jTjBEmw+6yfq/xZyoA= X-Gm-Gg: ASbGncuqs4bCtTqwCA15ipcgAmjWONZN2/Sd+7Y1ERm7wnSJ0inIU9d/nbTHINZrMUI sRRaUXUnXt8dL3SKsl+X9eKJZW1JiRGjBi587fzuQgasii1JpgXUhsteyKO45bRsoJq6O5kYvP5 3+JWBDHbvkhxvNQFvJkhm7J2lLZhehZO2fVMyPrCCLNM29ZU2IlapwJx+eh9s8CWeOhk8eO3aDY 3UeRrykSTlJj9pAaUBn4aYcoLIe3KMQVjRE6XhsnF2o1LfBGXPfTbpGu6jDjUm6XKBK0Z8ViP+n QER5knN9XQ== X-Google-Smtp-Source: AGHT+IHhjlK4aP1YrGw7GcCRqW7j3/eolE5W+H3FKnjdaGUIaINKK4kd8gKk4W1Rypg1cFwBnCO/fQ== X-Received: by 2002:a05:6214:2428:b0:6d8:960e:5785 with SMTP id 6a1803df08f44-6df9b234c28mr64624596d6.13.1736361487303; Wed, 08 Jan 2025 10:38:07 -0800 (PST) Received: from localhost.localdomain ([184.148.194.219]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6dd180ea74bsm193228036d6.25.2025.01.08.10.38.06 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 08 Jan 2025 10:38:06 -0800 (PST) From: Eric Ju To: git@vger.kernel.org Cc: calvinwan@google.com, jonathantanmy@google.com, chriscool@tuxfamily.org, eric.peijian@gmail.com, karthik.188@gmail.com, toon@iotcl.com, jltobler@gmail.com Subject: [PATCH v9 8/8] cat-file: add remote-object-info to batch-command Date: Wed, 8 Jan 2025 13:37:39 -0500 Message-ID: <20250108183740.67022-9-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250108183740.67022-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250108183740.67022-1-eric.peijian@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Since the `info` command in cat-file --batch-command prints object info for a given object, it is natural to add another command in cat-file --batch-command to print object info for a given object from a remote. Add `remote-object-info` to cat-file --batch-command. While `info` takes object ids one at a time, this creates overhead when making requests to a server.So `remote-object-info` instead can take multiple object ids at once. cat-file --batch-command is generally implemented in the following manner: - Receive and parse input from user - Call respective function attached to command - Get object info, print object info In --buffer mode, this changes to: - Receive and parse input from user - Store respective function attached to command in a queue - After flush, loop through commands in queue - Call respective function attached to command - Get object info, print object info Notice how the getting and printing of object info is accomplished one at a time. As described above, this creates a problem for making requests to a server. Therefore, `remote-object-info` is implemented in the following manner: - Receive and parse input from user If command is `remote-object-info`: - Get object info from remote - Loop through and print each object info Else: - Call respective function attached to command - Parse input, get object info, print object info And finally for --buffer mode `remote-object-info`: - Receive and parse input from user - Store respective function attached to command in a queue - After flush, loop through commands in queue: If command is `remote-object-info`: - Get object info from remote - Loop through and print each object info Else: - Call respective function attached to command - Get object info, print object info To summarize, `remote-object-info` gets object info from the remote and then loop through the object info passed in, printing the info. In order for remote-object-info to avoid remote communication overhead in the non-buffer mode, the objects are passed in as such: remote-object-info ... rather than remote-object-info remote-object-info ... remote-object-info Helped-by: Jonathan Tan Helped-by: Christian Couder Signed-off-by: Calvin Wan Signed-off-by: Eric Ju --- Documentation/git-cat-file.txt | 24 +- builtin/cat-file.c | 99 ++++ object-file.c | 11 + object-store-ll.h | 3 + t/t1017-cat-file-remote-object-info.sh | 664 +++++++++++++++++++++++++ 5 files changed, 797 insertions(+), 4 deletions(-) create mode 100755 t/t1017-cat-file-remote-object-info.sh diff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt index d5890ae368..6a2f9fd752 100644 --- a/Documentation/git-cat-file.txt +++ b/Documentation/git-cat-file.txt @@ -149,6 +149,13 @@ info :: Print object info for object reference ``. This corresponds to the output of `--batch-check`. +remote-object-info ...:: + Print object info for object references `` at specified + `` without downloading objects from the remote. + Error when the `object-info` capability is not supported by the server. + Error when no object references are provided. + This command may be combined with `--buffer`. + flush:: Used with `--buffer` to execute all preceding commands that were issued since the beginning or since the last flush was issued. When `--buffer` @@ -290,7 +297,8 @@ newline. The available atoms are: The full hex representation of the object name. `objecttype`:: - The type of the object (the same as `cat-file -t` reports). + The type of the object (the same as `cat-file -t` reports). See + `CAVEATS` below. Not supported by `remote-object-info`. `objectsize`:: The size, in bytes, of the object (the same as `cat-file -s` @@ -298,13 +306,14 @@ newline. The available atoms are: `objectsize:disk`:: The size, in bytes, that the object takes up on disk. See the - note about on-disk sizes in the `CAVEATS` section below. + note about on-disk sizes in the `CAVEATS` section below. Not + supported by `remote-object-info`. `deltabase`:: If the object is stored as a delta on-disk, this expands to the full hex representation of the delta base object name. Otherwise, expands to the null OID (all zeroes). See `CAVEATS` - below. + below. Not supported by `remote-object-info`. `rest`:: If this atom is used in the output string, input lines are split @@ -314,7 +323,10 @@ newline. The available atoms are: line) are output in place of the `%(rest)` atom. If no format is specified, the default format is `%(objectname) -%(objecttype) %(objectsize)`. +%(objecttype) %(objectsize)`, except for `remote-object-info` commands which use +`%(objectname) %(objectsize)` for now because "%(objecttype)" is not supported yet. +WARNING: When "%(objecttype)" is supported, the default format WILL be unified, so +DO NOT RELY on the current the default format to stay the same!!! If `--batch` is specified, or if `--batch-command` is used with the `contents` command, the object information is followed by the object contents (consisting @@ -396,6 +408,10 @@ scripting purposes. CAVEATS ------- +Note that since %(objecttype), %(objectsize:disk) and %(deltabase) are +currently not supported by the `remote-object-info` command, we will error +and exit when they are in the format string. + Note that the sizes of objects on disk are reported accurately, but care should be taken in drawing conclusions about which refs or objects are responsible for disk usage. The size of a packed non-delta object may be diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 69ea642dc6..5fb71aaf91 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -27,6 +27,9 @@ #include "promisor-remote.h" #include "mailmap.h" #include "write-or-die.h" +#include "alias.h" +#include "remote.h" +#include "transport.h" enum batch_mode { BATCH_MODE_CONTENTS, @@ -45,9 +48,12 @@ struct batch_options { char input_delim; char output_delim; const char *format; + int use_remote_info; }; static const char *force_path; +static struct object_info *remote_object_info; +static struct oid_array object_info_oids = OID_ARRAY_INIT; static struct string_list mailmap = STRING_LIST_INIT_NODUP; static int use_mailmap; @@ -579,6 +585,61 @@ static void batch_one_object(const char *obj_name, object_context_release(&ctx); } +static int get_remote_info(struct batch_options *opt, int argc, const char **argv) +{ + int retval = 0; + struct remote *remote = NULL; + struct object_id oid; + struct string_list object_info_options = STRING_LIST_INIT_NODUP; + static struct transport *gtransport; + + /* + * Change the format to "%(objectname) %(objectsize)" when + * remote-object-info command is used. Once we start supporting objecttype + * the default format should change to DEFAULT_FORMAT + */ + if (!opt->format) + opt->format = "%(objectname) %(objectsize)"; + + remote = remote_get(argv[0]); + if (!remote) + die(_("must supply valid remote when using remote-object-info")); + + oid_array_clear(&object_info_oids); + for (size_t i = 1; i < argc; i++) { + if (get_oid_hex(argv[i], &oid)) + die(_("Not a valid object name %s"), argv[i]); + oid_array_append(&object_info_oids, &oid); + } + if (object_info_oids.nr == 0) { + die(_("remote-object-info requires objects")); + } + gtransport = transport_get(remote, NULL); + if (gtransport->smart_options) { + CALLOC_ARRAY(remote_object_info, object_info_oids.nr); + gtransport->smart_options->object_info = 1; + gtransport->smart_options->object_info_oids = &object_info_oids; + + /* 'objectsize' is the only option currently supported */ + if (!strstr(opt->format, "%(objectsize)")) + die(_("%s is currently not supported with remote-object-info"), opt->format); + + string_list_append(&object_info_options, "size"); + + if (object_info_options.nr > 0) { + gtransport->smart_options->object_info_options = &object_info_options; + gtransport->smart_options->object_info_data = remote_object_info; + retval = transport_fetch_refs(gtransport, NULL); + } + } else { + retval = -1; + } + + string_list_clear(&object_info_options, 0); + transport_disconnect(gtransport); + return retval; +} + struct object_cb_data { struct batch_options *opt; struct expand_data *expand; @@ -670,6 +731,43 @@ static void parse_cmd_info(struct batch_options *opt, batch_one_object(line, output, opt, data); } +static void parse_cmd_remote_object_info(struct batch_options *opt, + const char *line, struct strbuf *output, + struct expand_data *data) +{ + int count; + const char **argv; + + char *line_to_split = xstrdup_or_null(line); + count = split_cmdline(line_to_split, &argv); + if (get_remote_info(opt, count, argv)) + goto cleanup; + + opt->use_remote_info = 1; + data->skip_object_info = 1; + for (size_t i = 0; i < object_info_oids.nr; i++) { + data->oid = object_info_oids.oid[i]; + if (remote_object_info[i].sizep) { + /* + * When reaching here, it means remote-object-info can retrieve + * information from server without downloading them. + */ + data->size = *remote_object_info[i].sizep; + opt->batch_mode = BATCH_MODE_INFO; + batch_object_write(argv[i+1], output, opt, data, NULL, 0); + } + } + opt->use_remote_info = 0; + data->skip_object_info = 0; + +cleanup: + for (size_t i = 0; i < object_info_oids.nr; i++) + free_object_info_contents(&remote_object_info[i]); + free(line_to_split); + free(argv); + free(remote_object_info); +} + static void dispatch_calls(struct batch_options *opt, struct strbuf *output, struct expand_data *data, @@ -701,6 +799,7 @@ static const struct parse_cmd { } commands[] = { { "contents", parse_cmd_contents, 1}, { "info", parse_cmd_info, 1}, + { "remote-object-info", parse_cmd_remote_object_info, 1}, { "flush", NULL, 0}, }; diff --git a/object-file.c b/object-file.c index 5b792b3dd4..96f204c93a 100644 --- a/object-file.c +++ b/object-file.c @@ -3128,3 +3128,14 @@ int read_loose_object(const char *path, munmap(map, mapsize); return ret; } + +void free_object_info_contents(struct object_info *object_info) +{ + if (!object_info) + return; + free(object_info->typep); + free(object_info->sizep); + free(object_info->disk_sizep); + free(object_info->delta_base_oid); + free(object_info->type_name); +} diff --git a/object-store-ll.h b/object-store-ll.h index cd3bd5bd99..20208e1d4f 100644 --- a/object-store-ll.h +++ b/object-store-ll.h @@ -553,4 +553,7 @@ int for_each_object_in_pack(struct packed_git *p, int for_each_packed_object(struct repository *repo, each_packed_object_fn cb, void *data, enum for_each_object_flags flags); +/* Free pointers inside of object_info, but not object_info itself */ +void free_object_info_contents(struct object_info *object_info); + #endif /* OBJECT_STORE_LL_H */ diff --git a/t/t1017-cat-file-remote-object-info.sh b/t/t1017-cat-file-remote-object-info.sh new file mode 100755 index 0000000000..fd6c63cdb9 --- /dev/null +++ b/t/t1017-cat-file-remote-object-info.sh @@ -0,0 +1,664 @@ +#!/bin/sh + +test_description='git cat-file --batch-command with remote-object-info command' + +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME + +. ./test-lib.sh +. "$TEST_DIRECTORY"/lib-cat-file.sh + +hello_content="Hello World" +hello_size=$(strlen "$hello_content") +hello_oid=$(echo_without_newline "$hello_content" | git hash-object --stdin) + +# This is how we get 13: +# 13 = + + + , where +# file mode is 100644, which is 6 characters; +# file name is hello, which is 5 characters +# a space is 1 character and a null is 1 character +tree_size=$(($(test_oid rawsz) + 13)) + +commit_message="Initial commit" + +# This is how we get 137: +# 137 = + + + +# + + +# + + +# + +# +# An easier way to calculate is: 1. use `git cat-file commit | wc -c`, +# to get 177, 2. then deduct 40 hex characters to get 137 +commit_size=$(($(test_oid hexsz) + 137)) + +tag_header_without_oid="type blob +tag hellotag +tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" +tag_header_without_timestamp="object $hello_oid +$tag_header_without_oid" +tag_description="This is a tag" +tag_content="$tag_header_without_timestamp 0 +0000 + +$tag_description" + +tag_oid=$(echo_without_newline "$tag_content" | git hash-object -t tag --stdin -w) +tag_size=$(strlen "$tag_content") + +set_transport_variables () { + hello_oid=$(echo_without_newline "$hello_content" | git hash-object --stdin) + tree_oid=$(git -C "$1" write-tree) + commit_oid=$(echo_without_newline "$commit_message" | git -C "$1" commit-tree $tree_oid) + tag_oid=$(echo_without_newline "$tag_content" | git -C "$1" hash-object -t tag --stdin -w) + tag_size=$(strlen "$tag_content") +} + +# This section tests --batch-command with remote-object-info command +# Since "%(objecttype)" is currently not supported by the command remote-object-info , +# the filters are set to "%(objectname) %(objectsize)" in some test cases. + +# Test --batch-command remote-object-info with 'git://' transport with +# transfer.advertiseobjectinfo set to true, i.e. server has object-info capability +. "$TEST_DIRECTORY"/lib-git-daemon.sh +start_git_daemon --export-all --enable=receive-pack +daemon_parent=$GIT_DAEMON_DOCUMENT_ROOT_PATH/parent + +test_expect_success 'create repo to be served by git-daemon' ' + git init "$daemon_parent" && + echo_without_newline "$hello_content" > $daemon_parent/hello && + git -C "$daemon_parent" update-index --add hello && + git -C "$daemon_parent" config transfer.advertiseobjectinfo true && + git clone "$GIT_DAEMON_URL/parent" -n "$daemon_parent/daemon_client_empty" +' + +test_expect_success 'batch-command remote-object-info git://' ' + ( + set_transport_variables "$daemon_parent" && + cd "$daemon_parent/daemon_client_empty" && + + # These results prove remote-object-info can get object info from the remote + echo "$hello_oid $hello_size" >expect && + echo "$tree_oid $tree_size" >>expect && + echo "$commit_oid $commit_size" >>expect && + echo "$tag_oid $tag_size" >>expect && + + # These results prove remote-object-info did not download objects from the remote + echo "$hello_oid missing" >>expect && + echo "$tree_oid missing" >>expect && + echo "$commit_oid missing" >>expect && + echo "$tag_oid missing" >>expect && + + git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF && + remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid + remote-object-info "$GIT_DAEMON_URL/parent" $tree_oid + remote-object-info "$GIT_DAEMON_URL/parent" $commit_oid + remote-object-info "$GIT_DAEMON_URL/parent" $tag_oid + info $hello_oid + info $tree_oid + info $commit_oid + info $tag_oid + EOF + test_cmp expect actual + ) +' + +test_expect_success 'batch-command remote-object-info git:// multiple sha1 per line' ' + ( + set_transport_variables "$daemon_parent" && + cd "$daemon_parent/daemon_client_empty" && + + # These results prove remote-object-info can get object info from the remote + echo "$hello_oid $hello_size" >expect && + echo "$tree_oid $tree_size" >>expect && + echo "$commit_oid $commit_size" >>expect && + echo "$tag_oid $tag_size" >>expect && + + # These results prove remote-object-info did not download objects from the remote + echo "$hello_oid missing" >>expect && + echo "$tree_oid missing" >>expect && + echo "$commit_oid missing" >>expect && + echo "$tag_oid missing" >>expect && + + git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF && + remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid $tree_oid $commit_oid $tag_oid + info $hello_oid + info $tree_oid + info $commit_oid + info $tag_oid + EOF + test_cmp expect actual + ) +' + +test_expect_success 'batch-command remote-object-info git:// default filter' ' + ( + set_transport_variables "$daemon_parent" && + cd "$daemon_parent/daemon_client_empty" && + + echo "$hello_oid $hello_size" >expect && + echo "$tree_oid $tree_size" >>expect && + echo "$commit_oid $commit_size" >>expect && + echo "$tag_oid $tag_size" >>expect && + GIT_TRACE_PACKET=1 git cat-file --batch-command >actual <<-EOF && + remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid $tree_oid + remote-object-info "$GIT_DAEMON_URL/parent" $commit_oid $tag_oid + EOF + test_cmp expect actual + ) +' + +test_expect_success 'batch-command --buffer remote-object-info git://' ' + ( + set_transport_variables "$daemon_parent" && + cd "$daemon_parent/daemon_client_empty" && + + # These results prove remote-object-info can get object info from the remote + echo "$hello_oid $hello_size" >expect && + echo "$tree_oid $tree_size" >>expect && + echo "$commit_oid $commit_size" >>expect && + echo "$tag_oid $tag_size" >>expect && + + # These results prove remote-object-info did not download objects from the remote + echo "$hello_oid missing" >>expect && + echo "$tree_oid missing" >>expect && + echo "$commit_oid missing" >>expect && + echo "$tag_oid missing" >>expect && + + git cat-file --batch-command="%(objectname) %(objectsize)" --buffer >actual <<-EOF && + remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid $tree_oid + remote-object-info "$GIT_DAEMON_URL/parent" $commit_oid $tag_oid + info $hello_oid + info $tree_oid + info $commit_oid + info $tag_oid + flush + EOF + test_cmp expect actual + ) +' + +test_expect_success 'batch-command -Z remote-object-info git:// default filter' ' + ( + set_transport_variables "$daemon_parent" && + cd "$daemon_parent/daemon_client_empty" && + + printf "%s\0" "$hello_oid $hello_size" >expect && + printf "%s\0" "$tree_oid $tree_size" >>expect && + printf "%s\0" "$commit_oid $commit_size" >>expect && + printf "%s\0" "$tag_oid $tag_size" >>expect && + + printf "%s\0" "$hello_oid missing" >>expect && + printf "%s\0" "$tree_oid missing" >>expect && + printf "%s\0" "$commit_oid missing" >>expect && + printf "%s\0" "$tag_oid missing" >>expect && + + batch_input="remote-object-info $GIT_DAEMON_URL/parent $hello_oid $tree_oid +remote-object-info $GIT_DAEMON_URL/parent $commit_oid $tag_oid +info $hello_oid +info $tree_oid +info $commit_oid +info $tag_oid +" && + echo_without_newline_nul "$batch_input" >commands_null_delimited && + + git cat-file --batch-command -Z < commands_null_delimited >actual && + test_cmp expect actual + ) +' + +# Test --batch-command remote-object-info with 'git://' and +# transfer.advertiseobjectinfo set to false, i.e. server does not have object-info capability +test_expect_success 'batch-command remote-object-info git:// fails when transfer.advertiseobjectinfo=false' ' + ( + git -C "$daemon_parent" config transfer.advertiseobjectinfo false && + set_transport_variables "$daemon_parent" && + + test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF && + remote-object-info $GIT_DAEMON_URL/parent $hello_oid $tree_oid $commit_oid $tag_oid + EOF + test_grep "object-info capability is not enabled on the server" err && + + # revert server state back + git -C "$daemon_parent" config transfer.advertiseobjectinfo true + + ) +' + +stop_git_daemon + +# Test --batch-command remote-object-info with 'file://' transport with +# transfer.advertiseobjectinfo set to true, i.e. server has object-info capability +# shellcheck disable=SC2016 +test_expect_success 'create repo to be served by file:// transport' ' + git init server && + git -C server config protocol.version 2 && + git -C server config transfer.advertiseobjectinfo true && + echo_without_newline "$hello_content" > server/hello && + git -C server update-index --add hello && + git clone -n "file://$(pwd)/server" file_client_empty +' + +test_expect_success 'batch-command remote-object-info file://' ' + ( + set_transport_variables "server" && + server_path="$(pwd)/server" && + cd file_client_empty && + + # These results prove remote-object-info can get object info from the remote + echo "$hello_oid $hello_size" >expect && + echo "$tree_oid $tree_size" >>expect && + echo "$commit_oid $commit_size" >>expect && + echo "$tag_oid $tag_size" >>expect && + + # These results prove remote-object-info did not download objects from the remote + echo "$hello_oid missing" >>expect && + echo "$tree_oid missing" >>expect && + echo "$commit_oid missing" >>expect && + echo "$tag_oid missing" >>expect && + + git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF && + remote-object-info "file://${server_path}" $hello_oid + remote-object-info "file://${server_path}" $tree_oid + remote-object-info "file://${server_path}" $commit_oid + remote-object-info "file://${server_path}" $tag_oid + info $hello_oid + info $tree_oid + info $commit_oid + info $tag_oid + EOF + test_cmp expect actual + ) +' + +test_expect_success 'batch-command remote-object-info file:// multiple sha1 per line' ' + ( + set_transport_variables "server" && + server_path="$(pwd)/server" && + cd file_client_empty && + + # These results prove remote-object-info can get object info from the remote + echo "$hello_oid $hello_size" >expect && + echo "$tree_oid $tree_size" >>expect && + echo "$commit_oid $commit_size" >>expect && + echo "$tag_oid $tag_size" >>expect && + + # These results prove remote-object-info did not download objects from the remote + echo "$hello_oid missing" >>expect && + echo "$tree_oid missing" >>expect && + echo "$commit_oid missing" >>expect && + echo "$tag_oid missing" >>expect && + + + git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF && + remote-object-info "file://${server_path}" $hello_oid $tree_oid $commit_oid $tag_oid + info $hello_oid + info $tree_oid + info $commit_oid + info $tag_oid + EOF + test_cmp expect actual + ) +' + +test_expect_success 'batch-command --buffer remote-object-info file://' ' + ( + set_transport_variables "server" && + server_path="$(pwd)/server" && + cd file_client_empty && + + # These results prove remote-object-info can get object info from the remote + echo "$hello_oid $hello_size" >expect && + echo "$tree_oid $tree_size" >>expect && + echo "$commit_oid $commit_size" >>expect && + echo "$tag_oid $tag_size" >>expect && + + # These results prove remote-object-info did not download objects from the remote + echo "$hello_oid missing" >>expect && + echo "$tree_oid missing" >>expect && + echo "$commit_oid missing" >>expect && + echo "$tag_oid missing" >>expect && + + git cat-file --batch-command="%(objectname) %(objectsize)" --buffer >actual <<-EOF && + remote-object-info "file://${server_path}" $hello_oid $tree_oid + remote-object-info "file://${server_path}" $commit_oid $tag_oid + info $hello_oid + info $tree_oid + info $commit_oid + info $tag_oid + flush + EOF + test_cmp expect actual + ) +' + +test_expect_success 'batch-command remote-object-info file:// default filter' ' + ( + set_transport_variables "server" && + server_path="$(pwd)/server" && + cd file_client_empty && + + echo "$hello_oid $hello_size" >expect && + echo "$tree_oid $tree_size" >>expect && + echo "$commit_oid $commit_size" >>expect && + echo "$tag_oid $tag_size" >>expect && + + git cat-file --batch-command >actual <<-EOF && + remote-object-info "file://${server_path}" $hello_oid $tree_oid + remote-object-info "file://${server_path}" $commit_oid $tag_oid + EOF + test_cmp expect actual + ) +' + +test_expect_success 'batch-command -Z remote-object-info file:// default filter' ' + ( + set_transport_variables "server" && + server_path="$(pwd)/server" && + cd file_client_empty && + + printf "%s\0" "$hello_oid $hello_size" >expect && + printf "%s\0" "$tree_oid $tree_size" >>expect && + printf "%s\0" "$commit_oid $commit_size" >>expect && + printf "%s\0" "$tag_oid $tag_size" >>expect && + + printf "%s\0" "$hello_oid missing" >>expect && + printf "%s\0" "$tree_oid missing" >>expect && + printf "%s\0" "$commit_oid missing" >>expect && + printf "%s\0" "$tag_oid missing" >>expect && + + batch_input="remote-object-info \"file://${server_path}\" $hello_oid $tree_oid +remote-object-info \"file://${server_path}\" $commit_oid $tag_oid +info $hello_oid +info $tree_oid +info $commit_oid +info $tag_oid +" && + echo_without_newline_nul "$batch_input" >commands_null_delimited && + + git cat-file --batch-command -Z < commands_null_delimited >actual && + test_cmp expect actual + ) +' + +# Test --batch-command remote-object-info with 'file://' and +# transfer.advertiseobjectinfo set to false, i.e. server does not have object-info capability +test_expect_success 'batch-command remote-object-info file:// fails when transfer.advertiseobjectinfo=false' ' + ( + set_transport_variables "server" && + server_path="$(pwd)/server" && + git -C "${server_path}" config transfer.advertiseobjectinfo false && + + test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF && + remote-object-info "file://${server_path}" $hello_oid $tree_oid $commit_oid $tag_oid + EOF + test_grep "object-info capability is not enabled on the server" err && + + # revert server state back + git -C "${server_path}" config transfer.advertiseobjectinfo true + ) +' + +# Test --batch-command remote-object-info with 'http://' transport with +# transfer.advertiseobjectinfo set to true, i.e. server has object-info capability + +. "$TEST_DIRECTORY"/lib-httpd.sh +start_httpd + +test_expect_success 'create repo to be served by http:// transport' ' + git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config http.receivepack true && + git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config transfer.advertiseobjectinfo true && + echo_without_newline "$hello_content" > $HTTPD_DOCUMENT_ROOT_PATH/http_parent/hello && + git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" update-index --add hello && + git clone "$HTTPD_URL/smart/http_parent" -n "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" +' + +test_expect_success 'batch-command remote-object-info http://' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" && + + # These results prove remote-object-info can get object info from the remote + echo "$hello_oid $hello_size" >expect && + echo "$tree_oid $tree_size" >>expect && + echo "$commit_oid $commit_size" >>expect && + echo "$tag_oid $tag_size" >>expect && + + # These results prove remote-object-info did not download objects from the remote + echo "$hello_oid missing" >>expect && + echo "$tree_oid missing" >>expect && + echo "$commit_oid missing" >>expect && + echo "$tag_oid missing" >>expect && + + git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF && + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid + remote-object-info "$HTTPD_URL/smart/http_parent" $tree_oid + remote-object-info "$HTTPD_URL/smart/http_parent" $commit_oid + remote-object-info "$HTTPD_URL/smart/http_parent" $tag_oid + info $hello_oid + info $tree_oid + info $commit_oid + info $tag_oid + EOF + test_cmp expect actual + ) +' + +test_expect_success 'batch-command remote-object-info http:// one line' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" && + + # These results prove remote-object-info can get object info from the remote + echo "$hello_oid $hello_size" >expect && + echo "$tree_oid $tree_size" >>expect && + echo "$commit_oid $commit_size" >>expect && + echo "$tag_oid $tag_size" >>expect && + + # These results prove remote-object-info did not download objects from the remote + echo "$hello_oid missing" >>expect && + echo "$tree_oid missing" >>expect && + echo "$commit_oid missing" >>expect && + echo "$tag_oid missing" >>expect && + + git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF && + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid $tree_oid $commit_oid $tag_oid + info $hello_oid + info $tree_oid + info $commit_oid + info $tag_oid + EOF + test_cmp expect actual + ) +' + +test_expect_success 'batch-command --buffer remote-object-info http://' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" && + + # These results prove remote-object-info can get object info from the remote + echo "$hello_oid $hello_size" >expect && + echo "$tree_oid $tree_size" >>expect && + echo "$commit_oid $commit_size" >>expect && + echo "$tag_oid $tag_size" >>expect && + + # These results prove remote-object-info did not download objects from the remote + echo "$hello_oid missing" >>expect && + echo "$tree_oid missing" >>expect && + echo "$commit_oid missing" >>expect && + echo "$tag_oid missing" >>expect && + + git cat-file --batch-command="%(objectname) %(objectsize)" --buffer >actual <<-EOF && + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid $tree_oid + remote-object-info "$HTTPD_URL/smart/http_parent" $commit_oid $tag_oid + info $hello_oid + info $tree_oid + info $commit_oid + info $tag_oid + flush + EOF + test_cmp expect actual + ) +' + +test_expect_success 'batch-command remote-object-info http:// default filter' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" && + + echo "$hello_oid $hello_size" >expect && + echo "$tree_oid $tree_size" >>expect && + echo "$commit_oid $commit_size" >>expect && + echo "$tag_oid $tag_size" >>expect && + + git cat-file --batch-command >actual <<-EOF && + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid $tree_oid + remote-object-info "$HTTPD_URL/smart/http_parent" $commit_oid $tag_oid + EOF + test_cmp expect actual + ) +' + +test_expect_success 'batch-command -Z remote-object-info http:// default filter' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" && + + printf "%s\0" "$hello_oid $hello_size" >expect && + printf "%s\0" "$tree_oid $tree_size" >>expect && + printf "%s\0" "$commit_oid $commit_size" >>expect && + printf "%s\0" "$tag_oid $tag_size" >>expect && + + batch_input="remote-object-info $HTTPD_URL/smart/http_parent $hello_oid $tree_oid +remote-object-info $HTTPD_URL/smart/http_parent $commit_oid $tag_oid +" && + echo_without_newline_nul "$batch_input" >commands_null_delimited && + + git cat-file --batch-command -Z < commands_null_delimited >actual && + test_cmp expect actual + ) +' + +test_expect_success 'remote-object-info fails on unspported filter option (objectsize:disk)' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + + test_must_fail git cat-file --batch-command="%(objectsize:disk)" 2>err <<-EOF && + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid + EOF + test_grep "%(objectsize:disk) is currently not supported with remote-object-info" err + ) +' + +test_expect_success 'remote-object-info fails on unspported filter option (deltabase)' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + + test_must_fail git cat-file --batch-command="%(deltabase)" 2>err <<-EOF && + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid + EOF + test_grep "%(deltabase) is currently not supported with remote-object-info" err + ) +' + +test_expect_success 'remote-object-info fails on server with legacy protocol' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + + test_must_fail git -c protocol.version=0 cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF && + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid + EOF + test_grep "remote-object-info requires protocol v2" err + ) +' + +test_expect_success 'remote-object-info fails on server with legacy protocol with default filter' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + + test_must_fail git -c protocol.version=0 cat-file --batch-command 2>err <<-EOF && + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid + EOF + test_grep "remote-object-info requires protocol v2" err + ) +' + +test_expect_success 'remote-object-info fails on malformed OID' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + malformed_object_id="this_id_is_not_valid" && + + test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF && + remote-object-info "$HTTPD_URL/smart/http_parent" $malformed_object_id + EOF + test_grep "Not a valid object name '$malformed_object_id'" err + ) +' + +test_expect_success 'remote-object-info fails on malformed OID with default filter' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + malformed_object_id="this_id_is_not_valid" && + + test_must_fail git cat-file --batch-command 2>err <<-EOF && + remote-object-info "$HTTPD_URL/smart/http_parent" $malformed_object_id + EOF + test_grep "Not a valid object name '$malformed_object_id'" err + ) +' + +test_expect_success 'remote-object-info fails on missing OID' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + git clone "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" missing_oid_repo && + test_commit -C missing_oid_repo message1 c.txt && + cd missing_oid_repo && + + object_id=$(git rev-parse message1:c.txt) && + test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF && + remote-object-info "$HTTPD_URL/smart/http_parent" $object_id + EOF + test_grep "object-info: not our ref $object_id" err + ) +' + +test_expect_success 'remote-object-info fails on not providing OID' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + + test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF && + remote-object-info "$HTTPD_URL/smart/http_parent" + EOF + test_grep "remote-object-info requires objects" err + ) +' + + +# Test --batch-command remote-object-info with 'http://' transport and +# transfer.advertiseobjectinfo set to false, i.e. server does not have object-info capability +test_expect_success 'batch-command remote-object-info http:// fails when transfer.advertiseobjectinfo=false ' ' + ( + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" && + git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config transfer.advertiseobjectinfo false && + + test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF && + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid $tree_oid $commit_oid $tag_oid + EOF + test_grep "object-info capability is not enabled on the server" err && + + # revert server state back + git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config transfer.advertiseobjectinfo true + ) +' + +# DO NOT add non-httpd-specific tests here, because the last part of this +# test script is only executed when httpd is available and enabled. + +test_done