From patchwork Tue Jan 14 02:14:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13938298 Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) (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 0E77835966 for ; Tue, 14 Jan 2025 02:15:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820930; cv=none; b=a3Tp0iMAxF7ZAmeuRsOc6hvmligYUT/chVYAlw8l8Nvo7WIB9vtJDXw+dyy825oKL1xYt3TclONaG956jS6zqhBUyr3ERu084+z0BwYSBI8TpL78jtoIwoR0SHfJsJ3Io6QPni7fBWiQJ1NbEqQZIVM+tcl1wBzzK5jgLlTNt8Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820930; c=relaxed/simple; bh=vexqpV1O36b32132VNXXq4Btme9tIKTgwV8rFnr3hE8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jWFPdLF9Hs3cc05mQGWw9Q+jm3W+yTIzESD8up4nsekaJqFZzGwbh4t9kAfKC1ifM0DtQMnOSaFEozjlnhkd+4oJBKhyx+xvVC2J0Ns2QlVJg+8ysiBOSMGJjhhAejHz2W8cbzRanhae4VIdUHEseusgeqIW/wKyh4fFSpT/K60= 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=KxZMKsDR; arc=none smtp.client-ip=209.85.160.178 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="KxZMKsDR" Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-46792996074so51637771cf.0 for ; Mon, 13 Jan 2025 18:15:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736820927; x=1737425727; 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=G9/aqIKfmSkyzqieNEKLl1J4fF5JOznvTgU531dvcs4=; b=KxZMKsDRv13s+OunrWlncWFjUEfUsUHnATzcgrAErhqZujHI4Kb8mL6W0NK+3gdAtM g+pbyXhgczuArBJEo/VotTVcscrBDvdtWMqZXXo9irZRrGLpzDXEOMFS4f8HrfxsuGgK eZou51v2G4IwLjijPGsGSKCcl7Ar3oohhIdQ3kR3CQRbUNdMwewcWqkdAVTgc2Q+obra nhjO6L+ZBa0e18XbB/mqFWy7jXx31U81d9dVs4wpAqdwm8phEOhVDDhN31NFm1RRXtRh oLIQUto7L7g8TZi2tO9oavy4CGgF98GVGdKPgZ9sm3cT4WN7zA6XO+9b3ch5zP7suwmx TwRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736820927; x=1737425727; 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=G9/aqIKfmSkyzqieNEKLl1J4fF5JOznvTgU531dvcs4=; b=wqnHw3x/sSTurAVmFr1LaMEb9WIR6csYSWfL3X9iIjfgoeGfgqxpxm1xfbwC0ujL9c 1or8LgOx0dEnZbWcT0l3lPfzEhzDsa1q+QfyD4Xa8i3COkE+gnKlVAZpXJRYk0Pqn1zG 9FII7pB8tbanHJvVEaMXoW8QA6IzXG9/yyO7tHh+TQDCzVEDKjl+hx5NcUBYA68k0NRF wi4d17GECX9qimJFoMw5NOjMnarE8SW05XCEprZXSUR2d7V8jaAcMkoUV0NE/QBfxTlJ 749l8TQq+mAjYLVRb5CKZQfSozKfpONExOO3Ai03yyl+C+jv1nBg7co/swCCjfkZJbFV 8SRw== X-Gm-Message-State: AOJu0Yx56wXHpIPhOgL1auYTUG6HtiRlTd1xDF5w0WzPgAapHDkmyHk6 /D5UIUOLS7fFqHiTFa52oIG+OOE1gFrV6H4UWjkxukPwp766lwO+somwjancvY0= X-Gm-Gg: ASbGnct7PHZgPeiAq6h8tA/m1mO2JYL6dxPu2Wfgp4dPgY4AsM2NCSyDlVeMbKvPB8T rl+5BYEWqKA01PTCyoO0w2T8LQrDCeXlm9CIWNlYBx4N24SVMqWpQ7xO+cjvV2H3TGgrR4puIBT uOD0qQWu+OK708/dd6CXnX9F7k1uA4w/NErXoeF24uF+ixs+Gwux8tPpuNs24JiVL+OEvvxVNN+ hqeEg+/rEVKkzN75hvepxiFX2x3cbjTgmlojCWlzpJV0Ke/8sHxAQnGoViWZrS8dPtXj/wgWM4W LPK+Icw= X-Google-Smtp-Source: AGHT+IHMa7LI4WGSLGkvv9C41mraaGs2HEssH3thQcPu9mBPeu8zZOetjC39qLEiF1U3WgfAIKAHHQ== X-Received: by 2002:a05:622a:99a:b0:46c:a0dc:efb1 with SMTP id d75a77b69052e-46ca0dceff0mr162749481cf.34.1736820926837; Mon, 13 Jan 2025 18:15:26 -0800 (PST) Received: from localhost.localdomain ([70.49.151.101]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-46c8734072bsm47572221cf.33.2025.01.13.18.15.25 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 18:15:26 -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 v10 1/8] git-compat-util: add strtoul_ul() with error handling Date: Mon, 13 Jan 2025 21:14:53 -0500 Message-ID: <20250114021502.41499-2-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250114021502.41499-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250114021502.41499-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 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/git-compat-util.h b/git-compat-util.h index e283c46c6f..f2935750bf 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1351,6 +1351,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result) return 0; } +/* + * Convert 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 Tue Jan 14 02:14:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13938300 Received: from mail-qt1-f169.google.com (mail-qt1-f169.google.com [209.85.160.169]) (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 CBE6A1C28E for ; Tue, 14 Jan 2025 02:15:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820931; cv=none; b=joacVDxdfHTo+1gmF3QxGVIyyrM9cYYrALX7IwCj1QQ1TR2XmbBJaWebfjVLEKNrAgaKczF2gp05UkfDotlo7xT1uABbXAcIFPUCJmTQnPooE7C7cn7nIKgiPr6k4xgzm8MCm1pZ1YmmSs47korjZpphvWcyEu+4DI2eYl9Eztk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820931; c=relaxed/simple; bh=+h0HDFo3bdJ4wHhGp2kbQQEXVSfQJD8tNPFJgwAQnqc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ar4MJZfM2BRKylQN/7BB0V9/LWEklCiRKelP1ZMKUOgX5xwE+NKyOqFzXTR9mtCluRJJaxc/wbLsxUfHC8Pr3pNv3PP6JPA3HT258ua/oyjfCVqsJc3BdFLMi0zO4i+0syTMTgvlvygzM4fveIB9RETw1IXsTmQ6l7FR1f0qDSs= 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=mlZHyNrp; arc=none smtp.client-ip=209.85.160.169 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="mlZHyNrp" Received: by mail-qt1-f169.google.com with SMTP id d75a77b69052e-467a1d43821so51034491cf.1 for ; Mon, 13 Jan 2025 18:15:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736820928; x=1737425728; 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=ERVjA08E5HyJCLNIWeU1AsRWjK8rZvD+7prWlEyJSYE=; b=mlZHyNrpqObm8O7rN0hW4AOp9IK7I+23PisfVHGPgTKP2WSr/Q5kl6weCiKeUKHj0q FIo5jyDuybFRw4FIlkH0Z7FPX+37O9Q1wLPsl5Pg5B9SmBeKCoFg58u3s1lvLjcRuRJN xWJISAS1DFrGPnm0KdkUKM+gjeetyNOKrj+Z7rD3rGcHiR5bpCiwjiQrNucx6mmX+5yl rOp8fU/0zD/1SQJm+53NaLRIY+TAS23ndWOVowXm4k8R4lk37TtLaFP3MIE/DF79Tjgh b/C53lQVSXeS8UN2VdrBTZ49M8xa/5FYRR1mDK+x422l9qm4cEKSAqkWS0Cb9aIyXQCC 2+Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736820928; x=1737425728; 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=ERVjA08E5HyJCLNIWeU1AsRWjK8rZvD+7prWlEyJSYE=; b=ps9vLWD1yIIKR/DS5NbbTUgq/M0z7G+KaOYPv2KqUNDS/KJGo6+VtoQeJ4rllDYJ6T QgMMdkgDbk4VtqgDPamaO6u8tZqA8JGrx3hczvoyh3+qyBLDb2JfDcaiBYZNtaAOiyVk vBEtxL5MvGG3FhQ2qDEIpIRoRI6tNHXkZA4ItaMIKD5wUI9sYeb2fvpo3aI28Z96amq7 aBzM72eIKuryU4HFXWRT3xVw5phR/r1SJVeTJTVAyRVW7LyJ6FQ+xwHQEmM1MxBYsSqM kM10BVsuPQpOhY+GR5ESGuDnxBTZyy1zJsIphkq6rCq8AUUiT2dDAAmktfxcT3jjEsNN 2s7w== X-Gm-Message-State: AOJu0YxX2W9Qk8/5MuYcGHljO0McehmyscRGs1SPA/ooJo23IoquMl// XSRv6Vh10TMdxmsGzAyPkaxlvlz8Ic4Arg15c43qq+TFr8cH9IH5koRYYavHTSI= X-Gm-Gg: ASbGncu80x5mRDeAH/7+Mg+TbJGBoMyEKTPWiO7GGKjjkPvPtNb3OktlYOW1mJxs9Of XaqKBP6DfkjmvHZj42KJFxebMpePgVOTNPtIBEzj0sXzuEjgqfoxb9HKkX5WkNnnJ6aEa5Dm0Gy eH5BmReEKAlxojBm9rx3OUWOv8XnFuT6lUVPB6kk88lgJc6Gg4LaLs7tD+wMUvuaKFNrMJ4LeQi 26mKiyfdIGsBPFyujC0OAA1uJVZUja7OWjQ1T+qjzVRmmi9HIVYyp1fZFetpB7QC1ICu9WS8bKn K5fbKz0= X-Google-Smtp-Source: AGHT+IH229NBUJdV/rLBxr+EOzlIxIkqCwYGciWVp16Tq/6mpWrAtALenI6tTvdfMCNkUw3EiSYnUA== X-Received: by 2002:a05:622a:199f:b0:46c:82dd:9a64 with SMTP id d75a77b69052e-46c9965b2edmr202566161cf.22.1736820927943; Mon, 13 Jan 2025 18:15:27 -0800 (PST) Received: from localhost.localdomain ([70.49.151.101]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-46c8734072bsm47572221cf.33.2025.01.13.18.15.26 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 18:15:27 -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 v10 2/8] cat-file: add declaration of variable i inside its for loop Date: Mon, 13 Jan 2025 21:14:54 -0500 Message-ID: <20250114021502.41499-3-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250114021502.41499-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250114021502.41499-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. While at it, we also change its type from "int" to "size_t" where the latter makes more sense. 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 Tue Jan 14 02:14:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13938301 Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) (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 2E7F742A83 for ; Tue, 14 Jan 2025 02:15:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820931; cv=none; b=T/u0okGYQNsoXbL644dHcHTnGgzRgiKLI3hG087c3BC1el1Ise+1xy6T2uEjLljBSWfMPnYFAl7LLVU6kl8CZQKJnxyhE8jZ3eS2fEQEf6yvn2EveIgFcjobXB8ElupVikfctqT1/wGcSk6Tb2gCjyrXADoqeBQcLvWqqc9QTww= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820931; c=relaxed/simple; bh=JBseXTGtYYYSUPAkETCJyGn4PTaA4oKacv//sGB4tDM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VxbSKf6NsSrWoa1iOF8KctUoDXAjW89vFx/DVyqbkl3WGkM2AnhBOjkxGf1iE01nfVZnJ/6trhsN28D4LYTUnTc5VA2iIpoV8axNoOx07eIFjVycGDl4f4vGkNmSlmKc8c0d4Ty/ZVGrayLONtmrsxPWel9xaNs1rLAlL7LPW+0= 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=PnJFPnS8; arc=none smtp.client-ip=209.85.160.182 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="PnJFPnS8" Received: by mail-qt1-f182.google.com with SMTP id d75a77b69052e-467b955e288so54172251cf.1 for ; Mon, 13 Jan 2025 18:15:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736820929; x=1737425729; 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=yy7B8mZFPNWDE+kBcaWGW6IVKEy4mhI681qTRUCjMpA=; b=PnJFPnS8Vsekh3l6BBOUJOh13Z/zs4GLhFYYYoeuuOUm98h/wIu0RimvC1oUGVHKqE K+/s46NVyd0UBDIJgXgo04ODQ8CIFsiyPx0HOSq4LZtcx8L26iZ4SpDCam5LONWzSRYz Zm/jguSlFjMrWlo/I6OpYfXVT4bnmEkBKa0EyPAVTMfBMH6JUDMkX35rDMvnd+tu4iQN +y8iviebyFsQrfbcZquBTWny5BO9sg+rqQybBt9fbzwkTD76jq17PIMPlWwuBvanpK4V jCYTIiXVPgeJw8iXT/MGvETbRHzD3NgTEF49UDPRQWVG+H2MLx3RF9194vu/IhIBXeqD 193w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736820929; x=1737425729; 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=yy7B8mZFPNWDE+kBcaWGW6IVKEy4mhI681qTRUCjMpA=; b=F4Z/ex4VSnN8psLAeqA6B5q+Wbh1ZhMAbZ4A21XValOJ2+Y+yJUtJZm/Zt/u4YJ1ox mtrFH8a/xqGImZVolXsvSn8YxRDQWYpds/mfzyH+j9eirlmwLleeALwVw4VSBn8GAYeZ wGumqfKQpYMQ45yQCQRQMmRU67KPTkCcoSZoNcvs1ZTnOQo08ClonayUApfap8e1jbtY pMmskEi7id9JNrvBqQlvcxbpc/puIBngZxGgw4zkkn7EluYNYWZNE/9Ilm0U7KOIkCNQ 047tFQyUDn3dhAEFmc44+J0mV4PPC0vxKsZ5jlYaR5wubcDuzzMvNeeuL8hbvvrhCWX8 7fEw== X-Gm-Message-State: AOJu0YyE8snxmTUcEgnpCeYIFEXp3via3bZ8kMliGV7LBuyb7ZULKNf3 cLDi6povGG+ucAgIOXBISud9Mm42N1+OqZt1ytSdctB2r1k6Ohqc8oensK+t1xE= X-Gm-Gg: ASbGnctgR3xvdPg/5uxJ6jxLgBoHPMRnReNvtf3LI1sU+k+drhWpsEiCXqP5jRHY1L0 kplFPSWj5OeoPI5hDza8aP1hP4V8fZwHtBS1EPjPOuoe9i0UOTR+gjSEPyXRVg54RkOKUbS3YGj ZamQXwHud9snz6MU44phsYroKSFoLzDzfdPSxJzUIPfPJR7EHAbOvljxLibKtqu5BZv4UowSV5k 2iiJldqB939gQfjXU7dBtoCghHymZdjsYGZ64gGIYUbeNeb6OEobZRxBaMPKi3oTMrxiGyuVqd7 +5c7ryI= X-Google-Smtp-Source: AGHT+IE+cIyos6J/GbooRDUYX6/t1/H4VNwcKDynnaBOfUWlMdn1wtYwQuoVktiM50spyERHM43sTQ== X-Received: by 2002:ac8:7c53:0:b0:467:6532:2cb6 with SMTP id d75a77b69052e-46c7109d6e6mr346802711cf.48.1736820928907; Mon, 13 Jan 2025 18:15:28 -0800 (PST) Received: from localhost.localdomain ([70.49.151.101]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-46c8734072bsm47572221cf.33.2025.01.13.18.15.28 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 18:15:28 -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 v10 3/8] t1006: split test utility functions into new "lib-cat-file.sh" Date: Mon, 13 Jan 2025 21:14:55 -0500 Message-ID: <20250114021502.41499-4-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250114021502.41499-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250114021502.41499-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 script "t1006-cat-file.sh" into a new "lib-cat-file.sh" 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..44af232d74 --- /dev/null +++ b/t/lib-cat-file.sh @@ -0,0 +1,16 @@ +# Library of git-cat-file related test functions. + +# Print a string without a trailing newline. +echo_without_newline () { + printf '%s' "$*" +} + +# Print a string without newlines and replace them with a NULL character (\0). +echo_without_newline_nul () { + echo_without_newline "$@" | tr '\n' '\0' +} + +# Calculate the length of a string. +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 Tue Jan 14 02:14:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13938303 Received: from mail-qt1-f169.google.com (mail-qt1-f169.google.com [209.85.160.169]) (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 BE66E2309A6 for ; Tue, 14 Jan 2025 02:15:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820934; cv=none; b=I1LlZpVf4NlH4cY291cg/9vsgm/hmpfWyCjtvPb687nbCW00sr7cV4JTobXUTXODVsdJBCJwL0W/fuRGduj3hERLB5qmbwXXW2uQMZ2GMFJi2mbYDQmiTgwdeI1RHzOuHJYW3VLRHFex+OgAWPuKrPo0OqPnKwrEfrt60WTe8xw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820934; c=relaxed/simple; bh=TKeW403SYch7/jwTdE0FvvYa/b/yhmiSoma1i7aYmpc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LIDbK0OZD5N1sg++jlv3w66KgsynitNjAzZ4yeV5ZWzvGk77dmHhBdAJyV9wAgrBvQYUW77H2wqbryGXxb8NgkUuKcLKCp1gMhi8Ai4hAsM1pPA28fW48isx9REc4JjxggwZI5h6hgQ4expjnrO4NcG7JWJeB+GGUApC9+IOw7M= 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=UinajRWf; arc=none smtp.client-ip=209.85.160.169 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="UinajRWf" Received: by mail-qt1-f169.google.com with SMTP id d75a77b69052e-4679eacf25cso34379251cf.3 for ; Mon, 13 Jan 2025 18:15:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736820931; x=1737425731; 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=hQ5QIlB3zqVmw9f06PPifm9yITGcgL0SCiC8QIzL+Hw=; b=UinajRWf2NaHZXx2xUxgxg6e06gom3ZyIwyyyVQzC5IYWbBbNkYOgNTRuaI1MpH5H/ kNMiGWZSs376YpLj+KD6rzoHZTw5FFYMjFsUIGCcPvw7+DcG14SWQr2Va/N6xWA02AV7 iByrlsr/a333vVrSWAx1QqI8M94A5WttbwfoVktadNxsEKGEhppRbNxV6pyJER0k0JBv RRmSg01g6AG6sCmke6zozoY8XCI3Xuya3sPb/EfET8uzj4Mqq0K461ilrkUJrWuMngmb pYcqmnn+0ee7xKgSi553poqHDJu+tIeT2QM0QMyN6Tl8gvNdtCIsjJ8nj8e19qF28ttU fvRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736820931; x=1737425731; 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=hQ5QIlB3zqVmw9f06PPifm9yITGcgL0SCiC8QIzL+Hw=; b=ETjazrAP1XGT1rJETKkXU8E4fGz+n1VQa13MM/Eu0ZMI0N5mDtYQ7c/0li6eMEZMmE OQM+S/XwiXd6wuxEHm1ssL7y6xGLIwOMCZCpm+kmpVKSRFYlzNGioThzUzZu1bxa6QLI ahoLW3CmQnxD56dFuI5gaLJXIkQo+UwpCrb0ujp0KXioi9903R3y9X//FYUoKugienwp ZbUm/g0nxd0e7PbFd2eI2wJmNCpvBbIq4mjW7Siqln3t8DjsJ7a5VbPT1UAJ1PFLuHMh HN1RKPSRKWT8AGZATISWL+PsiREF2WOaw3orJHna1CtQY2AmWuFHd6iCzhS1SbrB3bew 9e0g== X-Gm-Message-State: AOJu0Yx/Exk9NEoxk4at0VlDPWYHXpj1KYfE9cnX+lmLzFUunSVj/oa6 kPSmYWtsZVQDNw5KdUo+o4zUm/VQm4fKsSLhGca70Vkisa5aqEbDzAvv/u8V/fA= X-Gm-Gg: ASbGncsQ29QZ7fFLTkRNXXbl4p3eXFzwq+S+d4mnluFlmVqUslRCCUC09l2VMLbehMj LNjnpxQmKfssMEs4a1zhh3Lonh/baOpU4cI6TEMuu95GWzHJ1ejwZURj2KW6uaWLVNR0ODJk15U 3Hwy5yGqdyzZgJUK8bM2OWr7d8hxvy8KWxlnl3w8o2G1e4i4/y0HFiBkC4I7sPiEuKG1/XPdxDY 78bq5dByiiz9OozgkcVxAfgzBwrZEEgevb48JOXAjzBsFD2wlVIrzy2qlv4MxpPKGMGdek93gt4 2AhQ9fc= X-Google-Smtp-Source: AGHT+IGts187wPbLsFnx+4rIaQSaBcNOmtM64b3iM78u8D8qFepiGYXTBNZfJdFg2/ioM9n7XzxTkw== X-Received: by 2002:a05:622a:144e:b0:467:81ef:1953 with SMTP id d75a77b69052e-46c71079ec7mr297522241cf.7.1736820929976; Mon, 13 Jan 2025 18:15:29 -0800 (PST) Received: from localhost.localdomain ([70.49.151.101]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-46c8734072bsm47572221cf.33.2025.01.13.18.15.29 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 18:15:29 -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 v10 4/8] fetch-pack: refactor packet writing Date: Mon, 13 Jan 2025 21:14:56 -0500 Message-ID: <20250114021502.41499-5-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250114021502.41499-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250114021502.41499-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 Tue Jan 14 02:14:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13938302 Received: from mail-qk1-f176.google.com (mail-qk1-f176.google.com [209.85.222.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 6977A23099D for ; Tue, 14 Jan 2025 02:15:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820934; cv=none; b=Csye0h+iHC8Hh2q6VC34zVhli9fIAuQ8jTZWJ4+FtvC7hXv7bagh4F0jKBkxEMldpXZyuTSDPX2ChG8GUN+2uxiY2qLzuKtcqrIdRyd6WS7+Pwweu56vFj1Tkp7CunRcG0vhT791RFKtggmd2mg4Hl7C9ThQ/pq1pv16LC/3NQg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820934; c=relaxed/simple; bh=oy+iaXiMmM84hDo7DxxaWhndetO6EGObYaamJNh+o9E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TjfQsMiPYmTwMdrCDPVUN8/U/0yswTMhsoVmvlcKkGOr3J1gN5hYA7D5h7H98GNIG0mix8DcxzfOntFJgmaf6UMQGa2m0/4QQJA+dNFocWHDveyL05TfnpWNU0qsRBazEPr55suEW4uW990Vce0BBU/sb3sRRCTlbR+8mGpLMyU= 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=GH6ygbDv; arc=none smtp.client-ip=209.85.222.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="GH6ygbDv" Received: by mail-qk1-f176.google.com with SMTP id af79cd13be357-7be49f6b331so12806485a.1 for ; Mon, 13 Jan 2025 18:15:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736820931; x=1737425731; 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=Wcwduh7LRa4FEws+DIm/+UbfjxIpsiyJp4QGheW+KI4=; b=GH6ygbDvKEJC2CArpG0eP0UyGmmL1dX8E8Jr+BxoqvQ+q/3+iUUBBJaM31KjuXigW3 bXapMHh2IUNjZdKakx3Pnb1XEppGYtIoBgRweEmcbHbiUdl9b/5ulku+88ydvnr94enj FKOy9CU3VvkGDshSMfMOZuFBAA3rSPxAgdScHsMkj2pGmkB4YrjSHQ4qv5pzqYcYYCu9 Az4aaKFrpZIl5V0g1D06STDY+AExjZ+x7F/+CdN0cZZcQbI7o/i3QCwYV6T8Uib0NQfr ckvg0LzeCTPt1co0TEE+16w+0TMzzJGxRmYSvdBtq9MzjRtixRmjwmqifJn+NOtUcq9P EvGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736820931; x=1737425731; 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=Wcwduh7LRa4FEws+DIm/+UbfjxIpsiyJp4QGheW+KI4=; b=YPF7HfmbWy8FTk4+gOtRx0St8DX1BHCmtwpZj3lpzalJpb5iNxaEeDkxG/QfHDlTGE JEuYlugLTVcwV/eJaSkldyOIsgoKLkXgRn5QeFzTsLpXjSOkuL90nRqw53O3kYsWCpiM ewyMKhObzXAsWeDjxz3dpPHiZ3q6Ib9avxNSRE1J4ZJC6BF4B6UM7tK6W7KQb7dTzOq/ JZDgTB/uhhE8uRSrM7CVxRH8VaKuZJMeAfcf9KilIGh+aXWH2uH9h03TyAWfqE+HePjc x2d9I4lcS4UdKXnGRgw7oFVl+OZiToxj2mZehp5s9J61Uc8MjksTr6CL4LievHjmFP6w mhZA== X-Gm-Message-State: AOJu0YzSdLpaU1Kf0DppZo5i/ZwMCnQyW75tTyzmvEEdViJ16G7T5+MG 2fnWL31Xb0CitQ58EQiV92JWn9/ecAzvySSQ1bZJSO9mfBwMa7jCCJw8+Sfq4GI= X-Gm-Gg: ASbGncu9OIyZ7wujBHLMrCASBxmydGYdQElrdED/yQdbVvRos97HOBf0W+n/cviMxSI MI+6CPRz4YIC4JHDiQymoDTSFv5R6xTrmivzesccpiKSIXtLND+jgdtfyFZQvAn0KgHMnY0XfEq o5a28Aiwy/xxkcecuitc21hr3/FN6tfy6BlwOJIM2xKU6w8HwsLIi3M+2P1N1/gLauWYrSpp62l +5J57ef8yd7goeNfU+ARwpEsepCJ08gqWdv0q9rYbVR6o5brhMN481GfFZlybPYLFqGnhYyMre+ EgHxurU= X-Google-Smtp-Source: AGHT+IHc8Njc30/EFpbbc8t5824J3FTWQMROSOWTP1/T/hEpQpx937EFFomtWD/pKEFffd5eWZUbBg== X-Received: by 2002:a05:620a:2444:b0:7b6:d8aa:7ef8 with SMTP id af79cd13be357-7bcd979e1eamr3123553585a.37.1736820931015; Mon, 13 Jan 2025 18:15:31 -0800 (PST) Received: from localhost.localdomain ([70.49.151.101]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-46c8734072bsm47572221cf.33.2025.01.13.18.15.30 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 18:15:30 -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 v10 5/8] fetch-pack: move fetch initialization Date: Mon, 13 Jan 2025 21:14:57 -0500 Message-ID: <20250114021502.41499-6-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250114021502.41499-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250114021502.41499-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 Tue Jan 14 02:14:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13938304 Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) (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 6E5B642A83 for ; Tue, 14 Jan 2025 02:15:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820935; cv=none; b=Aa3cP/FdlTHyTGUDYRy/edVtKP+cp54KZyQYf3/hc8CyD0em2pc+FBEtfNlO7r/OIRLchrub7HQ3tYJXv9qX863MF+9GqaUJiK2lsV6GEC+712uXiJcLogExftU5DEzlU9TJi7908udtzi7lxB4i7wuZuhRYIKXMz9DdQOBWRSM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820935; c=relaxed/simple; bh=jrSA3HlbmH5BEYN9N1c5+RtLrVe5ydZhdTV3x90T1/c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VWLWXfEVAazXO4q2T8mevOa4ZlIynNytzlkj6yKqHX+B2PBSU0YuUbcavaON8aCCcaXdytOWADQinW1G6M8FF/J3LYg6yFDiYPVS1Y0UomPSjYJdq9BKsVXaixaMm9oVBV/kKAiZE9gEwTzl+DDZl4CF4rpXVo4D1UEwj21qUvI= 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=ENbMVpkP; arc=none smtp.client-ip=209.85.160.182 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="ENbMVpkP" Received: by mail-qt1-f182.google.com with SMTP id d75a77b69052e-46c8474d8f6so32383191cf.3 for ; Mon, 13 Jan 2025 18:15:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736820932; x=1737425732; 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=F0Apue2WtnALJeyxwq7+yCN6CrIFpP2E0UXVBjhqnaY=; b=ENbMVpkP33k3xzhOXsqnFhERbAR+GwQ7itMztyQqBEJFQkkSnn/OvlbRf7rO96xiap FUojg019llkz7U4W3EHkFXYOTbfA1OS/JCgVCNW4f2jlAO3l5YGPPaPsqkHYk1IitjPF w8jvQzD9r/I9n5tyGLcmwGc6PqJ9qDyciq84hAEwLzcthPF14i3wZX9Js01EXyYhcT6c KirA/red7Yeyfgpg4Tddd2V6hc0beWGWmNsp/z5AruNBWe24eosYjEdAPAsUHgusss6+ Pm+VOUwRjUOQsg5N7S0YoSshGFKXDjfzav/Ivk49VQenI/7IosGtQaXlv/Ysm0Z3/zQn rj3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736820932; x=1737425732; 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=F0Apue2WtnALJeyxwq7+yCN6CrIFpP2E0UXVBjhqnaY=; b=vsegMEmcE8lPqDUW0noWcsP1j/JPjr+rYDokYO61pPXFEFER3FZwuL4VEd3QzLxJEm qgWPXjdmb8W7GLGxZ1eGvJtWUSrPWG5pE7Z/TsJs8RjwFpUaBk/wcL/P8tI0jd/20Ypl 1qSg1cxwu0MV5yPdzy1UKLR0/QnnUOLbZ6K53MXN5xuF2KVMGPWJRCVBJg0OvuBx9ryi TXAJd4u0TkvopljCk4DU5+O0lWoPzI+uZ7sLwoEFl+gkWGrS97//c3WccZ20lRmGekZi dRezc5grHHpf1easLA4Iexc5nkKfzWRuR+s+tvgSNVSChFwNa8slxbnKQYF9hxHk1Hnr Pwbg== X-Gm-Message-State: AOJu0YyJ6GuhK5jYrnSl/uqtU980s7ASpTAXJRuxbLCzE/h7JcRKj7Ue 3d0W5YqjefkoqMlKiPrb6KlWMlEU+CHOZmbqrgREtSqmlyN3iYARbMCc2edgngc= X-Gm-Gg: ASbGncsQDEHCcH/F7u2G/MEU/dcFFwYILveRrgVo+wCAyTV6SXhvjbURMJSgKA9fPLN YOUyLiuQplSZXODWhZ2oV5PKvMQvbY/ugy5XPUl57Lyz2tCc6/go/QI7G++gbxkrjJN5heoDN0B gfPgP3K91o84wT4U/OTeue50rcaNOssDn/+No4LBGJy8VlbE1GrEOmhIWZTQmM9ru9mCFE+sBb6 BnHtSUqSV6CvozStAxgvAW/PcFrYKUVKTKhZ9wiEwyYk3l2Wq2TgQ6ZRGyhQ5jm2ZNVks7Hoe48 k3EK/Ec= X-Google-Smtp-Source: AGHT+IEVs1hJ1OfdwmR3wYS2+fcfIqV0lblQEVBtEqCeQUceuFLjh1YArrhecaXBp8NT1oNeqAAr9Q== X-Received: by 2002:a05:622a:1194:b0:467:4bd4:866b with SMTP id d75a77b69052e-46c710e5840mr354567421cf.35.1736820932241; Mon, 13 Jan 2025 18:15:32 -0800 (PST) Received: from localhost.localdomain ([70.49.151.101]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-46c8734072bsm47572221cf.33.2025.01.13.18.15.31 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 18:15:31 -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 v10 6/8] serve: advertise object-info feature Date: Mon, 13 Jan 2025 21:14:58 -0500 Message-ID: <20250114021502.41499-7-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250114021502.41499-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250114021502.41499-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 Tue Jan 14 02:14:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13938305 Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) (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 8CEF22309AD for ; Tue, 14 Jan 2025 02:15:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820936; cv=none; b=Q+ZKIt2/HRphNILZy4vUCESruLUtasi+yGrKLX43MvkZXAsauWqxKdyYYQ2G26LvsJBNDiYgk/v2rCfqRhuGI6i8zhiDjvDE0MI5YrZNxGsKtaR2hmGJNa6QJbUvqLyeX1HlgpIFNmWQZdpaJSUIjKOpKxDRPHxA1++SC+jsK4o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820936; c=relaxed/simple; bh=b2hxqR7UaoO30Kna58NK2iBly7BBguQax25eC+vtook=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g7mIqLXbXRrm7j3v2CODbvkH/Awm7MNWjWSZ/ZagRSGOjfUGgfFlKKRvz4q1bsnCWPGyPnPU/6EHnTQ0XmDS2ThIjLqpP5+0I5TB/mZVPMxS7THFbHD0XZzMSWHIrF/A0uIMmvQM4+VEa6l6w5GRdcj/OD1+KCdmS/tADZ3UIxw= 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=lxJwV/zW; arc=none smtp.client-ip=209.85.160.175 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="lxJwV/zW" Received: by mail-qt1-f175.google.com with SMTP id d75a77b69052e-467918c35easo64860861cf.2 for ; Mon, 13 Jan 2025 18:15:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736820933; x=1737425733; 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=GMQemDx5ad2czyeZzepm6s80VacXt2xFbeAM8WEOznw=; b=lxJwV/zWjPmWndp1hNtLxeETRGPhdF8hSraY0AvukA02f9nduNE3CZ1pROyIm3X86q CISTKK80v4yfAsbValHh7UdDV1/2RWpO6u4iBsKxR8lgT9zZJe/sO71vcQG+NOOSGDx6 hL3eb51YbAkclK/doDNbPR3lqhgQ/LGeRnDRm5RyXtiitSPS3C2Cte0Z9huOnepwgayx dRefC2b6dmUb7MDURFEVSE+QkkA71VX0C5PQJUX2FwAqeKDZTO1U63txN3oHuzu+f5pd gu2GU8T+blLt2bDUe5lVYKZCI2d+IS5BVWyFG0AZiCVwKFcVXLhqKw4hAPNMJg0TmCeS oY4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736820933; x=1737425733; 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=GMQemDx5ad2czyeZzepm6s80VacXt2xFbeAM8WEOznw=; b=NJuLV6qUDW1u9l8ffv0T8uT55qUPBZI3sW1ISyIxCn+N6+cec5J8fXld7RlTNuq9tl +CnpceH94WBehR1J8UqSzfMOvke4V4YxfTtYqhvRbLZlqZa0Lazh8aG0M9poVSliC2u0 YPPav2B/IeX5l6NUXAGaxEbbv8+qJqOZ5VLh+FxRmQgghZtFNT9Z258aZkqIZEELivTS NnM74mhG+Pp6p4bGmoraq0rnxiNXjjGmRhrWL3bYuqOUtmat5lSEwgAJeqTmOrz82UL2 XZxzdMO3yzuoTEKeAPQLRHEqCB2i0G/JkwfQ7XUk6D6n8r/4AneR8mUfiKD3uFQtOMER yHGQ== X-Gm-Message-State: AOJu0Yz+xaQdrRxSojCD7SV0k+z6Mr3TW/ehn5m7eUxRpwyn8Ev5uYnV vlr66dZyMJOCDWuMJuPhTLtDKFwXRi9qMKxHqsXopCKAuAfZfNBzyQg3AWKo5Po= X-Gm-Gg: ASbGnct/vmHLZC2dWQ4HTBzimgsGRDXELnn6iUJBEUVV/XDaG7VXRbEK9UnG3FK8IW1 jib6fU3xMnVm5GRlIqSbzu6KK6sJgAiQVWYW7gCn4LRzdEciQRBRNmVEYm3LMie4iAdgVpPB2vy vhHE9ozKXQqINk325xrI5nuoUXPwmi5PccVds5RItlKeUowMItMQkku42Sy/wothkTtRBvX5YJC 4cSImmQr1xTfTmMklVR+42Mnc2St9/I0rtYrq+E5RIzxQpGfM6ajWU7IrYsQvqQuXfbeiBs+QQB ylIp0Aw= X-Google-Smtp-Source: AGHT+IHqmoXh8ch1BDDa7F4lh2o15kr8YuBzfaNh1Ax1xJtRqNCVJjq4ro1g9OdsOCJC19CIjmSojQ== X-Received: by 2002:a05:622a:1a0d:b0:467:7cda:9388 with SMTP id d75a77b69052e-46c7102ff29mr407206211cf.31.1736820933350; Mon, 13 Jan 2025 18:15:33 -0800 (PST) Received: from localhost.localdomain ([70.49.151.101]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-46c8734072bsm47572221cf.33.2025.01.13.18.15.32 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 18:15:32 -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 v10 7/8] transport: add client support for object-info Date: Mon, 13 Jan 2025 21:14:59 -0500 Message-ID: <20250114021502.41499-8-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250114021502.41499-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250114021502.41499-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 Tue Jan 14 02:15:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Ju X-Patchwork-Id: 13938306 Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) (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 D70ED230D0F for ; Tue, 14 Jan 2025 02:15:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820938; cv=none; b=mDcq8KPyT7p2mr305HAQGwHWs6DDDMFUft+s1Rb65KjOJodkodL9nQXg+WIUXpjL9+5Ti7qk+DvCh3MH6GQbk3Vx38Y95wJ3yZUbHUiW2gxPT62B11qFe+KWvSmoZKcIIp3eRJwAfcyp3a+nMrDwMOZnV8Oztz3YQk/eaccgJLA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736820938; c=relaxed/simple; bh=5Cnp41noeWSB1EilsRMMRHJEr53GkMiU/6oVXh4qJBU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ow8f9dGDOtWGlD6Di+iIrQfiF8SMvl1326Yb5fiokj4WvLTRRnU/FFHhHHbJiGL/o9d8jp8BYzfLjepVqtorUoU/U8Ml92UL4QRKukdX661eRQAc9+VG740yNiz1ZbTsE7KU0pf70k9GWukOg4weUUNnRwIscSBSYoukHNVR29k= 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=c/GYI1MN; arc=none smtp.client-ip=209.85.219.52 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="c/GYI1MN" Received: by mail-qv1-f52.google.com with SMTP id 6a1803df08f44-6dd43aa1558so44819796d6.0 for ; Mon, 13 Jan 2025 18:15:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736820935; x=1737425735; 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=MeUhxDYZ3ltTPKDzGplommRPKaKh7eHbf5l2goQvpyY=; b=c/GYI1MNKE+k5sXthOdmGm8E4Nm3Lwsdzww8dWBja1xq929tOq7GLDWJX2wLV7TiO0 OuuMKCbdKk/GFRpvI8BMZQn4Kz9oZtekWu4Z6CEB6WeZdaJNUhlrbVIPBg7bdUIwsGMA Lzb8I9E8p2XAe0C9p8Tbpy/sADIE0AOR4ZkqgBCajftTPYmYLqDV13MQIVRoWTgQYVQ3 rUapeeaVU2I5JJw4a3zc0J88gCdy/HsMtLvoYIW10azyLob9A7DdWiD191avmHODwO2A tfWT7n9z4V2F0n59CQIqDNy740AnU8A4/PMnM7UUBDq/Xso8n2M1cR/pJvy/EVvANqij EhBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736820935; x=1737425735; 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=MeUhxDYZ3ltTPKDzGplommRPKaKh7eHbf5l2goQvpyY=; b=RiKjNYQv30x7W1wrvoDY4+YCt1NeRglTkytfbOVf+DiwcsIpdEMyU9PuKIVlJ1edwl THzYeTxRxQsMVfQEAWjAAO1q4tTQ/Efeq+xn3pdx6O2d8FgjLK4RWcM//L07wStZ0PQR uMEg7RXspZJ+qggsjgYfGqONKNnoZMd07Ak3uCc6lciu065a1qagrUhNTW2kn8KACEs9 LjzMmlncCJaruwz4tjU1Dr9f/6pa+uu+EmCUneiztzsUlGZjUY4hmolZU6VHfK4QsiyQ Q1RGoG0DqqOd2xDG+T8/kOUv1Jpd0oskH3yDfC4lgNIOwb+RPIBc5HPwtNtNsgSz3e8o sFJw== X-Gm-Message-State: AOJu0YyczU7NLnUIsU4SLU9Ieyp3t92fye8gfC29IKRux+x+lVbs3rDC 6feHXXP8Xgi6xkmqppB9cSaapHm2R12mSg7H1PaFsowNvCJjSSlMjppDsifmcX0= X-Gm-Gg: ASbGncsp0L60dNVzfNEoXiTh8YWC6rYlcG39KLUfwoTEml7Ka8Anh8Z7wy8BAf4bLDr DaEtPdRnSULk7UPL2kKRGnQ9ixZ42NH8mQtqXuegJqef7rHfznKSvZ15tJQRAqgAQNfpn2fMR3o jXwE9Fg7N12mUNRUYbV38RRZS2nX9AVTVV8KpOp+mq9qRg+59CLJT9RMveSEhdy2OTtW09zfoEx BkdEFf8sEusuTFQGOOF/ILeCS8EUnjdOLoUbA1ALykEv5h4Zi/Bcxz5KEA8kbenD/4eP/IVqYdI hi99oT4= X-Google-Smtp-Source: AGHT+IGeOUv6VFxgRiPGXVPMPdPcIXSwJPXaex5X3HYdK3mxow0A2GR3UkHJuD0dXG3jabQZODUuFw== X-Received: by 2002:a05:6214:19c5:b0:6d8:8256:41d4 with SMTP id 6a1803df08f44-6df9b1f00c4mr403038686d6.19.1736820934514; Mon, 13 Jan 2025 18:15:34 -0800 (PST) Received: from localhost.localdomain ([70.49.151.101]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-46c8734072bsm47572221cf.33.2025.01.13.18.15.33 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 18:15:33 -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 v10 8/8] cat-file: add remote-object-info to batch-command Date: Mon, 13 Jan 2025 21:15:00 -0500 Message-ID: <20250114021502.41499-9-eric.peijian@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250114021502.41499-1-eric.peijian@gmail.com> References: <20240628190503.67389-1-eric.peijian@gmail.com> <20250114021502.41499-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. The `cat-file --batch-command` 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 loops 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 | 96 ++++ object-file.c | 11 + object-store-ll.h | 3 + t/t1017-cat-file-remote-object-info.sh | 664 +++++++++++++++++++++++++ 5 files changed, 794 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..4fbb3a077b 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. + Raise an error when the `object-info` capability is not supported by the remote. + Raise an 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 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 raise +an error and exit when they appear 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..78b16e10bf 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, @@ -48,6 +51,8 @@ struct batch_options { }; 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 +584,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) + 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 +730,41 @@ 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; + + 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); + } + } + 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 +796,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