From patchwork Mon Jan 6 10:30:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usman Akinyemi X-Patchwork-Id: 13927181 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 99BF619E7EB for ; Mon, 6 Jan 2025 10:37:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736159846; cv=none; b=WVh5WCmtxCU1DsIzeLWfLXMAZ6+biGR8GqIdXpiEfDPAb++1Bc1WxzVhwXkirQqhvRT0KGjMbdqLx0o+QGrh+K28ZBC/2kE+5cap+VREK8nEWUb+M3/YFxwsC+Q6WXUQui4Xt6I84/m98ck3kCYoFNDEgpszFQ/GUQJDZk2K1hI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736159846; c=relaxed/simple; bh=4bfKYyQL8jq4bXVdyRKu2xS4lnHSDoau7SMTXmhJwu0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VKExaMwqoveEqcFrbKxnc/9pfQJ4ppcAxTEvhQOy4xP9hV33nnT8SrFGiSuTORoXiU7nhDfSvKNCAX5wuxPr/i5wI/6LbsWVlrDPG6l0q41fTPkFVrRUbsLcQzGeeemRMzOh8ZD+vEfmyS7d0fY88OOdLg4yuCKFwT5iwEP+vFY= 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=SoFontv5; arc=none smtp.client-ip=209.85.214.177 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="SoFontv5" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-2161eb94cceso142388885ad.2 for ; Mon, 06 Jan 2025 02:37:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736159844; x=1736764644; 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=5FHbSicMzoJx/HMYCpf0z6A+uZyIiYcJHEkn48h7deo=; b=SoFontv5D6rTEuzcOXQMQSXwSeY4UEjyy2G2jJHI+g7zxG1SbxpuP+SbOTKRxkAPc2 Of4ukmXjtDnJjCkYYmW12/8uqSHrDsGe4udGCB9rNY0VzhNri5HrpqrgI5QtubB0Me/E 0PSi3HVgVgJ1711H6kzNCYMdNF3JRBnvNbspNDEsJvY/umom4wNVCCKZK85/trcpcPah sA+/m47RZqN/d70VgXqJ1Yz6kIlghU8wLE3y5SOBsFc4QVcZAURk8uR+W/5Rikh/Pw4h hxQbDoZ9db2rXsltiYXAcvWd2RgCAGCUltupGreQ7J5l0Khqm2qFYga4V5KgZB/2NCoY bJ6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736159844; x=1736764644; 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=5FHbSicMzoJx/HMYCpf0z6A+uZyIiYcJHEkn48h7deo=; b=LmMlcfrPvlrvex8Nt79/d4IOF94aSPiOuGB5negJK9B8FtvLQXJQgBtOgd1PmBNmgY WxsamHPGQ6EeTJCTaX0+AVz6Cp3EvigCVA2YKn9zgQfgdRuWCX5jsicpPeRuMiuvo4my Wb0m221zYRgRfo9EzrJJhRofpHtgAPUKLNUIbGW4xBBxN+sq4w0z0qo/1rgSyEzwa/Et EZeVRCZdzzQxGMZn2XdYRcSAEH71VVFLFAw9Z+W36ilUo+dQxE9IqLX8WZGboml45pdJ w/NVQ1XGIV/Ex2cu7BAM7u7Tw/mttptlRZp/pzA6P07gQ3MYS4LKmk6cQ0LZy584UP1T P19w== X-Gm-Message-State: AOJu0Yw5QjpgY4Xg1CW4rbsezesZLloQrwcCRTqUI5Whtnhfb0j3iES9 TQkQW8VA660Uw50l4vbXJn2sPqGImLk3zfZegCFWUlVX1kw+DZsh2JeW8kRrwdg= X-Gm-Gg: ASbGnctJ8OkY4GZB/+j4xHW37hxn6EhZbt+X/Tl0N0pRseCEYkGhnOPwDu/7RkoeZtY McK5NwFE9v1XICAf50qV5Cw0t0JdZH2aq4mZiyr73BGfj7pGzbLs9lEHDkAcL+s8oS5UnCb6xGs WXdDfH5zuTOr+ogq5i5Ije+9BAFqwziHq16W0BVHonE6QVfTmqkwInXIjBXIPFpCNpboYKJbNe6 fLnxMoKAd1Zg854fmgI5TuXuTwIdwZO9RaNvtXcjMQaGqSiPqaIvswUShRfjqcplNj64GWN7e+8 i/d14g== X-Google-Smtp-Source: AGHT+IE/+KETGJTjEcn65cAgq3H7CQ/jTlbHG3fXeU0VrejXUkT+I9YVlRDubQlsnn5lwiZSziiSIA== X-Received: by 2002:a17:903:41c3:b0:216:69ca:773b with SMTP id d9443c01a7336-219e6e8c3d6mr871440215ad.5.1736159843678; Mon, 06 Jan 2025 02:37:23 -0800 (PST) Received: from archlinux.plaksha.edu.in ([202.164.41.66]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-842b85f00f9sm28774281a12.43.2025.01.06.02.37.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jan 2025 02:37:23 -0800 (PST) From: Usman Akinyemi To: git@vger.kernel.org, christian.couder@gmail.com Cc: gitster@pobox.com, ps@pks.im, johncai86@gmail.com, Johannes.Schindelin@gmx.de, me@ttaylorr.com, phillip.wood@dunelm.org.uk, Christian Couder Subject: [PATCH 1/4] version: refactor redact_non_printables() Date: Mon, 6 Jan 2025 16:00:49 +0530 Message-ID: <20250106103713.1452035-2-usmanakinyemi202@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250106103713.1452035-1-usmanakinyemi202@gmail.com> References: <20250106103713.1452035-1-usmanakinyemi202@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The git_user_agent_sanitized() function performs some sanitizing to avoid special characters being sent over the line and possibly messing up with the protocol or with the parsing on the other side. Let's extract this sanitizing into a new redact_non_printables() function, as we will want to reuse it in a following patch. For now the new redact_non_printables() function is still static as it's only needed locally. While at it, let's also make a few small improvements: - use 'size_t' for 'i' instead of 'int', - move the declaration of 'i' inside the 'for ( ... )', - use strbuf_detach() to explicitly detach the string contained by the 'buf' strbuf. Mentored-by: Christian Couder Signed-off-by: Usman Akinyemi --- version.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/version.c b/version.c index 4d763ab48d..78f025c808 100644 --- a/version.c +++ b/version.c @@ -6,6 +6,20 @@ const char git_version_string[] = GIT_VERSION; const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT; +/* + * Trim and replace each character with ascii code below 32 or above + * 127 (included) using a dot '.' character. + * TODO: ensure consecutive non-printable characters are only replaced once +*/ +static void redact_non_printables(struct strbuf *buf) +{ + strbuf_trim(buf); + for (size_t i = 0; i < buf->len; i++) { + if (buf->buf[i] <= 32 || buf->buf[i] >= 127) + buf->buf[i] = '.'; + } +} + const char *git_user_agent(void) { static const char *agent = NULL; @@ -27,12 +41,8 @@ const char *git_user_agent_sanitized(void) struct strbuf buf = STRBUF_INIT; strbuf_addstr(&buf, git_user_agent()); - strbuf_trim(&buf); - for (size_t i = 0; i < buf.len; i++) { - if (buf.buf[i] <= 32 || buf.buf[i] >= 127) - buf.buf[i] = '.'; - } - agent = buf.buf; + redact_non_printables(&buf); + agent = strbuf_detach(&buf, NULL); } return agent; From patchwork Mon Jan 6 10:30:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usman Akinyemi X-Patchwork-Id: 13927182 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.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 19D1E1DB52D for ; Mon, 6 Jan 2025 10:37:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736159851; cv=none; b=OUyFg5vZ0uvLiHKmDEQYi9guGHSxVVBlsle+67aPmUz2q+MTUyAvkdQURVUORnEx9HOLciGC3phLVsSBeg1k9BerppXjrQfhsWZeS/FeqzlVyy5m86mKBnO0GfXgtTYxDQgYyxC0NlDNJWXYcC6JF1a6JzFdkWEj3VBSQ2wmSH8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736159851; c=relaxed/simple; bh=g40B3DR0M0J2sP/tV3hKBzYJ6uc9cHlZCoD+3TYH/uA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qSWTzWrf4A0AkLKBgt0EWycOlLmzSpJGES6ZItZYDk4E7uPxWVgCzBwRGChmcZwrpzGo/QgxMPZl19OrDa8njaPE80E7VTC96Zr8xzg9kyCVQiKOHpfoZNHl+5VqZ0Licifrx4CG6RqvpT+EexR/MT1g+iXDTd9WZR9DpXGfR/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=kAmvAqfZ; arc=none smtp.client-ip=209.85.214.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="kAmvAqfZ" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2162c0f6a39so210229995ad.0 for ; Mon, 06 Jan 2025 02:37:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736159847; x=1736764647; 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=Q9vbB+06M/1oU/3N4LcGfrVa7O23n4wo+0GDEC8nRgE=; b=kAmvAqfZLipU8gcTD4pArRUD9ZpsYuEOYItYuJVRe02W/j4DlA5Hkyhihn01ie5JlL +MgQ2fiPMEaVTrdVPVX8WsM/PRN3KUGlIomxrFJ2H7UV3iN0vWugmh7WBp/PJk0rA2pk bJ5RTWWw2u+DrancpQ9ztRHesByDXfXE2aC6BjkqkbgvrH1zrAeCjgWe+kAROep+SDNr tO6+Ys6r5H40zn/YO5zFC7kqyx1YLADnPmdWcy3M1rSqS9MiKB/YFZyjjuMmIw+nAwDk JbX3agOlC8GKuE0up0ahyo3RpUvzz6obYwPdsNyUB3DWTI9Pfx/2VV7Lee3uFX91afph aVQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736159847; x=1736764647; 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=Q9vbB+06M/1oU/3N4LcGfrVa7O23n4wo+0GDEC8nRgE=; b=t5xyKJYl0i7vfG/oJWeEpvVhdD291MZaj4eITezfe3nVJqBQp0Nys1yWZeIjWNdNrG 9lRIbveZaM7e8C6DJGFq/9IJ4Iwii5cnIr3vse66zxk9iUGDuIxGBss6u8q+LTvYqT+A vL8p3nW9uOudFdUTI0vffyDbAM1yCKakvZzTkBQNsqHcD4DuI5Gv59EBBxq6jmmKOGxG h5+538NAkyZo8KDphPqvfYUxkuwNLfvCSIhO8l3FFwY2+iS9fzQm3tyrZK3PkiM0HY98 CgcxbD+A/hlTl0uRsfBlIwjO1qZZyCUXfY6DQxu6xVqsMWveFEl9a64gJTsnOLeHLitP Eftg== X-Gm-Message-State: AOJu0YyDlASt/Fd6gafaNxZ0wyqysHxk4cJf9iyHkd59G2Wfip21mamK DkS461xZNkEJzP0A0AfVIMYCiaYga77vk+x3YXuYR0BqrdXNPcp7w/meSj0+v+k= X-Gm-Gg: ASbGncsZ4T5Mud5jjxiyk5sYc1akOvXU2FGpD0nrIa1Dbj7zQPSUpbsSJtfav3VtWSQ tunt2K6zi0rhgIWx01BqcjcKEraS51qFXFqmQU1NqL7zMrQURmyaGQCHt3H1ai99uSiHUlmAu9b JDwH47vNtXFvIBpo6gOKK7L18aB+b4kUuCb0JeMVazyI4roCC5rpJtmxJpPDi2FDKFehA8RvxyH rS1UBmspPiTsCNrunPEf8Bs1EFjFVY31zR6TFtmcCIHbYL95DvLkqlFjFaiP/CIdOIxqK+XI2cx D/Q3BA== X-Google-Smtp-Source: AGHT+IE/lJoc8y3ywZ9VuAqViR4+JULfiESbGeSOVFWDX+kXRop/KiGOhiiIHoP3/hIbUd7GI9HRjw== X-Received: by 2002:a05:6a20:3d86:b0:1e1:fbab:1f18 with SMTP id adf61e73a8af0-1e5c7002694mr97216553637.23.1736159847079; Mon, 06 Jan 2025 02:37:27 -0800 (PST) Received: from archlinux.plaksha.edu.in ([202.164.41.66]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-842b85f00f9sm28774281a12.43.2025.01.06.02.37.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jan 2025 02:37:26 -0800 (PST) From: Usman Akinyemi To: git@vger.kernel.org, christian.couder@gmail.com Cc: gitster@pobox.com, ps@pks.im, johncai86@gmail.com, Johannes.Schindelin@gmx.de, me@ttaylorr.com, phillip.wood@dunelm.org.uk, Christian Couder Subject: [PATCH 2/4] version: refactor get_uname_info() Date: Mon, 6 Jan 2025 16:00:50 +0530 Message-ID: <20250106103713.1452035-3-usmanakinyemi202@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250106103713.1452035-1-usmanakinyemi202@gmail.com> References: <20250106103713.1452035-1-usmanakinyemi202@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Some code from "builtin/bugreport.c" uses uname(2) to get system information. Let's refactor this code into a new get_uname_info() function, so that we can reuse it in a following commit. We may need to refactor this function in the future if an `osVersion.format` config option is added, but for now we only need it to accept a "full" flag that makes it switch between providing full OS information and providing only the OS name. The mode providing only the OS name is needed in a following commit. Mentored-by: Christian Couder Signed-off-by: Usman Akinyemi --- builtin/bugreport.c | 13 ++----------- version.c | 23 +++++++++++++++++++++++ version.h | 7 +++++++ 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/builtin/bugreport.c b/builtin/bugreport.c index 7c2df035c9..e3288a86c8 100644 --- a/builtin/bugreport.c +++ b/builtin/bugreport.c @@ -12,10 +12,10 @@ #include "diagnose.h" #include "object-file.h" #include "setup.h" +#include "version.h" static void get_system_info(struct strbuf *sys_info) { - struct utsname uname_info; char *shell = NULL; /* get git version from native cmd */ @@ -24,16 +24,7 @@ static void get_system_info(struct strbuf *sys_info) /* system call for other version info */ strbuf_addstr(sys_info, "uname: "); - if (uname(&uname_info)) - strbuf_addf(sys_info, _("uname() failed with error '%s' (%d)\n"), - strerror(errno), - errno); - else - strbuf_addf(sys_info, "%s %s %s %s\n", - uname_info.sysname, - uname_info.release, - uname_info.version, - uname_info.machine); + get_uname_info(sys_info, 1); strbuf_addstr(sys_info, _("compiler info: ")); get_compiler_info(sys_info); diff --git a/version.c b/version.c index 78f025c808..44ffc4dd57 100644 --- a/version.c +++ b/version.c @@ -2,6 +2,7 @@ #include "version.h" #include "version-def.h" #include "strbuf.h" +#include "gettext.h" const char git_version_string[] = GIT_VERSION; const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT; @@ -47,3 +48,25 @@ const char *git_user_agent_sanitized(void) return agent; } + +int get_uname_info(struct strbuf *buf, unsigned int full) +{ + struct utsname uname_info; + + if (uname(&uname_info)) { + strbuf_addf(buf, _("uname() failed with error '%s' (%d)\n"), + strerror(errno), + errno); + return -1; + } + + if (full) + strbuf_addf(buf, "%s %s %s %s\n", + uname_info.sysname, + uname_info.release, + uname_info.version, + uname_info.machine); + else + strbuf_addf(buf, "%s\n", uname_info.sysname); + return 0; +} diff --git a/version.h b/version.h index 7c62e80577..5eb586c0bd 100644 --- a/version.h +++ b/version.h @@ -7,4 +7,11 @@ extern const char git_built_from_commit_string[]; const char *git_user_agent(void); const char *git_user_agent_sanitized(void); +/* + Try to get information about the system using uname(2). + Return -1 and put an error message into 'buf' in case of uname() + error. Return 0 and put uname info into 'buf' otherwise. +*/ +int get_uname_info(struct strbuf *buf, unsigned int full); + #endif /* VERSION_H */ From patchwork Mon Jan 6 10:30:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usman Akinyemi X-Patchwork-Id: 13927183 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 CDA2D19E7EB for ; Mon, 6 Jan 2025 10:37:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736159854; cv=none; b=tRtudEgG/VgAxgStzk+sFXCDjm2rXFnHHF5QaaGSvCp4A3tdZj08s/wPUfheMQBXCIpyHWO9uORTyMIgrC3vn2s0xAXLSzZotHXVish7Ue7KSae85oOEWf+c7ES0cC3+qhDfCW9FqBu1G+PtIN2Y2sJ35YLlBlbT1oSfkOoWuhw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736159854; c=relaxed/simple; bh=YYFQTvpqSN7W/kRXejV0+UFuwkTyx1M9IOZUgZa70mA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BbJtUc/X0I8U7jz3Kn4hova6nnep4PFrBeyFwdJZIltzdoCzuhgRJF1Ftzr3mpr6CYNb1OV2X9xoSb/90beDmuViEjYtR/sUXSw66A1NG4El4/sONNJHMdXZtxoSdfT9wnNzW3boiUv2b2+rmpmwb8stFVEXdtsM/f0y25bL09Q= 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=JPo8ZfT5; arc=none smtp.client-ip=209.85.214.174 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="JPo8ZfT5" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2164b662090so177632055ad.1 for ; Mon, 06 Jan 2025 02:37:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736159850; x=1736764650; 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=GE5ihcAJ+Kpt1lJIv9WzWk7McPwII/sW/F0fTCVn8IU=; b=JPo8ZfT56gnepAnoOff/pz3n6qBSj7EUNPz+73AOXIp1JOJpdf1Ks82h2YDiKNQF3T uSqtNP1C5Mx19aV9/zasoxteWiBM/71Ba2HVjjbZCGgPiPPfQQSwLYkj7nUDx4r87Sb8 z1XJARKbPMZWcYzMhZXIGPmd5FLqNPC0fYTUCifDBo5a4NQHMfFpy64wW0B4KixdQlrk +iZSPeQxuUXMcBoSM6j0A4/bwrbCdlK1ybU89xoJ2kyne4A9fMuw6fWKH889TuuIso2i thGefrfQ02ji64NCpGUFyLvBDAQMLyjEa5MS2iHk33xyntrKoz1md42Pi59IxeFA1HZ+ ujEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736159850; x=1736764650; 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=GE5ihcAJ+Kpt1lJIv9WzWk7McPwII/sW/F0fTCVn8IU=; b=cChcUL7lNVj7Ojhyb+GRjN14mO+6qGagC2tjezyX0HCb5QPLfD3JSGjIleI3vG902s kMSJwIALa+mp0JZLb5mcgMrzB/hsdT1aaMhztcAAmKX0otVDrn3CJ5++GbRg37CFW2Vm DzLoLc0JS38b7LBt+Gz8LOH6Dq1sL36ZasnkMpRttR4jR+nNasgyv6DUuVowpOxcfvjH lZp2oYq0T/U+dZvBPQA5XwP2R3yaNeg6B3B6HRqdB4rCx5acIOOa282X2bllWU+ArAOn uj1ImpwUpakH+nGzNgAT9uFVBxUWAtf2gsnjm2lL6apu4xijTSu0izZoE0TMAU5Ket+n dddg== X-Gm-Message-State: AOJu0YzWQuCUUNtAuT4/FR2LeeAAK+akX/a1Rm6aQNh2T+ffzaUmsYfy 0Fhle/jdVxXcF0yi5W6ou8cE4bjBta3Dgs3E4DjKDxqFv8cXOcnRQXBpA6wpwU4= X-Gm-Gg: ASbGncsyqkI152cRmbNx85tTSBxidaXhBVuLu8FjAvhGAnBC1BRsoRQF2yzcr4pAOCH LYnvzwpYMja6zEwgzOenXbstoFPfc8k6EK56tBvDfE7/RfcgwWvNExEalFCitrI/qT8xJlXdjif NCtEapsuKYjo/e/Bg2wUTbQzNG4ee7XE0Lk8NO/etggezKjyCFokRSJCG3mTp//49n+OqfGiS/Z ufxs9N/H/E3fbt/zQIomgAknDgVOBNFB3QDbOpx4I+Ore+kYfO5viDw2PCgf/HRBbdWAJxeo9Dx w+VspA== X-Google-Smtp-Source: AGHT+IFDY3mSKUo88TmmHq7DVjOJAQQ+Pkb6F6H6e9CZUEAELdTMHxVNDx6+u/Jr2VTjUDmqVrQRAw== X-Received: by 2002:a05:6a00:414a:b0:727:3fd5:b530 with SMTP id d2e1a72fcca58-72abde0f264mr73969785b3a.15.1736159850554; Mon, 06 Jan 2025 02:37:30 -0800 (PST) Received: from archlinux.plaksha.edu.in ([202.164.41.66]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-842b85f00f9sm28774281a12.43.2025.01.06.02.37.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jan 2025 02:37:30 -0800 (PST) From: Usman Akinyemi To: git@vger.kernel.org, christian.couder@gmail.com Cc: gitster@pobox.com, ps@pks.im, johncai86@gmail.com, Johannes.Schindelin@gmx.de, me@ttaylorr.com, phillip.wood@dunelm.org.uk, Christian Couder Subject: [PATCH 3/4] connect: advertise OS version Date: Mon, 6 Jan 2025 16:00:51 +0530 Message-ID: <20250106103713.1452035-4-usmanakinyemi202@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250106103713.1452035-1-usmanakinyemi202@gmail.com> References: <20250106103713.1452035-1-usmanakinyemi202@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 As some issues that can happen with a Git client can be operating system specific, it can be useful for a server to know which OS a client is using. In the same way it can be useful for a client to know which OS a server is using. Let's introduce a new protocol (`os-version`) allowing Git clients and servers to exchange operating system information. The protocol is controlled by the new `transfer.advertiseOSVersion` config option. Add the `transfer.advertiseOSVersion` config option to address privacy concerns issue. It defaults to `true` and can be changed to `false`. When enabled, this option makes clients and servers send each other the OS name (e.g., "Linux" or "Windows"). The information is retrieved using the 'sysname' field of the `uname(2)` system call. However, there are differences between `uname(1)` (command-line utility) and `uname(2)` (system call) outputs on Windows. These discrepancies complicate testing on Windows platforms. For example: - `uname(1)` output: MINGW64_NT-10.0-20348.3.4.10-87d57229.x86_64\ .2024-02-14.20:17.UTC.x86_64 - `uname(2)` output: Windows.10.0.20348 Until a good way to test the feature on Windows is found, the transfer.advertiseOSVersion is set to false on Windows during testing. Mentored-by: Christian Couder Signed-off-by: Usman Akinyemi --- Documentation/config/transfer.txt | 7 ++++++ Documentation/gitprotocol-v2.txt | 20 +++++++++++++++ connect.c | 3 +++ serve.c | 14 +++++++++++ t/t5555-http-smart-common.sh | 12 ++++++++- t/t5701-git-serve.sh | 12 ++++++++- t/test-lib-functions.sh | 8 ++++++ version.c | 42 +++++++++++++++++++++++++++++++ version.h | 6 +++++ 9 files changed, 122 insertions(+), 2 deletions(-) diff --git a/Documentation/config/transfer.txt b/Documentation/config/transfer.txt index f1ce50f4a6..e2d95d1ccd 100644 --- a/Documentation/config/transfer.txt +++ b/Documentation/config/transfer.txt @@ -125,3 +125,10 @@ transfer.bundleURI:: transfer.advertiseObjectInfo:: When `true`, the `object-info` capability is advertised by servers. Defaults to false. + +transfer.advertiseOSVersion:: + When `true`, the `os-version` capability is advertised by clients and + servers. It makes clients and servers send to each other a string + representing the operating system name, like "Linux" or "Windows". + This string is retrieved from the 'sysname' field of the struct returned + by the uname(2) system call. Defaults to true. diff --git a/Documentation/gitprotocol-v2.txt b/Documentation/gitprotocol-v2.txt index 1652fef3ae..c28262c60b 100644 --- a/Documentation/gitprotocol-v2.txt +++ b/Documentation/gitprotocol-v2.txt @@ -190,6 +190,26 @@ printable ASCII characters except space (i.e., the byte range 32 < x < and debugging purposes, and MUST NOT be used to programmatically assume the presence or absence of particular features. +os-version +~~~~~~~~~~ + +In the same way as the `agent` capability above, the server can +advertise the `os-version` capability with a value `X` (in the form +`os-version=X`) to notify the client that the server is running an +operating system that can be identified by `X`. The client may +optionally send its own `os-version` string by including the +`os-version` capability with a value `Y` (in the form `os-version=Y`) +in its request to the server (but it MUST NOT do so if the server did +not advertise the os-version capability). The `X` and `Y` strings may +contain any printable ASCII characters except space (i.e., the byte +range 32 < x < 127), and are typically made from the result of +`uname -s`(OS name e.g Linux). The os-version capability can be disabled +entirely by setting the `transfer.advertiseOSVersion` config option +to `false`. The `os-version` strings are purely informative for +statistics and debugging purposes, and MUST NOT be used to +programmatically assume the presence or absence of particular +features. + ls-refs ~~~~~~~ diff --git a/connect.c b/connect.c index 10fad43e98..6d5792b63c 100644 --- a/connect.c +++ b/connect.c @@ -492,6 +492,9 @@ static void send_capabilities(int fd_out, struct packet_reader *reader) if (server_supports_v2("agent")) packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized()); + if (server_supports_v2("os-version") && advertise_os_version(the_repository)) + packet_write_fmt(fd_out, "os-version=%s", os_version_sanitized()); + if (server_feature_v2("object-format", &hash_name)) { int hash_algo = hash_algo_by_name(hash_name); if (hash_algo == GIT_HASH_UNKNOWN) diff --git a/serve.c b/serve.c index c8694e3751..5b0d54ae9a 100644 --- a/serve.c +++ b/serve.c @@ -31,6 +31,16 @@ static int agent_advertise(struct repository *r UNUSED, return 1; } +static int os_version_advertise(struct repository *r, + struct strbuf *value) +{ + if (!advertise_os_version(r)) + return 0; + if (value) + strbuf_addstr(value, os_version_sanitized()); + return 1; +} + static int object_format_advertise(struct repository *r, struct strbuf *value) { @@ -123,6 +133,10 @@ static struct protocol_capability capabilities[] = { .name = "agent", .advertise = agent_advertise, }, + { + .name = "os-version", + .advertise = os_version_advertise, + }, { .name = "ls-refs", .advertise = ls_refs_advertise, diff --git a/t/t5555-http-smart-common.sh b/t/t5555-http-smart-common.sh index e47ea1ad10..f9e2a66cba 100755 --- a/t/t5555-http-smart-common.sh +++ b/t/t5555-http-smart-common.sh @@ -123,9 +123,19 @@ test_expect_success 'git receive-pack --advertise-refs: v1' ' ' test_expect_success 'git upload-pack --advertise-refs: v2' ' + printf "agent=FAKE" >agent_and_os_name && + if test_have_prereq WINDOWS + then + # We do not use test_config here so that any tests below can reuse + # the "expect" file from this test + git config transfer.advertiseOSVersion false + else + printf "\nos-version=%s\n" $(uname -s | test_redact_non_printables) >>agent_and_os_name + fi && + cat >expect <<-EOF && version 2 - agent=FAKE + $(cat agent_and_os_name) ls-refs=unborn fetch=shallow wait-for-done server-option diff --git a/t/t5701-git-serve.sh b/t/t5701-git-serve.sh index de904c1655..f4668b7acd 100755 --- a/t/t5701-git-serve.sh +++ b/t/t5701-git-serve.sh @@ -8,13 +8,23 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME . ./test-lib.sh test_expect_success 'test capability advertisement' ' + printf "agent=git/$(git version | cut -d" " -f3)" >agent_and_os_name && + if test_have_prereq WINDOWS + then + # We do not use test_config here so that tests below will be able to reuse + # the expect.base and expect.trailer files + git config transfer.advertiseOSVersion false + else + printf "\nos-version=%s\n" $(uname -s | test_redact_non_printables) >>agent_and_os_name + fi && + test_oid_cache <<-EOF && wrong_algo sha1:sha256 wrong_algo sha256:sha1 EOF cat >expect.base <<-EOF && version 2 - agent=git/$(git version | cut -d" " -f3) + $(cat agent_and_os_name) ls-refs=unborn fetch=shallow wait-for-done server-option diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index 78e054ab50..447c698d74 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -2007,3 +2007,11 @@ test_trailing_hash () { test-tool hexdump | sed "s/ //g" } + +# Trim and replace each character with ascii code below 32 or above +# 127 (included) using a dot '.' character. +# Octal intervals \001-\040 and \177-\377 +# corresponds to decimal intervals 1-32 and 127-255 +test_redact_non_printables () { + tr -d "\n" | tr "[\001-\040][\177-\377]" "." +} diff --git a/version.c b/version.c index 44ffc4dd57..8242baf41c 100644 --- a/version.c +++ b/version.c @@ -3,6 +3,7 @@ #include "version-def.h" #include "strbuf.h" #include "gettext.h" +#include "config.h" const char git_version_string[] = GIT_VERSION; const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT; @@ -70,3 +71,44 @@ int get_uname_info(struct strbuf *buf, unsigned int full) strbuf_addf(buf, "%s\n", uname_info.sysname); return 0; } + +const char *os_version(void) +{ + static const char *os = NULL; + + if (!os) { + struct strbuf buf = STRBUF_INIT; + + get_uname_info(&buf, 0); + os = strbuf_detach(&buf, NULL); + } + + return os; +} + +const char *os_version_sanitized(void) +{ + static const char *os_sanitized = NULL; + + if (!os_sanitized) { + struct strbuf buf = STRBUF_INIT; + + strbuf_addstr(&buf, os_version()); + redact_non_printables(&buf); + os_sanitized = strbuf_detach(&buf, NULL); + } + + return os_sanitized; +} + +int advertise_os_version(struct repository *r) +{ + static int transfer_advertise_os_version = -1; + + if (transfer_advertise_os_version == -1) { + repo_config_get_bool(r, "transfer.advertiseosversion", &transfer_advertise_os_version); + /* enabled by default */ + transfer_advertise_os_version = !!transfer_advertise_os_version; + } + return transfer_advertise_os_version; +} diff --git a/version.h b/version.h index 5eb586c0bd..8167ce956a 100644 --- a/version.h +++ b/version.h @@ -1,6 +1,8 @@ #ifndef VERSION_H #define VERSION_H +struct repository; + extern const char git_version_string[]; extern const char git_built_from_commit_string[]; @@ -14,4 +16,8 @@ const char *git_user_agent_sanitized(void); */ int get_uname_info(struct strbuf *buf, unsigned int full); +const char *os_version(void); +const char *os_version_sanitized(void); +int advertise_os_version(struct repository *r); + #endif /* VERSION_H */ From patchwork Mon Jan 6 10:30:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usman Akinyemi X-Patchwork-Id: 13927184 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 0C9CD1DB92A for ; Mon, 6 Jan 2025 10:37:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736159857; cv=none; b=Q/3gkUyKbLnCOVuesz+WX4XU1NFaxgFnDXfaYxX/JW56voFg0wFmlYhxPR78gBv/RCwqPKUrMEtO/arZ8Nh0MMZXgPmLQ/y/2aDyE2OxJDr85ah7wv441nUpVWgddvd8RWTn2fleh3Yq+uLo6hFEBWv+uPne51SCZW8/GB9lcdg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736159857; c=relaxed/simple; bh=8/PDchkR/KMRquoK5FityImFgPDfszfA29AUIdbJGKY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Zu2TaE6IFhQg3xZIbJZ3SliKUEJt3RvVsh+LpQF/ZUp7eubmR+1LYFctqU7XBZkMKbcco5CqhyhZCUEUzQvThTNMGXlh9UAOjQms/SAEbe+xerHkcDAAgf2HaoKTKoYN4lSjmAstyIYXYIcnqxZ/CAns0zu23TZ9pv258ih/j10= 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=PpIkSsSN; arc=none smtp.client-ip=209.85.214.177 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="PpIkSsSN" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-21675fd60feso2463745ad.2 for ; Mon, 06 Jan 2025 02:37:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736159854; x=1736764654; 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=2Ak2KvSyKXuyYal8G0OJ/TdfdcBHWAxOYRWEb3+u+sM=; b=PpIkSsSNlWOsFpR2zCid7yNLFtPOz+Ni52Dv7gOmeJNuBxk5a/0qe6A1ijAVgIhPVI CT2NJ4NqpsxLxVcTkSsO8Kma82dsFiKgIwODSa3sxTEIDevf+EqAF6Q2cTmv9mlH2V7I 11qDtwvAUFg7gae/B6s3xI8BiCeXFLlJC2kbjI56bwCQnOd7USf1V7fwrctD5n5c7ieY eEG7HQq8m3Tasleceiqaue8DRyyQ70GrFCyK490JSC6sbN9h7AUIdK8yCcCu9iBH2FC7 /64kN8b58H0aFgpViJiHePKbTe5Fw88lS19Nk/z45O1I2URf7mw0i5SXmEaDIVHywRuy PWMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736159854; x=1736764654; 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=2Ak2KvSyKXuyYal8G0OJ/TdfdcBHWAxOYRWEb3+u+sM=; b=Aqx8hwU2Q25yncNRMKmNxRjQqyO7T8UfRdiNEyFbFmdF+dEVlqfEli2v8mWq48hskK Qnh1DAFD67fj2CHDf+UtrLE0WE94kXDhfV75OP4hdGaE15HwaH5D5XzMPNHeVlFtj6zp 9GJggpNFidkXYvKhokORHyrnkTZMMRMAWkJlkHjmPs2brt4DQTNMg89tb8J8lMge2pV9 ComtoEM5h9q1h+upYeKMUTMFnb0tTWJQgg3ARWmNj/0PV9mRRnHXFcVs76Gu/jbs4FzK XaT7W74d4/lD9cyCFw4SVmizO2zbt3o4fRWJyrCiiUW3no+tx34iJ5SeRz8x4wgQew40 eNdg== X-Gm-Message-State: AOJu0Yz9/ASvxhmCKQQWhPcSRZ9O5i9pNxKEdCcims59RUIChZnh/vH9 3sZ9HMsAHApyvK2ib/D+w1eRp96kWA2JmJwaHrNjZHFS2awIJ0TzjFmwDeDItTk= X-Gm-Gg: ASbGncvrq/ZAGJw+PNl3iPUt4AhIgSvxgOmwua48f0bsWaHQhaADbYplgzhsKCg5IrL qoGqAteTRN/P7z9PPDb/WW+Qrb7cdf2BHJq9dRJNRFzhLoHhIuRZNHXRqeOkLvl8p44JvCvHamm 8PF59D4noZ6JszFqm1OcZCrde4vPzM2oHEdj0z9JJ4IVIQ4syHl/mfBDX4hM+FAzBxcO7r+99+T Hgn+2FXIfKOFTRBbHiSVYLd7V2rGIT9s+8YWm6u/zYxf1YK7RXxi+WiNOIg4QFz2+Td7QsVUbbN IMwa3g== X-Google-Smtp-Source: AGHT+IFV29saUT06l7D3q67/HDEzXAzR/BOryFAr/4e6v2s+ZexmOnkxS/i7Mauv/q6vLCD3H6jwpA== X-Received: by 2002:a17:902:e5c7:b0:215:3fb9:5201 with SMTP id d9443c01a7336-219e6f14834mr789772555ad.44.1736159854118; Mon, 06 Jan 2025 02:37:34 -0800 (PST) Received: from archlinux.plaksha.edu.in ([202.164.41.66]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-842b85f00f9sm28774281a12.43.2025.01.06.02.37.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jan 2025 02:37:33 -0800 (PST) From: Usman Akinyemi To: git@vger.kernel.org, christian.couder@gmail.com Cc: gitster@pobox.com, ps@pks.im, johncai86@gmail.com, Johannes.Schindelin@gmx.de, me@ttaylorr.com, phillip.wood@dunelm.org.uk, Christian Couder Subject: [PATCH 4/4] version: introduce osversion.command config for os-version output Date: Mon, 6 Jan 2025 16:00:52 +0530 Message-ID: <20250106103713.1452035-5-usmanakinyemi202@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250106103713.1452035-1-usmanakinyemi202@gmail.com> References: <20250106103713.1452035-1-usmanakinyemi202@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently by default, the new `os-version` capability only exchange the operating system name between servers and clients i.e "Linux" or "Windows". Let's introduce a new configuration option, `osversion.command`, to handle the string exchange between servers and clients. This option allows customization of the exchanged string by leveraging the output of the specified command. If this is not set, the `os-version` capability exchange just the operating system name. Mentored-by: Christian Couder Signed-off-by: Usman Akinyemi --- Documentation/config/transfer.txt | 11 ++++++- Documentation/gitprotocol-v2.txt | 13 ++++---- t/t5555-http-smart-common.sh | 29 ++++++++++++++++++ t/t5701-git-serve.sh | 33 ++++++++++++++++++++ version.c | 51 ++++++++++++++++++++++++++++++- 5 files changed, 129 insertions(+), 8 deletions(-) diff --git a/Documentation/config/transfer.txt b/Documentation/config/transfer.txt index e2d95d1ccd..28a08f21fc 100644 --- a/Documentation/config/transfer.txt +++ b/Documentation/config/transfer.txt @@ -131,4 +131,13 @@ transfer.advertiseOSVersion:: servers. It makes clients and servers send to each other a string representing the operating system name, like "Linux" or "Windows". This string is retrieved from the 'sysname' field of the struct returned - by the uname(2) system call. Defaults to true. + by the uname(2) system call. If the `osVersion.command` is set, the + output of the command specified will be the string exchanged by the clients + and the servers. Defaults to true. + +osVersion.command:: + If this variable is set, the specified command will be run and the output + will be used as the value `X` for `os-version` capability (in the form + `os-version=X`). `osVersion.command` is only used if `transfer.advertiseOSVersion` + is true. Refer to the linkgit:git-config[1] documentation to learn more about + `transfer.advertiseOSVersion` config option. diff --git a/Documentation/gitprotocol-v2.txt b/Documentation/gitprotocol-v2.txt index c28262c60b..53621c0bce 100644 --- a/Documentation/gitprotocol-v2.txt +++ b/Documentation/gitprotocol-v2.txt @@ -203,12 +203,13 @@ in its request to the server (but it MUST NOT do so if the server did not advertise the os-version capability). The `X` and `Y` strings may contain any printable ASCII characters except space (i.e., the byte range 32 < x < 127), and are typically made from the result of -`uname -s`(OS name e.g Linux). The os-version capability can be disabled -entirely by setting the `transfer.advertiseOSVersion` config option -to `false`. The `os-version` strings are purely informative for -statistics and debugging purposes, and MUST NOT be used to -programmatically assume the presence or absence of particular -features. +`uname -s`(OS name e.g Linux). If the `osVersion.command` is set, +the `X` and `Y` are made from the ouput of the command specified. +The os-version capability can be disabled entirely by setting the +`transfer.advertiseOSVersion` config option to `false`. The `os-version` +strings are purely informative for statistics and debugging purposes, and +MUST NOT be used to programmatically assume the presence or absence of +particular features. ls-refs ~~~~~~~ diff --git a/t/t5555-http-smart-common.sh b/t/t5555-http-smart-common.sh index f9e2a66cba..8d5844eaf2 100755 --- a/t/t5555-http-smart-common.sh +++ b/t/t5555-http-smart-common.sh @@ -152,6 +152,35 @@ test_expect_success 'git upload-pack --advertise-refs: v2' ' test_cmp actual expect ' +test_expect_success 'git upload-pack --advertise-refs: v2 with osVersion.command config set' ' + # test_config is used here as we are not reusing any file output from here + test_config osVersion.command "uname -srvm" && + printf "agent=FAKE" >agent_and_long_os_name && + + if test_have_prereq !WINDOWS + then + printf "\nos-version=%s\n" $(uname -srvm | test_redact_non_printables) >>agent_and_long_os_name + fi && + + cat >expect <<-EOF && + version 2 + $(cat agent_and_long_os_name) + ls-refs=unborn + fetch=shallow wait-for-done + server-option + object-format=$(test_oid algo) + 0000 + EOF + + GIT_PROTOCOL=version=2 \ + GIT_USER_AGENT=FAKE \ + git upload-pack --advertise-refs . >out 2>err && + + test-tool pkt-line unpack actual && + test_must_be_empty err && + test_cmp actual expect +' + test_expect_success 'git receive-pack --advertise-refs: v2' ' # There is no v2 yet for receive-pack, implicit v0 cat >expect <<-EOF && diff --git a/t/t5701-git-serve.sh b/t/t5701-git-serve.sh index f4668b7acd..51d99cd62c 100755 --- a/t/t5701-git-serve.sh +++ b/t/t5701-git-serve.sh @@ -41,6 +41,39 @@ test_expect_success 'test capability advertisement' ' test_cmp expect actual ' +test_expect_success 'test capability advertisement with osVersion.command config set' ' + # test_config is used here as we are not reusing any file output from here + test_config osVersion.command "uname -srvm" && + printf "agent=git/$(git version | cut -d" " -f3)" >agent_and_long_os_name && + + if test_have_prereq !WINDOWS + then + printf "\nos-version=%s\n" $(uname -srvm | test_redact_non_printables) >>agent_and_long_os_name + fi && + + test_oid_cache <<-EOF && + wrong_algo sha1:sha256 + wrong_algo sha256:sha1 + EOF + cat >expect.base_long <<-EOF && + version 2 + $(cat agent_and_long_os_name) + ls-refs=unborn + fetch=shallow wait-for-done + server-option + object-format=$(test_oid algo) + EOF + cat >expect.trailer_long <<-EOF && + 0000 + EOF + cat expect.base_long expect.trailer_long >expect && + + GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \ + --advertise-capabilities >out && + test-tool pkt-line unpack actual && + test_cmp expect actual +' + test_expect_success 'stateless-rpc flag does not list capabilities' ' # Empty request test-tool pkt-line pack >in <<-EOF && diff --git a/version.c b/version.c index 8242baf41c..b446232898 100644 --- a/version.c +++ b/version.c @@ -1,9 +1,13 @@ +#define USE_THE_REPOSITORY_VARIABLE + #include "git-compat-util.h" #include "version.h" #include "version-def.h" #include "strbuf.h" #include "gettext.h" #include "config.h" +#include "run-command.h" +#include "alias.h" const char git_version_string[] = GIT_VERSION; const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT; @@ -72,6 +76,50 @@ int get_uname_info(struct strbuf *buf, unsigned int full) return 0; } +/* + * Return -1 if unable to retrieve the osversion.command config or + * if the command is malformed; otherwise, return 0 if successful. + */ +static int fill_os_version_command(struct child_process *cmd) +{ + const char *os_version_command; + const char **argv; + char *os_version_copy; + int n; + + if (git_config_get_string_tmp("osversion.command", &os_version_command)) + return -1; + + os_version_copy = xstrdup(os_version_command); + n = split_cmdline(os_version_copy, &argv); + + if (n < 0) { + warning(_("malformed osVersion.command config option: %s"), + _(split_cmdline_strerror(n))); + free(os_version_copy); + return -1; + } + + for (int i = 0; i < n; i++) + strvec_push(&cmd->args, argv[i]); + free(os_version_copy); + free(argv); + + return 0; +} + +static int capture_os_version(struct strbuf *buf) +{ + struct child_process cmd = CHILD_PROCESS_INIT; + + if (fill_os_version_command(&cmd)) + return -1; + if (capture_command(&cmd, buf, 0)) + return -1; + + return 0; +} + const char *os_version(void) { static const char *os = NULL; @@ -79,7 +127,8 @@ const char *os_version(void) if (!os) { struct strbuf buf = STRBUF_INIT; - get_uname_info(&buf, 0); + if (capture_os_version(&buf)) + get_uname_info(&buf, 0); os = strbuf_detach(&buf, NULL); }