From patchwork Fri Sep 6 22:21:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13794806 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 E0D071B85D9 for ; Fri, 6 Sep 2024 22:21:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725661285; cv=none; b=r4TNF8E/kMfXmqCNrs/osyU7PsWSbKpED8n9aAZxNmIKMuRPoblYRqEaRCoNfyC1pNmsULFp2mpZrYg9T+XXVBTN1Jf3gnV/10qBT/BFrt52TNMyijn9F0vqVkiIncE4YIe4v3zdoMxQLCxKxNcxvajFLoOL2yc2zKg80X3e3b4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725661285; c=relaxed/simple; bh=cNjTQ9do7Pb592/l3hClue6Rg3U4igqIppECLFsKOsg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KgPIV1AO1L1Qkn/oXh+lCNAuCHvN1WmTcPRzX7+upjioNnaFZr0StsRyrK86efP2vTzC9JELENlLr8tDWUjThmRyD2sahmxjTumL7XumB3XM90jtizwfretyJWJPyIw70v8uf8a6Q+mEc7fH7Wrtj1DGxBTrXfZb2HTdqMm2IwU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--calvinwan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ll5x0lIu; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--calvinwan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ll5x0lIu" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2052a68430fso35393315ad.2 for ; Fri, 06 Sep 2024 15:21:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725661283; x=1726266083; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5YWAuPL8uhjw1wpxp06VnUE1NuAuxV0ix64I1KoaU5c=; b=ll5x0lIuUJXJtCQR62jQXvX6aZ286ezLefp7qq7lVainNxQVRkrSf2JIvjcbTyd3Jq wmt5B/voiJkmvPzm2ba4Hin1swwy7IUV4igalwzyAAORbQFsLH0X5TO3tzQC/pB8+97v FeoBDufCR4HoIBJqCjmycf7NFWA6Gjw4Mnx4XepPvH95zFtCSkVfmUynem4w/5ZOQUXq hDxjjD8aVARrjiEtVHGqvZxq5UyY55BYRae1+RgwW6sRB1c1UvqVvlFzt5/4PEdW/QSs W6dBdHcOpixFLRkasmXQQFLsRbnP6TLbjgxwF2WGds8iEW6/Ck7pAGZqhp1vy1JOMykO X7yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725661283; x=1726266083; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5YWAuPL8uhjw1wpxp06VnUE1NuAuxV0ix64I1KoaU5c=; b=hNYkkyhj0/rFgO3nqYMVELgKsob5/BjOPce09fowqICFKJqtmg4s5W5uThrf3PX7Kc auw16cWTNp+UFmk3jm2lJvxmSggyjnPRQoyECU4VFOnKkK5ya9tUr5KSSnbmyBnCmZ++ bk375HcUzzMCn12pp4Im4WaxtM8cmnLb/QdUrSRp4evo6BoWKTQZrhHy7of7qQkOsA0c Eur41Ta+fdakKYOJ1NLcurAzQwGqyhk/iUgOcxO9BDcDsVvN062nXRcAlN0gc6nNBZlh AQEwmlwWqE/SNF5eYoRA5BHYHC47SF1m+asl1tfsTxPFfmVGnEItLRRoZdDNPBZr3eYc gXDw== X-Gm-Message-State: AOJu0YwNXRy2wSICufOx9YLMWLTm72BGs6rBZYehFWuUbg6VJQDd9lWa IrdoWaBPism6JslsKoiGi9+IRnzDKZfUFUDbObgVzFXcuCDb1rGbc0DHK8B8Sq1YAJLvPqrwgfI HFoCOvQ37KxJsr7acrAzK6NrbZVvqqhEqE+GNAZ1ty8hEPVxnfJV4Lq3Fk/Af/NHGsfDN9rL1Dz gNZbLKerVSmsTPSYoVTPQarsTaM7+r2+wFJitwm6D6COXR X-Google-Smtp-Source: AGHT+IGK6VYPB16X4w5Uk/oR/UcVpl4J+dMgdhPJzxiMJoA+/GUDXhoWbo6ED59riPDl8jxKArEWzvMF/EO1Xp0= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a17:902:f68c:b0:205:4d47:e3ca with SMTP id d9443c01a7336-206f04b1669mr4359905ad.2.1725661282845; Fri, 06 Sep 2024 15:21:22 -0700 (PDT) Date: Fri, 6 Sep 2024 22:21:11 +0000 In-Reply-To: <20240906221853.257984-1-calvinwan@google.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240906221853.257984-1-calvinwan@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240906222116.270196-1-calvinwan@google.com> Subject: [PATCH v3 1/6] common-main: split init and exit code into new files From: Calvin Wan To: git@vger.kernel.org Cc: Josh Steadmon , spectral@google.com, emilyshaffer@google.com, emrass@google.com, rsbecker@nexbridge.com, gitster@pobox.com, mh@glandium.org, sandals@crustytoothpaste.net, Jason@zx2c4.com, dsimic@manjaro.org, Calvin Wan From: Josh Steadmon Currently, object files in libgit.a reference common_exit(), which is contained in common-main.o. However, common-main.o also includes main(), which references cmd_main() in git.o, which in turn depends on all the builtin/*.o objects. We would like to allow external users to link libgit.a without needing to include so many extra objects. Enable this by splitting common_exit() and check_bug_if_BUG() into a new file common-exit.c, and add common-exit.o to LIB_OBJS so that these are included in libgit.a. This split has previously been proposed ([1], [2]) to support fuzz tests and unit tests by avoiding conflicting definitions for main(). However, both of those issues were resolved by other methods of avoiding symbol conflicts. Now we are trying to make libgit.a more self-contained, so hopefully we can revisit this approach. Additionally, move the initialization code out of main() into a new init_git() function in its own file. Include this in libgit.a as well, so that external users can share our setup code without calling our main(). [1] https://lore.kernel.org/git/Yp+wjCPhqieTku3X@google.com/ [2] https://lore.kernel.org/git/20230517-unit-tests-v2-v2-1-21b5b60f4b32@google.com/ Change-Id: I3d9b23ac28a50a4b79760987a3f37b0c6f4cbf9c Signed-off-by: Calvin Wan --- Makefile | 2 ++ common-exit.c | 26 ++++++++++++++++ common-init.c | 63 ++++++++++++++++++++++++++++++++++++++ common-init.h | 6 ++++ common-main.c | 83 ++------------------------------------------------- 5 files changed, 99 insertions(+), 81 deletions(-) create mode 100644 common-exit.c create mode 100644 common-init.c create mode 100644 common-init.h diff --git a/Makefile b/Makefile index 3eab701b10..7caeb3c872 100644 --- a/Makefile +++ b/Makefile @@ -979,6 +979,8 @@ LIB_OBJS += combine-diff.o LIB_OBJS += commit-graph.o LIB_OBJS += commit-reach.o LIB_OBJS += commit.o +LIB_OBJS += common-exit.o +LIB_OBJS += common-init.o LIB_OBJS += compat/nonblock.o LIB_OBJS += compat/obstack.o LIB_OBJS += compat/terminal.o diff --git a/common-exit.c b/common-exit.c new file mode 100644 index 0000000000..1aaa538be3 --- /dev/null +++ b/common-exit.c @@ -0,0 +1,26 @@ +#include "git-compat-util.h" +#include "trace2.h" + +static void check_bug_if_BUG(void) +{ + if (!bug_called_must_BUG) + return; + BUG("on exit(): had bug() call(s) in this process without explicit BUG_if_bug()"); +} + +/* We wrap exit() to call common_exit() in git-compat-util.h */ +int common_exit(const char *file, int line, int code) +{ + /* + * For non-POSIX systems: Take the lowest 8 bits of the "code" + * to e.g. turn -1 into 255. On a POSIX system this is + * redundant, see exit(3) and wait(2), but as it doesn't harm + * anything there we don't need to guard this with an "ifdef". + */ + code &= 0xff; + + check_bug_if_BUG(); + trace2_cmd_exit_fl(file, line, code); + + return code; +} diff --git a/common-init.c b/common-init.c new file mode 100644 index 0000000000..5cc73f058c --- /dev/null +++ b/common-init.c @@ -0,0 +1,63 @@ +#define USE_THE_REPOSITORY_VARIABLE + +#include "git-compat-util.h" +#include "common-init.h" +#include "exec-cmd.h" +#include "gettext.h" +#include "attr.h" +#include "repository.h" +#include "setup.h" +#include "strbuf.h" +#include "trace2.h" + +/* + * Many parts of Git have subprograms communicate via pipe, expect the + * upstream of a pipe to die with SIGPIPE when the downstream of a + * pipe does not need to read all that is written. Some third-party + * programs that ignore or block SIGPIPE for their own reason forget + * to restore SIGPIPE handling to the default before spawning Git and + * break this carefully orchestrated machinery. + * + * Restore the way SIGPIPE is handled to default, which is what we + * expect. + */ +static void restore_sigpipe_to_default(void) +{ + sigset_t unblock; + + sigemptyset(&unblock); + sigaddset(&unblock, SIGPIPE); + sigprocmask(SIG_UNBLOCK, &unblock, NULL); + signal(SIGPIPE, SIG_DFL); +} + +void init_git(const char **argv) +{ + struct strbuf tmp = STRBUF_INIT; + + trace2_initialize_clock(); + + /* + * Always open file descriptors 0/1/2 to avoid clobbering files + * in die(). It also avoids messing up when the pipes are dup'ed + * onto stdin/stdout/stderr in the child processes we spawn. + */ + sanitize_stdfds(); + restore_sigpipe_to_default(); + + git_resolve_executable_dir(argv[0]); + + setlocale(LC_CTYPE, ""); + git_setup_gettext(); + + initialize_repository(the_repository); + + attr_start(); + + trace2_initialize(); + trace2_cmd_start(argv); + trace2_collect_process_info(TRACE2_PROCESS_INFO_STARTUP); + + if (!strbuf_getcwd(&tmp)) + tmp_original_cwd = strbuf_detach(&tmp, NULL); +} diff --git a/common-init.h b/common-init.h new file mode 100644 index 0000000000..3e6db20cae --- /dev/null +++ b/common-init.h @@ -0,0 +1,6 @@ +#ifndef COMMON_INIT_H +#define COMMON_INIT_H + +void init_git(const char **argv); + +#endif /* COMMON_INIT_H */ diff --git a/common-main.c b/common-main.c index 8e68ac9e42..6b7ab077b0 100644 --- a/common-main.c +++ b/common-main.c @@ -1,92 +1,13 @@ -#define USE_THE_REPOSITORY_VARIABLE - #include "git-compat-util.h" -#include "exec-cmd.h" -#include "gettext.h" -#include "attr.h" -#include "repository.h" -#include "setup.h" -#include "strbuf.h" -#include "trace2.h" - -/* - * Many parts of Git have subprograms communicate via pipe, expect the - * upstream of a pipe to die with SIGPIPE when the downstream of a - * pipe does not need to read all that is written. Some third-party - * programs that ignore or block SIGPIPE for their own reason forget - * to restore SIGPIPE handling to the default before spawning Git and - * break this carefully orchestrated machinery. - * - * Restore the way SIGPIPE is handled to default, which is what we - * expect. - */ -static void restore_sigpipe_to_default(void) -{ - sigset_t unblock; - - sigemptyset(&unblock); - sigaddset(&unblock, SIGPIPE); - sigprocmask(SIG_UNBLOCK, &unblock, NULL); - signal(SIGPIPE, SIG_DFL); -} +#include "common-init.h" int main(int argc, const char **argv) { int result; - struct strbuf tmp = STRBUF_INIT; - - trace2_initialize_clock(); - - /* - * Always open file descriptors 0/1/2 to avoid clobbering files - * in die(). It also avoids messing up when the pipes are dup'ed - * onto stdin/stdout/stderr in the child processes we spawn. - */ - sanitize_stdfds(); - restore_sigpipe_to_default(); - - git_resolve_executable_dir(argv[0]); - - setlocale(LC_CTYPE, ""); - git_setup_gettext(); - - initialize_repository(the_repository); - - attr_start(); - - trace2_initialize(); - trace2_cmd_start(argv); - trace2_collect_process_info(TRACE2_PROCESS_INFO_STARTUP); - - if (!strbuf_getcwd(&tmp)) - tmp_original_cwd = strbuf_detach(&tmp, NULL); + init_git(argv); result = cmd_main(argc, argv); /* Not exit(3), but a wrapper calling our common_exit() */ exit(result); } - -static void check_bug_if_BUG(void) -{ - if (!bug_called_must_BUG) - return; - BUG("on exit(): had bug() call(s) in this process without explicit BUG_if_bug()"); -} - -/* We wrap exit() to call common_exit() in git-compat-util.h */ -int common_exit(const char *file, int line, int code) -{ - /* - * For non-POSIX systems: Take the lowest 8 bits of the "code" - * to e.g. turn -1 into 255. On a POSIX system this is - * redundant, see exit(3) and wait(2), but as it doesn't harm - * anything there we don't need to guard this with an "ifdef". - */ - code &= 0xff; - - check_bug_if_BUG(); - trace2_cmd_exit_fl(file, line, code); - - return code; -} From patchwork Fri Sep 6 22:21:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13794807 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 12325158523 for ; Fri, 6 Sep 2024 22:21:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725661288; cv=none; b=hGeNmPzENzdXV90DjH7L56N9FsWRgG0/Xu6U5OuSw0hTXInqF2pdUIz+Fw6fnqWVYkEPML6Aldbm6gIQUPBqEjJtUQNYasve7lN3ZEypntuoG387qON3fKKGAMn96i6GrpQPe5XUalg2r35CKNpsswZr7VNTVuoW29m0xa5D+ig= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725661288; c=relaxed/simple; bh=Sd8BIE47690g4hh5292UXBSZfJjQxedx1ADOfO0Af+g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TPCd2IQulxjoP49V6LrCOYEVngsG4CGySHWBxXZPmXNfUqvJJba0JO7Gp7ZXZHa/ty8AM7Aw66r+hmZEFIVOS6p6XE4WAoVDW2gPp6nJ3eUkNxHR9N8fxZWsEhKMJ49yNKerzD1Qlv4W8QKqCxsU/gE70L9frs1xXMck3u4mgDc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--calvinwan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jL2B96/F; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--calvinwan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jL2B96/F" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e1d46cee0b0so371518276.2 for ; Fri, 06 Sep 2024 15:21:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725661285; x=1726266085; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=c/tylS5Bc/umtGWc+gOJ2ik/5pN4MZcbPguwyrqTnFc=; b=jL2B96/F6dz5eejtJ3Hmg2q5KHA6HIfUBGjzUKjLLQruHzAfiQkaQr6IBXTBiouoKF zXWmDMRBDZlIJNLaYX+GPyNpG8jAptJ7hVXp31Xcod6gaodCyAORT3XHIoHNYILCFai9 ydz8E87KmlO7XfiJpfXdr7glFvuIwucM9+lEQuqUXfxj7a8velAZ4OTKoBWf/xdZSbwH cP0QXPIm90jtTulJzdevb1ceUkrKp/jbtPi53rniVY3a9AWt5Kf4L8oZv/ojaACH0WaF B68Y2qrmLJ3qFM4rbbBUf4ZLkyFzWKfBk+DWzSxQhvVTyN6k4lqWn3lX0NBgsJRYhvNa XIYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725661285; x=1726266085; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=c/tylS5Bc/umtGWc+gOJ2ik/5pN4MZcbPguwyrqTnFc=; b=AsDGfAgZbq5lNnQxm5Nk+e5fnM0J05A3tUHk+mJEASKqNOk93cOEjJAYDGPsqlwfTz 1y87/+esJDm+mVgArmB5LKoAtiIcYdbf6JDNnT8xteE3mbav+xVobpu0YHMSoqJb7bUw CJFF/SdyI38e90wPp1XFf7ULyxrJRq55EifvYBXAXSMKnnmvXxnNR5LtWADZJjVNlhGm DyWutw7GXlzljQpV6FMA6jhYY29Q5H33Z1wmU0sPwTgh85+MULc3xfVJIp2Afu8o+el8 JijBKtk0zRyudLr1JGQkc0oG3Ps0/F6Mjv6x/hEq3obgkKIU8BHrEDp7Nh9pFRMprD85 4WRw== X-Gm-Message-State: AOJu0YwWKZBLt+An2/EyxMtfISFxtfLjNtYnsRLpmfwPiNuK1sb9tXHB iU015YNLIvdHQqqYexpUi1a/hN171ifPUKx/1yOL85aiLOe4n89y/xdaHplblO9MusehfxtAVrP XMQCC7zOy2dLSteFLbi4DKXN5SlIpZKoSEkQf1l5jxHKPKUNnI041fI4BvMz99B4RZsMGfREWH7 tAc6URNKofamABColGNU4c/HHrWV0CfPQ04HxNmaVLNV0X X-Google-Smtp-Source: AGHT+IE77Y6GfusAkPgE0CATljPQ4lPK+HzP42kLZyQV4mhzHG1yUj2mNKbcwNiaKhIRwfv6TNlaAaE1Hbmr8ak= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a25:d842:0:b0:e16:6d88:b8c2 with SMTP id 3f1490d57ef6-e1d34890f4fmr8310276.4.1725661284582; Fri, 06 Sep 2024 15:21:24 -0700 (PDT) Date: Fri, 6 Sep 2024 22:21:12 +0000 In-Reply-To: <20240906221853.257984-1-calvinwan@google.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240906221853.257984-1-calvinwan@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240906222116.270196-2-calvinwan@google.com> Subject: [PATCH v3 2/6] libgit-sys: introduce Rust wrapper for libgit.a From: Calvin Wan To: git@vger.kernel.org Cc: Josh Steadmon , spectral@google.com, emilyshaffer@google.com, emrass@google.com, rsbecker@nexbridge.com, gitster@pobox.com, mh@glandium.org, sandals@crustytoothpaste.net, Jason@zx2c4.com, dsimic@manjaro.org, Calvin Wan From: Josh Steadmon Introduce libgit-sys, a Rust wrapper crate that allows Rust code to call functions in libgit.a. This initial patch defines build rules and an interface that exposes user agent string getter functions as a proof of concept. This library can be tested with `cargo test`. In later commits, a higher-level library containing a more Rust-friendly interface will be added at `contrib/libgit-rs`. Symbols in libgit can collide with symbols from other libraries such as libgit2. We avoid this by first exposing library symbols in public_symbol_export.[ch]. These symbols are prepended with "libgit_" to avoid collisions and set to visible using a visibility pragma. In build.rs, Rust builds contrib/libgit-rs/libgit-sys/libgitpub.a, which also contains libgit.a and other dependent libraries, with -fvisibility=hidden to hide all symbols within those libraries that haven't been exposed with a visibility pragma. Co-authored-by: Kyle Lippincott Co-authored-by: Calvin Wan Signed-off-by: Calvin Wan Signed-off-by: Kyle Lippincott Change-Id: Ibb98a3907e69839231355c098b2488ef06d1ba45 --- .gitignore | 1 + Makefile | 13 +++++ contrib/libgit-rs/libgit-sys/Cargo.lock | 55 +++++++++++++++++++ contrib/libgit-rs/libgit-sys/Cargo.toml | 12 ++++ contrib/libgit-rs/libgit-sys/README.md | 15 +++++ contrib/libgit-rs/libgit-sys/build.rs | 31 +++++++++++ .../libgit-sys/public_symbol_export.c | 20 +++++++ .../libgit-sys/public_symbol_export.h | 8 +++ contrib/libgit-rs/libgit-sys/src/lib.rs | 42 ++++++++++++++ 9 files changed, 197 insertions(+) create mode 100644 contrib/libgit-rs/libgit-sys/Cargo.lock create mode 100644 contrib/libgit-rs/libgit-sys/Cargo.toml create mode 100644 contrib/libgit-rs/libgit-sys/README.md create mode 100644 contrib/libgit-rs/libgit-sys/build.rs create mode 100644 contrib/libgit-rs/libgit-sys/public_symbol_export.c create mode 100644 contrib/libgit-rs/libgit-sys/public_symbol_export.h create mode 100644 contrib/libgit-rs/libgit-sys/src/lib.rs diff --git a/.gitignore b/.gitignore index 8caf3700c2..dfd72820fb 100644 --- a/.gitignore +++ b/.gitignore @@ -248,3 +248,4 @@ Release/ /git.VC.db *.dSYM /contrib/buildsystems/out +/contrib/libgit-rs/libgit-sys/target diff --git a/Makefile b/Makefile index 7caeb3c872..0090514e55 100644 --- a/Makefile +++ b/Makefile @@ -653,6 +653,8 @@ CURL_CONFIG = curl-config GCOV = gcov STRIP = strip SPATCH = spatch +LD = ld +OBJCOPY = objcopy export TCL_PATH TCLTK_PATH @@ -2713,6 +2715,7 @@ OBJECTS += $(XDIFF_OBJS) OBJECTS += $(FUZZ_OBJS) OBJECTS += $(REFTABLE_OBJS) $(REFTABLE_TEST_OBJS) OBJECTS += $(UNIT_TEST_OBJS) +OBJECTS += contrib/libgit-rs/libgit-sys/public_symbol_export.o ifndef NO_CURL OBJECTS += http.o http-walker.o remote-curl.o @@ -3720,6 +3723,7 @@ clean: profile-clean coverage-clean cocciclean $(RM) $(htmldocs).tar.gz $(manpages).tar.gz $(MAKE) -C Documentation/ clean $(RM) Documentation/GIT-EXCLUDED-PROGRAMS + $(RM) -r contrib/libgit-rs/libgit-sys/target ifndef NO_PERL $(RM) -r perl/build/ endif @@ -3865,3 +3869,12 @@ $(UNIT_TEST_PROGS): $(UNIT_TEST_BIN)/%$X: $(UNIT_TEST_DIR)/%.o \ build-unit-tests: $(UNIT_TEST_PROGS) unit-tests: $(UNIT_TEST_PROGS) t/helper/test-tool$X $(MAKE) -C t/ unit-tests + +contrib/libgit-rs/libgit-sys/partial_symbol_export.o: contrib/libgit-rs/libgit-sys/public_symbol_export.o libgit.a reftable/libreftable.a xdiff/lib.a + $(LD) -r $^ -o $@ + +contrib/libgit-rs/libgit-sys/hidden_symbol_export.o: contrib/libgit-rs/libgit-sys/partial_symbol_export.o + $(OBJCOPY) --localize-hidden $^ $@ + +contrib/libgit-rs/libgit-sys/libgitpub.a: contrib/libgit-rs/libgit-sys/hidden_symbol_export.o + $(AR) $(ARFLAGS) $@ $^ diff --git a/contrib/libgit-rs/libgit-sys/Cargo.lock b/contrib/libgit-rs/libgit-sys/Cargo.lock new file mode 100644 index 0000000000..af8676ab42 --- /dev/null +++ b/contrib/libgit-rs/libgit-sys/Cargo.lock @@ -0,0 +1,55 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cc" +version = "1.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6" +dependencies = [ + "shlex", +] + +[[package]] +name = "libc" +version = "0.2.158" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" + +[[package]] +name = "libgit-sys" +version = "0.1.0" +dependencies = [ + "libz-sys", +] + +[[package]] +name = "libz-sys" +version = "1.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2d16453e800a8cf6dd2fc3eb4bc99b786a9b90c663b8559a5b1a041bf89e472" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "pkg-config" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" diff --git a/contrib/libgit-rs/libgit-sys/Cargo.toml b/contrib/libgit-rs/libgit-sys/Cargo.toml new file mode 100644 index 0000000000..905fd6774f --- /dev/null +++ b/contrib/libgit-rs/libgit-sys/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "libgit-sys" +version = "0.1.0" +edition = "2021" +build = "build.rs" +links = "gitpub" + +[lib] +path = "src/lib.rs" + +[dependencies] +libz-sys = "1.1.19" \ No newline at end of file diff --git a/contrib/libgit-rs/libgit-sys/README.md b/contrib/libgit-rs/libgit-sys/README.md new file mode 100644 index 0000000000..7a59602c30 --- /dev/null +++ b/contrib/libgit-rs/libgit-sys/README.md @@ -0,0 +1,15 @@ +# cgit-info + +A small hacky proof-of-concept showing how to provide a Rust FFI for the Git +library. + +## Building + +`cargo build` automatically builds and picks up on changes made to both +the Rust wrapper and git.git code so there is no need to run `make` +beforehand. + +## Running + +Assuming you don't make any changes to the Git source, you can just work from +`contrib/cgit-rs` and use `cargo build` or `cargo run` as usual. diff --git a/contrib/libgit-rs/libgit-sys/build.rs b/contrib/libgit-rs/libgit-sys/build.rs new file mode 100644 index 0000000000..c187ab6cd0 --- /dev/null +++ b/contrib/libgit-rs/libgit-sys/build.rs @@ -0,0 +1,31 @@ +use std::env; +use std::path::PathBuf; + +pub fn main() -> std::io::Result<()> { + let crate_root = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); + let git_root = crate_root.join("../../.."); + let dst = PathBuf::from(env::var_os("OUT_DIR").unwrap()); + + let make_output = std::process::Command::new("make") + .env_remove("PROFILE") + .current_dir(git_root.clone()) + .args([ + "CFLAGS=-fvisibility=hidden", + "contrib/libgit-rs/libgit-sys/libgitpub.a", + ]) + .output() + .expect("Make failed to run"); + if !make_output.status.success() { + panic!( + "Make failed:\n stdout = {}\n stderr = {}\n", + String::from_utf8(make_output.stdout).unwrap(), + String::from_utf8(make_output.stderr).unwrap() + ); + } + std::fs::copy(crate_root.join("libgitpub.a"), dst.join("libgitpub.a"))?; + println!("cargo::rustc-link-search=native={}", dst.display()); + println!("cargo::rustc-link-lib=gitpub"); + println!("cargo::rerun-if-changed={}", git_root.display()); + + Ok(()) +} diff --git a/contrib/libgit-rs/libgit-sys/public_symbol_export.c b/contrib/libgit-rs/libgit-sys/public_symbol_export.c new file mode 100644 index 0000000000..39c27d9c1a --- /dev/null +++ b/contrib/libgit-rs/libgit-sys/public_symbol_export.c @@ -0,0 +1,20 @@ +// Shim to publicly export Git symbols. These must be renamed so that the +// original symbols can be hidden. Renaming these with a "libgit_" prefix also +// avoid conflicts with other libraries such as libgit2. + +#include "contrib/libgit-rs/libgit-sys/public_symbol_export.h" +#include "version.h" + +#pragma GCC visibility push(default) + +const char *libgit_user_agent(void) +{ + return git_user_agent(); +} + +const char *libgit_user_agent_sanitized(void) +{ + return git_user_agent_sanitized(); +} + +#pragma GCC visibility pop diff --git a/contrib/libgit-rs/libgit-sys/public_symbol_export.h b/contrib/libgit-rs/libgit-sys/public_symbol_export.h new file mode 100644 index 0000000000..a3372f93fa --- /dev/null +++ b/contrib/libgit-rs/libgit-sys/public_symbol_export.h @@ -0,0 +1,8 @@ +#ifndef PUBLIC_SYMBOL_EXPORT_H +#define PUBLIC_SYMBOL_EXPORT_H + +const char *libgit_user_agent(void); + +const char *libgit_user_agent_sanitized(void); + +#endif /* PUBLIC_SYMBOL_EXPORT_H */ diff --git a/contrib/libgit-rs/libgit-sys/src/lib.rs b/contrib/libgit-rs/libgit-sys/src/lib.rs new file mode 100644 index 0000000000..346b26083d --- /dev/null +++ b/contrib/libgit-rs/libgit-sys/src/lib.rs @@ -0,0 +1,42 @@ +use std::ffi::c_char; + +extern crate libz_sys; + +extern "C" { + // From version.c + pub fn libgit_user_agent() -> *const c_char; + pub fn libgit_user_agent_sanitized() -> *const c_char; +} + +#[cfg(test)] +mod tests { + use std::ffi::CStr; + + use super::*; + + #[test] + fn user_agent_starts_with_git() { + let c_str = unsafe { CStr::from_ptr(libgit_user_agent()) }; + let agent = c_str + .to_str() + .expect("User agent contains invalid UTF-8 data"); + assert!( + agent.starts_with("git/"), + r#"Expected user agent to start with "git/", got: {}"#, + agent + ); + } + + #[test] + fn sanitized_user_agent_starts_with_git() { + let c_str = unsafe { CStr::from_ptr(libgit_user_agent_sanitized()) }; + let agent = c_str + .to_str() + .expect("Sanitized user agent contains invalid UTF-8 data"); + assert!( + agent.starts_with("git/"), + r#"Expected user agent to start with "git/", got: {}"#, + agent + ); + } +} From patchwork Fri Sep 6 22:21:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13794808 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 965A81B85D9 for ; Fri, 6 Sep 2024 22:21:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725661289; cv=none; b=KieHiaiRDNhZ/c6TlYGmGYs3GgxpkNcbVnpIxOJa7NQWTUtXal84IJzEiBDsKLlYKHX7CF6edjnTc0a80F932g3JMdNCat6UDBTjLtTSHtfCKCx4tmYlEdswM+AJhZZjlnr7PvFIh+x679iSQBf/yyeoQNDXrBTjkxliskCKxA0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725661289; c=relaxed/simple; bh=N88vkX129rqZ+DWkQlIs/o/NSbG07lAItLOzzw16+5A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qTLDjz7TVtdvMOEgD/n9TuzhPOTR7DekRsQsH2VJOIxoRnMq1+pyyYlLC+weuvD/8VHCIcy6+6eA5ZoJuf1tl5MoaB7LQBIl7n0WjiPigRZ36F8NwiSk0fI1q+PCqxD+PjjTXTO4l/0x3jBLzCqD21YN6pgsItM3QdufYXMdQ7c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--calvinwan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sODR6Hsk; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--calvinwan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sODR6Hsk" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6d5fccc3548so58637827b3.1 for ; Fri, 06 Sep 2024 15:21:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725661286; x=1726266086; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tk1afKTSB6oTJ5sAfEAwQadSx+VjBdujEI5TGPbVZlM=; b=sODR6HskevPrw0j5pAXFGN9GaiZebedNu9Om6p0xAg3UO1esaOytLOZe895BAusTRc FGdupfLA9ydHd0xjYHLPvVyrDGaTYHByisTSJIgXFElYTN2WQl+cbR7chUoLoyiY/vVd ZvEgVdKTp9YdUQBdRoGDzdyuCFV3hNSvqT/uxbwEmlU/ubiG8OjBybST0o9aTWYBexda K63s1+mquXQHhm2THgqOhIuGVXE3SmvLoUzYLiwxr8I0l5IxXZQ5X1VoLCWUmEA1y5TE Apj/WzRQ4gC1CienGD4pd3Ybg2zvtRWovCNFjPHtkoJxsuh4eWpFsYlKrJXWnQp6HJMf 5Zew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725661286; x=1726266086; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tk1afKTSB6oTJ5sAfEAwQadSx+VjBdujEI5TGPbVZlM=; b=pH4DU4G55mqmM55V+7psXXKR6beAYpKmUj6BYWZV0KYQOMo689hJQ+gpEeoR/10bP9 7gTMSXK4paqnvLYQfYzx2eZrR0ZBc+ZYErbaOufOqfZPnMGX9J6wWolJqhGF+7WgUUK5 mDt4gxONE4ORclI+lxlkVF8uP5ByNSavkmAIQxicCN9VTdpqbKFnItyfEoBSf3Ov11uZ XH/fTZ/LJGH/9ioogPJ1qzLvlqaV4Fg3R2wfTECFyYDtcidbJqczfOGNYV//pLbdMUQd J5MGyg9athd/wZx5GrMFGs6b1AJ86wjC8V2uiLxZ7AOVhuQtM9gxkRjuWozvwlUuwXDA nfgw== X-Gm-Message-State: AOJu0YxcoctU00rlduKf2vQCRzyu9cl4BPTx8cZy1duFzK+6+172b3i+ ZdxagFF/eglgbA900rWUyN25eTC538nfJ1DdFJwd8PMFRG3MA6gaSE/XgFEy+2v0pSuDtBmJFq8 R6z04KEergw52B1poT728Vpzhsx1ikwDmn0O/0w9GQS0Mr0kvWlCqST7nOy9cJhqGw1cPktYMQ7 68+SWY87l4ebpL0sQfFMLhuYZTYsUu5+31L9l99WrY96W/ X-Google-Smtp-Source: AGHT+IH0kT3bSJbOkspSxfdqAO5xEBMbwXZCTOkXktl07RCr8IY0/visDqTGC+X6OMWZrOnqGcYS560voMxA0+g= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a05:690c:460c:b0:6d3:e7e6:8460 with SMTP id 00721157ae682-6db4426b0ecmr744197b3.1.1725661285980; Fri, 06 Sep 2024 15:21:25 -0700 (PDT) Date: Fri, 6 Sep 2024 22:21:13 +0000 In-Reply-To: <20240906221853.257984-1-calvinwan@google.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240906221853.257984-1-calvinwan@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240906222116.270196-3-calvinwan@google.com> Subject: [PATCH v3 3/6] libgit-sys: add repo initialization and config access From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan , steadmon@google.com, spectral@google.com, emilyshaffer@google.com, emrass@google.com, rsbecker@nexbridge.com, gitster@pobox.com, mh@glandium.org, sandals@crustytoothpaste.net, Jason@zx2c4.com, dsimic@manjaro.org Wrap a few repo setup and config access functions in libgit-sys. These were selected as proof-of-concept items to show that we can access local config from Rust. Co-authored-by: Josh Steadmon Signed-off-by: Calvin Wan Change-Id: I6dd886af8c63e1f0f3251064cd8903aecdf768bb --- .../libgit-sys/public_symbol_export.c | 26 +++++++++ .../libgit-sys/public_symbol_export.h | 8 +++ contrib/libgit-rs/libgit-sys/src/lib.rs | 57 ++++++++++++++++++- 3 files changed, 89 insertions(+), 2 deletions(-) diff --git a/contrib/libgit-rs/libgit-sys/public_symbol_export.c b/contrib/libgit-rs/libgit-sys/public_symbol_export.c index 39c27d9c1a..65d1620d28 100644 --- a/contrib/libgit-rs/libgit-sys/public_symbol_export.c +++ b/contrib/libgit-rs/libgit-sys/public_symbol_export.c @@ -2,11 +2,37 @@ // original symbols can be hidden. Renaming these with a "libgit_" prefix also // avoid conflicts with other libraries such as libgit2. +#include "git-compat-util.h" #include "contrib/libgit-rs/libgit-sys/public_symbol_export.h" +#include "common-init.h" +#include "config.h" +#include "setup.h" #include "version.h" +extern struct repository *the_repository; + #pragma GCC visibility push(default) +const char *libgit_setup_git_directory(void) +{ + return setup_git_directory(); +} + +int libgit_config_get_int(const char *key, int *dest) +{ + return repo_config_get_int(the_repository, key, dest); +} + +void libgit_init_git(const char **argv) +{ + init_git(argv); +} + +int libgit_parse_maybe_bool(const char *val) +{ + return git_parse_maybe_bool(val); +} + const char *libgit_user_agent(void) { return git_user_agent(); diff --git a/contrib/libgit-rs/libgit-sys/public_symbol_export.h b/contrib/libgit-rs/libgit-sys/public_symbol_export.h index a3372f93fa..64332f30de 100644 --- a/contrib/libgit-rs/libgit-sys/public_symbol_export.h +++ b/contrib/libgit-rs/libgit-sys/public_symbol_export.h @@ -1,6 +1,14 @@ #ifndef PUBLIC_SYMBOL_EXPORT_H #define PUBLIC_SYMBOL_EXPORT_H +const char *libgit_setup_git_directory(void); + +int libgit_config_get_int(const char *key, int *dest); + +void libgit_init_git(const char **argv); + +int libgit_parse_maybe_bool(const char *val); + const char *libgit_user_agent(void); const char *libgit_user_agent_sanitized(void); diff --git a/contrib/libgit-rs/libgit-sys/src/lib.rs b/contrib/libgit-rs/libgit-sys/src/lib.rs index 346b26083d..9b2e85dff8 100644 --- a/contrib/libgit-rs/libgit-sys/src/lib.rs +++ b/contrib/libgit-rs/libgit-sys/src/lib.rs @@ -1,8 +1,19 @@ -use std::ffi::c_char; +use std::ffi::{c_char, c_int}; extern crate libz_sys; extern "C" { + pub fn libgit_setup_git_directory() -> *const c_char; + + // From config.c + pub fn libgit_config_get_int(key: *const c_char, dest: *mut c_int) -> c_int; + + // From common-init.c + pub fn libgit_init_git(argv: *const *const c_char); + + // From parse.c + pub fn libgit_parse_maybe_bool(val: *const c_char) -> c_int; + // From version.c pub fn libgit_user_agent() -> *const c_char; pub fn libgit_user_agent_sanitized() -> *const c_char; @@ -10,7 +21,7 @@ extern "C" { #[cfg(test)] mod tests { - use std::ffi::CStr; + use std::ffi::{CStr, CString}; use super::*; @@ -39,4 +50,46 @@ mod tests { agent ); } + + #[test] + fn parse_bools_from_strings() { + let arg = CString::new("true").unwrap(); + assert_eq!(unsafe { libgit_parse_maybe_bool(arg.as_ptr()) }, 1); + + let arg = CString::new("yes").unwrap(); + assert_eq!(unsafe { libgit_parse_maybe_bool(arg.as_ptr()) }, 1); + + let arg = CString::new("false").unwrap(); + assert_eq!(unsafe { libgit_parse_maybe_bool(arg.as_ptr()) }, 0); + + let arg = CString::new("no").unwrap(); + assert_eq!(unsafe { libgit_parse_maybe_bool(arg.as_ptr()) }, 0); + + let arg = CString::new("maybe").unwrap(); + assert_eq!(unsafe { libgit_parse_maybe_bool(arg.as_ptr()) }, -1); + } + + #[test] + fn access_configs() { + /* + * Since we can't easily set a config for the local repo, test + * for the existence of a key rather than a specific value + */ + let fake_argv = [std::ptr::null::()]; + unsafe { + libgit_init_git(fake_argv.as_ptr()); + libgit_setup_git_directory(); + } + let mut dest: c_int = 0; + let key = CString::new("core.repositoryformatversion").unwrap(); + unsafe { + let val = libgit_config_get_int(key.as_ptr(), &mut dest as *mut i32); + assert_eq!(val, 0) + }; + let missing_key = CString::new("foo.bar").unwrap(); + unsafe { + let val = libgit_config_get_int(missing_key.as_ptr(), &mut dest as *mut i32); + assert_eq!(val, 1) + }; + } } From patchwork Fri Sep 6 22:21:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13794809 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 89F4615ADB1 for ; Fri, 6 Sep 2024 22:21:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725661290; cv=none; b=IfNGlnGozjKY7zi1Ci+zQIOe4/OZdkPQc57+ZwVw+hJxlSqiLa5Q4LUvCv/E2kdIAY4fmHPulVFOKZjcVpty/hf2ikztLPlGhXRgxF94eoTjCfq2Cq47PefYIKzgDKB+Fk47kEfFg2FNOeD6wMcef/ek4FQ12LFVGFpEldPtPCI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725661290; c=relaxed/simple; bh=IXmU79gdG/icTWTSWtZq6xpmqqsEzvlYOuCdyVQRf08=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MTSIPic4RNJ4o0zoyMGVjkmHoTkL/MoFrSEzu/6xlxOZEXgl9tNlETHpoLhBH5Wta1khY/cOxUccwuF58HXAIeLAJ1IkkjAJIYXPEahmVpOKFltqsnH8od5l7swaxSZWY84RSSl5HjhtcGviAYNJk2pA9SMIOcBxrUHYiDnT0Jo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--calvinwan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hvKMYq6/; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--calvinwan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hvKMYq6/" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2053f49d0c9so41591065ad.1 for ; Fri, 06 Sep 2024 15:21:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725661289; x=1726266089; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=UixZiE6IiF+TJjsM/CfMJgptyLdm7/OvGi1tWJyuDLc=; b=hvKMYq6/KzAWeJxztUsT9HkT9moGP961YPUModBSZiN+UAEJb4IyB+9ktPofga9u+O XYBeF53AYSa3jYJwB3uMr7YvtVJN1sJCc4l+CzaH5nYBvIh1vGeg0b+4zZvVC1hUdy7C uo2iVra+qWsEvEkEdOB43Zjir7V+7ItuDjSsaUZ5O4y0UHiv02DWXrj5u2aPXio7Mamx QWbSsx2tkWIfRGl5gj58EgWxiKWtUCKTVuJqI7hklcfwUGj60d1J/p07bBpycyhofyCn KC3Hrm1UNOF23+rNEOYufRfCM1/dCyCYEgtVdfj0GkwiAO5/LS3HRP0Dvm2zBqA9BSsZ 8E6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725661289; x=1726266089; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UixZiE6IiF+TJjsM/CfMJgptyLdm7/OvGi1tWJyuDLc=; b=UWXSScJWzfRULtQtt0TtWR0EFzrUHuvbzlMKM81oNTkll7817hdd8uT5y5qLE5LhVk ywD/Ut8nZorDab/0nSbrTCsneI/+vz55ad8TwR6qUPNBCoS5s0VkZxGbD/Kti78VttEc nUHw/iFRF/uLxK9bYGaku4jBW5FnxAhADOqn/uLlmFzQbwhKRvlNg5CTOa3ONRCGJM2Y k1pKme43ru22egTvdyJ1uBASlsfuGqxkPxA8G5rspILmj2tVqwrGXJ1uiXZDTq7c7qQ7 Lw/AQ91ZwyJpM4OAqaLwQJd4gDuQ6fH2k37UlPTFASJLqFmYoKC3GDkAd6qhnGtrAKug xxfQ== X-Gm-Message-State: AOJu0YzcErn2bdrN1GATCqd8O5qbG011IHdgKt7drd8nun8AP7FSLZko 1/3pP3WsdKlz4PsjnUj66SLCdQxhXo/vnSOK3kYUf+wqpvNv0LiK5yy3KjJbnaweSIbhquVZmgD wmrB5k22MzKC6r/DKQ8e6slBaPewtYN8BkIZL6G5Hu4ABVWE+AeJsT8l/vKRrbtN8vJXto0LA+y PVg0gbR4sQJtMnyIaQhsG3Vq6fDZtBimVStyptovU/kKfo X-Google-Smtp-Source: AGHT+IEKrLCpVw6QqaE2tCzKYQldTu0Kt2VdBFu98IGjQyUVgYmugpO2DIEgRsl3ShYbZHw2u8hVeqdPehp6vFs= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a17:902:d503:b0:201:cdcd:4227 with SMTP id d9443c01a7336-206eebdb9f9mr3744475ad.6.1725661287536; Fri, 06 Sep 2024 15:21:27 -0700 (PDT) Date: Fri, 6 Sep 2024 22:21:14 +0000 In-Reply-To: <20240906221853.257984-1-calvinwan@google.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240906221853.257984-1-calvinwan@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240906222116.270196-4-calvinwan@google.com> Subject: [PATCH v3 4/6] config: add git_configset_alloc() and git_configset_clear_and_free() From: Calvin Wan To: git@vger.kernel.org Cc: Josh Steadmon , spectral@google.com, emilyshaffer@google.com, emrass@google.com, rsbecker@nexbridge.com, gitster@pobox.com, mh@glandium.org, sandals@crustytoothpaste.net, Jason@zx2c4.com, dsimic@manjaro.org, Calvin Wan From: Josh Steadmon Add git_configset_alloc() and git_configset_clear_and_free() functions so that callers can manage config_set structs on the heap. This also allows non-C external consumers to treat config_sets as opaque structs. Co-authored-by: Calvin Wan Signed-off-by: Calvin Wan Change-Id: I21684c87691d978e3303e04324428aa3567dbd70 --- config.c | 11 +++++++++++ config.h | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/config.c b/config.c index 6421894614..444db8de79 100644 --- a/config.c +++ b/config.c @@ -2324,6 +2324,11 @@ static int config_set_element_cmp(const void *cmp_data UNUSED, return strcmp(e1->key, e2->key); } +struct config_set *git_configset_alloc(void) +{ + return xmalloc(sizeof(struct config_set)); +} + void git_configset_init(struct config_set *set) { hashmap_init(&set->config_hash, config_set_element_cmp, NULL, 0); @@ -2353,6 +2358,12 @@ void git_configset_clear(struct config_set *set) set->list.items = NULL; } +void git_configset_clear_and_free(struct config_set *set) +{ + git_configset_clear(set); + free(set); +} + static int config_set_callback(const char *key, const char *value, const struct config_context *ctx, void *cb) diff --git a/config.h b/config.h index 54b47dec9e..074c85a788 100644 --- a/config.h +++ b/config.h @@ -472,6 +472,11 @@ struct config_set { struct configset_list list; }; +/** + * Alloc a config_set + */ +struct config_set *git_configset_alloc(void); + /** * Initializes the config_set `cs`. */ @@ -520,6 +525,11 @@ int git_configset_get_string_multi(struct config_set *cs, const char *key, */ void git_configset_clear(struct config_set *cs); +/** + * Clears and frees a heap-allocated `config_set` structure. + */ +void git_configset_clear_and_free(struct config_set *cs); + /* * These functions return 1 if not found, and 0 if found, leaving the found * value in the 'dest' pointer. From patchwork Fri Sep 6 22:21:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13794810 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 04C1C1B85D9 for ; Fri, 6 Sep 2024 22:21:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725661292; cv=none; b=oXMBC0dTRojJsrWcA0bkD4peqmyQLnGYOhqJthqWvH77WvhzTMQSeAk2oM2GIvOB+//vA5hJ4r662XmmDA8Zjwu4t1ZxCO2ezeTsqZmsfQHIPx0wMnqWTL2sFN/SzDq0rhx+afosJYf50eyLOmVqoTu4eVdpQhY+9DmldT0PeAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725661292; c=relaxed/simple; bh=cU8JeOlugnySnSvOa20nP1BVDRB1P/P7GHTttxVpW1Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=T+ifJQDTMUX1UoAayptjNupbBv8TzbeuMozdFkfx21FlxKdDOijVquvSdPBXCExio5zDxA/H8U/uPJ5IGnQ3xAPU2MAtL73hVqDs+YPOpGxfAq9RMVryXT5j3GKQvfRQ3bJ7q30o3X3djX6IQ07Esi66pJPt0nZyGlqrYQchCMg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--calvinwan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Q7GSwO7n; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--calvinwan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Q7GSwO7n" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6d4daca4a4bso79538307b3.0 for ; Fri, 06 Sep 2024 15:21:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725661290; x=1726266090; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=236+OPE9lZBsJ7XrTRhPbpOQ8RkJHgGiaDPgxyOr83M=; b=Q7GSwO7nLawfiGl7CxKL3eNIhlicxw2fQF0FiLD+4OqhUkVMFOx+BkbLskEJGZP3gH K98BPvoULH20s4a+P1vy8kqJHpfMn1T0hwZRATvMURjeahCx+jy3UCfLSU2gsykd4nS6 3l8kK1tCf4YEJQ+gsYkA3Iz05OwM2yYQhm8df0aIoSukWPdOVHV1Wq8fb/eOJOLnyY26 YMU09DsByPh99yYSv7RweMa/7NQr1XXFCBjVzkQacuUutyno6tawBFFqXGbb6feAytaM ZxWDw/0QqT9IOVblgNkoqYxDiFHC53F3EdNk85nJeupS4wDzUewx4Tw/Kbp7bzOcHFxN 5zLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725661290; x=1726266090; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=236+OPE9lZBsJ7XrTRhPbpOQ8RkJHgGiaDPgxyOr83M=; b=nlp3Dnl1HZ8cB9WdzKCm+w8gZH9vYYz76cIRfF+AvgydxITm3BnnZys3N3JX+3KSmE bcNwQP7Im7qRmhOSd3AGON4QwnFXQUBHk8PBczfM6Fs8aJfHRkRRQbieEF9/YGhRzh8f AK0dq8tRBHBH3NB9Oc5heKbASLTITQvsHzCR83ucIMAoE1Yxv1p4h4mG5e3nMS1YyL0c oSiQQh5BEBPOCm1NstUWH0o0S3mfCAyM1xZoKcmfTF1anYXEeCdALR3ku5G0Nf9Gdlwz fiz/LJR2097E2gUEIsPJvualS5LXetKTvWA8m8fyT3EqZD5abgbt8omzuazHwEY1NXRY m3tw== X-Gm-Message-State: AOJu0Yw5qNRalsf/pGlCLkHaMvHjqoOgNBahq/X4oGuoDCInQ48+hNn9 poycjWlyE3MpqbxcRW4XNZxRE0KRBCD0ixF4xZq+vzkcCHNqABBNHd8RfVk7TPkgvskPH8dxENB SU4BvOU0U02OzHnD1DHQtTP3cKOJha7TZ4IAB3zp3kedcWyuxvob5sNtm692O17o4vSK9JcuU6H Avqy3FD0kUWOzdQNzzzCI1bxbMZW8tVuYmdo4fOoOqr1f+ X-Google-Smtp-Source: AGHT+IHFz9cB+etVcaChRJXDzzuXpc8JktBbbZbFL9u6656ZiW/iGBJvIz4qh3Ic1Qc57GCpSvzrt5KB/HYfV3c= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a25:aa88:0:b0:e1a:a114:d35 with SMTP id 3f1490d57ef6-e1d10aa8eafmr81460276.5.1725661289335; Fri, 06 Sep 2024 15:21:29 -0700 (PDT) Date: Fri, 6 Sep 2024 22:21:15 +0000 In-Reply-To: <20240906221853.257984-1-calvinwan@google.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240906221853.257984-1-calvinwan@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240906222116.270196-5-calvinwan@google.com> Subject: [PATCH v3 5/6] libgit: add higher-level libgit crate From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan , steadmon@google.com, spectral@google.com, emilyshaffer@google.com, emrass@google.com, rsbecker@nexbridge.com, gitster@pobox.com, mh@glandium.org, sandals@crustytoothpaste.net, Jason@zx2c4.com, dsimic@manjaro.org Wrap `struct config_set` and a few of its associated functions in libgit-sys. Also introduce a higher-level "libgit" crate which provides a more Rust-friendly interface to config_set structs. Co-authored-by: Josh Steadmon Signed-off-by: Calvin Wan Change-Id: If85e77809434f0ef05e2db35132ecf84621e10ef --- .gitignore | 1 + Makefile | 2 +- contrib/libgit-rs/Cargo.lock | 62 ++++++++++++++ contrib/libgit-rs/Cargo.toml | 10 +++ .../libgit-sys/public_symbol_export.c | 30 +++++++ .../libgit-sys/public_symbol_export.h | 12 +++ contrib/libgit-rs/libgit-sys/src/lib.rs | 31 ++++++- contrib/libgit-rs/src/lib.rs | 85 +++++++++++++++++++ contrib/libgit-rs/testdata/config1 | 2 + contrib/libgit-rs/testdata/config2 | 2 + contrib/libgit-rs/testdata/config3 | 2 + 11 files changed, 237 insertions(+), 2 deletions(-) create mode 100644 contrib/libgit-rs/Cargo.lock create mode 100644 contrib/libgit-rs/Cargo.toml create mode 100644 contrib/libgit-rs/src/lib.rs create mode 100644 contrib/libgit-rs/testdata/config1 create mode 100644 contrib/libgit-rs/testdata/config2 create mode 100644 contrib/libgit-rs/testdata/config3 diff --git a/.gitignore b/.gitignore index dfd72820fb..0a42f27117 100644 --- a/.gitignore +++ b/.gitignore @@ -248,4 +248,5 @@ Release/ /git.VC.db *.dSYM /contrib/buildsystems/out +/contrib/libgit-rs/target /contrib/libgit-rs/libgit-sys/target diff --git a/Makefile b/Makefile index 0090514e55..abeee01d9e 100644 --- a/Makefile +++ b/Makefile @@ -3723,7 +3723,7 @@ clean: profile-clean coverage-clean cocciclean $(RM) $(htmldocs).tar.gz $(manpages).tar.gz $(MAKE) -C Documentation/ clean $(RM) Documentation/GIT-EXCLUDED-PROGRAMS - $(RM) -r contrib/libgit-rs/libgit-sys/target + $(RM) -r contrib/libgit-rs/target contrib/libgit-rs/libgit-sys/target ifndef NO_PERL $(RM) -r perl/build/ endif diff --git a/contrib/libgit-rs/Cargo.lock b/contrib/libgit-rs/Cargo.lock new file mode 100644 index 0000000000..187176f5df --- /dev/null +++ b/contrib/libgit-rs/Cargo.lock @@ -0,0 +1,62 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cc" +version = "1.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6" +dependencies = [ + "shlex", +] + +[[package]] +name = "libc" +version = "0.2.158" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" + +[[package]] +name = "libgit" +version = "0.1.0" +dependencies = [ + "libgit-sys", +] + +[[package]] +name = "libgit-sys" +version = "0.1.0" +dependencies = [ + "libz-sys", +] + +[[package]] +name = "libz-sys" +version = "1.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2d16453e800a8cf6dd2fc3eb4bc99b786a9b90c663b8559a5b1a041bf89e472" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "pkg-config" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" diff --git a/contrib/libgit-rs/Cargo.toml b/contrib/libgit-rs/Cargo.toml new file mode 100644 index 0000000000..b8914517e0 --- /dev/null +++ b/contrib/libgit-rs/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "libgit" +version = "0.1.0" +edition = "2021" + +[lib] +path = "src/lib.rs" + +[dependencies] +libgit-sys = { version = "0.1.0", path = "libgit-sys" } diff --git a/contrib/libgit-rs/libgit-sys/public_symbol_export.c b/contrib/libgit-rs/libgit-sys/public_symbol_export.c index 65d1620d28..07d6bfdd84 100644 --- a/contrib/libgit-rs/libgit-sys/public_symbol_export.c +++ b/contrib/libgit-rs/libgit-sys/public_symbol_export.c @@ -33,6 +33,36 @@ int libgit_parse_maybe_bool(const char *val) return git_parse_maybe_bool(val); } +struct libgit_config_set *libgit_configset_alloc(void) +{ + return git_configset_alloc(); +} + +void libgit_configset_clear_and_free(struct libgit_config_set *cs) +{ + git_configset_clear_and_free(cs); +} + +void libgit_configset_init(struct libgit_config_set *cs) +{ + git_configset_init(cs); +} + +int libgit_configset_add_file(struct libgit_config_set *cs, const char *filename) +{ + return git_configset_add_file(cs, filename); +} + +int libgit_configset_get_int(struct libgit_config_set *cs, const char *key, int *dest) +{ + return git_configset_get_int(cs, key, dest); +} + +int libgit_configset_get_string(struct libgit_config_set *cs, const char *key, char **dest) +{ + return git_configset_get_string(cs, key, dest); +} + const char *libgit_user_agent(void) { return git_user_agent(); diff --git a/contrib/libgit-rs/libgit-sys/public_symbol_export.h b/contrib/libgit-rs/libgit-sys/public_symbol_export.h index 64332f30de..3933698976 100644 --- a/contrib/libgit-rs/libgit-sys/public_symbol_export.h +++ b/contrib/libgit-rs/libgit-sys/public_symbol_export.h @@ -9,6 +9,18 @@ void libgit_init_git(const char **argv); int libgit_parse_maybe_bool(const char *val); +struct libgit_config_set *libgit_configset_alloc(void); + +void libgit_configset_clear_and_free(struct libgit_config_set *cs); + +void libgit_configset_init(struct libgit_config_set *cs); + +int libgit_configset_add_file(struct libgit_config_set *cs, const char *filename); + +int libgit_configset_get_int(struct libgit_config_set *cs, const char *key, int *dest); + +int libgit_configset_get_string(struct libgit_config_set *cs, const char *key, char **dest); + const char *libgit_user_agent(void); const char *libgit_user_agent_sanitized(void); diff --git a/contrib/libgit-rs/libgit-sys/src/lib.rs b/contrib/libgit-rs/libgit-sys/src/lib.rs index 9b2e85dff8..f3eb44a238 100644 --- a/contrib/libgit-rs/libgit-sys/src/lib.rs +++ b/contrib/libgit-rs/libgit-sys/src/lib.rs @@ -1,8 +1,17 @@ -use std::ffi::{c_char, c_int}; +use std::ffi::{c_char, c_int, c_void}; + +#[allow(non_camel_case_types)] +#[repr(C)] +pub struct libgit_config_set { + _data: [u8; 0], + _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>, +} extern crate libz_sys; extern "C" { + pub fn free(ptr: *mut c_void); + pub fn libgit_setup_git_directory() -> *const c_char; // From config.c @@ -17,6 +26,26 @@ extern "C" { // From version.c pub fn libgit_user_agent() -> *const c_char; pub fn libgit_user_agent_sanitized() -> *const c_char; + + pub fn libgit_configset_alloc() -> *mut libgit_config_set; + pub fn libgit_configset_clear_and_free(cs: *mut libgit_config_set); + + pub fn libgit_configset_init(cs: *mut libgit_config_set); + + pub fn libgit_configset_add_file(cs: *mut libgit_config_set, filename: *const c_char) -> c_int; + + pub fn libgit_configset_get_int( + cs: *mut libgit_config_set, + key: *const c_char, + int: *mut c_int, + ) -> c_int; + + pub fn libgit_configset_get_string( + cs: *mut libgit_config_set, + key: *const c_char, + dest: *mut *mut c_char, + ) -> c_int; + } #[cfg(test)] diff --git a/contrib/libgit-rs/src/lib.rs b/contrib/libgit-rs/src/lib.rs new file mode 100644 index 0000000000..43aae09656 --- /dev/null +++ b/contrib/libgit-rs/src/lib.rs @@ -0,0 +1,85 @@ +use std::ffi::{c_char, c_int, c_void, CStr, CString}; +use std::path::Path; + +use libgit_sys::*; + +pub struct ConfigSet(*mut libgit_config_set); +impl ConfigSet { + pub fn new() -> Self { + unsafe { + let ptr = libgit_configset_alloc(); + libgit_configset_init(ptr); + ConfigSet(ptr) + } + } + + pub fn add_files(&mut self, files: &[&Path]) { + for file in files { + let pstr: &str = file.to_str().expect("Invalid UTF-8"); + let rs = CString::new(&*pstr).expect("Couldn't convert to CString"); + unsafe { + libgit_configset_add_file(self.0, rs.as_ptr()); + } + } + } + + pub fn get_int(&mut self, key: &str) -> Option { + let key = CString::new(key).expect("Couldn't convert to CString"); + let mut val: c_int = 0; + unsafe { + if libgit_configset_get_int(self.0, key.as_ptr(), &mut val as *mut c_int) != 0 { + return None; + } + } + + Some(val) + } + + pub fn get_str(&mut self, key: &str) -> Option { + let key = CString::new(key).expect("Couldn't convert to CString"); + let mut val: *mut c_char = std::ptr::null_mut(); + unsafe { + if libgit_configset_get_string(self.0, key.as_ptr(), &mut val as *mut *mut c_char) != 0 + { + return None; + } + let borrowed_str = CStr::from_ptr(val); + let owned_str = CString::from_vec_with_nul(borrowed_str.to_bytes_with_nul().to_vec()); + free(val as *mut c_void); // Free the xstrdup()ed pointer from the C side + Some(owned_str.unwrap()) + } + } +} + +impl Default for ConfigSet { + fn default() -> Self { + Self::new() + } +} + +impl Drop for ConfigSet { + fn drop(&mut self) { + unsafe { + libgit_configset_clear_and_free(self.0); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn load_configs_via_configset() { + let mut cs = ConfigSet::new(); + cs.add_files(&[Path::new("testdata/config1"), + Path::new("testdata/config2"), + Path::new("testdata/config3")]); + // ConfigSet retrieves correct value + assert_eq!(cs.get_int("trace2.eventTarget"), Some(1)); + // ConfigSet respects last config value set + assert_eq!(cs.get_int("trace2.eventNesting"), Some(3)); + // ConfigSet returns None for missing key + assert_eq!(cs.get_str("foo.bar"), None); + } +} diff --git a/contrib/libgit-rs/testdata/config1 b/contrib/libgit-rs/testdata/config1 new file mode 100644 index 0000000000..4e9a9d25d1 --- /dev/null +++ b/contrib/libgit-rs/testdata/config1 @@ -0,0 +1,2 @@ +[trace2] + eventNesting = 1 diff --git a/contrib/libgit-rs/testdata/config2 b/contrib/libgit-rs/testdata/config2 new file mode 100644 index 0000000000..b8d1eca423 --- /dev/null +++ b/contrib/libgit-rs/testdata/config2 @@ -0,0 +1,2 @@ +[trace2] + eventTarget = 1 diff --git a/contrib/libgit-rs/testdata/config3 b/contrib/libgit-rs/testdata/config3 new file mode 100644 index 0000000000..ca7b9a7c38 --- /dev/null +++ b/contrib/libgit-rs/testdata/config3 @@ -0,0 +1,2 @@ +[trace2] + eventNesting = 3 From patchwork Fri Sep 6 22:21:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13794811 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 5919115B118 for ; Fri, 6 Sep 2024 22:21:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725661294; cv=none; b=KuCF+FKy81pB0dVh7BNAvvr+av7Dg9VNbUtZicMwjklIH/nlJwkjQKI4gwZiSQU5xr/GGiIK1CNT1qJ4cwSJNPEgZNr1q+sYSj2pPojWv7bQ2h2GIP99JpddvQeyjxI1fP+AqSQmiY9T7bd1JBJLGuXN8dXXj/I+llrlKICtwso= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725661294; c=relaxed/simple; bh=n1Z4toqLKTe9idXtzwLkCt6XkUgWKAX2rWLA3WZ+WrE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hXTEeCcciTaorOktacWIlZL5JzXGhFrGiwOVw3ormNhJV/E3labnqNdSbI7aIPM6tkPlWcFLLim0RV52bdxGclZM6l1XXofagV9i3dDVe//zWWBOweefVR1mLzcXR82unyd8i/j8jRhA48Pqc5TtEj1qru1oNnEm74GGJprjou8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--calvinwan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DW3kLAhJ; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--calvinwan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DW3kLAhJ" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2d889fc26e7so3072149a91.3 for ; Fri, 06 Sep 2024 15:21:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725661293; x=1726266093; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=XXqBYKy29wLXRTFntiRP0axnFMI9DerB60/VYYTaxhs=; b=DW3kLAhJfo+THlrosHyMHLvi4MBDhc2c1iK9mbAol2gmNl8U3VEeq7hebo307xaVY9 I+/cBVn3XNFlfITfpUxkZ6SaCbBD36WeBdvVzQIgOZ6BotTy177QDXgtoDXJew3qekXk Wrjlpxg/IAG/RKGw8mrC9XMBW62p5g0KDJDkQ6HAwFV3+rWmZ7EaRBU1RMmtHvipV68m 2qYNWaVjY0huxPrrb2kBR+VgEoJBmOxjh4XS2FCmCn46R9043tZGXQ7cfsziPGmwmlQm mhv+rZVQYjoG93Kph8mKbN1HRhVYNTKAsbOpcTkSpMSWS1Nvcd/RAXzSzV/0TzaJsa7D 2c/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725661293; x=1726266093; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XXqBYKy29wLXRTFntiRP0axnFMI9DerB60/VYYTaxhs=; b=d6pfzPbM4EvmD3AKMfXRc1ag3plDuhY0Oyr7sr22raR1rDgPRWFK0qYO+Ytuchbdou 0JKm43zq3KHIDRMilvtl1Uxoj+08pZP+81RWyuYNZt6srqdfcFG8WzT1wG2N2rsdYQk/ MOOEjLsxDcIBo7WDkZTQRDKcMgrK0OQmXtULSVhw7rCj3dmD68zwCxMOOujAmKRqWnXO zRswvEhllkbZxD9PAMp80s/JAlrT6xXCwu1D3Xyu09QJqGlSLf27n0Q6h0KDVTRzhwz7 odcmMJR9+TzwudEbfr88iXq4XZb7/Xi9pj+1Lijy6755uMsLKwZMpjR0XfXnG2GwlRqC 2yLw== X-Gm-Message-State: AOJu0Yyp73BSsv78cEIEaxOGgm+YU6t6jPfwceik4djrIRobIOoNEVDk 46rd7qB0U0uz2U/zGEC4jv1Ilt5VLOhcSfwqDKMWSTb3RgWbgFqrQ5LENDBMpdc1Sx1NgMWuL9a ZL8EI4owumblTVA3j6Hr2zKm7PET6q027wOae/pEHXPfpGuxkCSj7Trme+8/ivay2WMpYsoyQsr DdXaspquUOZSOn71725Wdpwic2+ifKeXisTjKK3agZXzhy X-Google-Smtp-Source: AGHT+IFGIC0Dnd3QsG2AZ/PdOvE8JVacxyYii7r8tTTgHxNAzTbo4Mla/QcVuTsx5On2ALNBuwOjaLH1v5fnSaE= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a17:90a:5e0e:b0:2d8:dd3b:a6de with SMTP id 98e67ed59e1d1-2dad5114956mr8156a91.7.1725661291025; Fri, 06 Sep 2024 15:21:31 -0700 (PDT) Date: Fri, 6 Sep 2024 22:21:16 +0000 In-Reply-To: <20240906221853.257984-1-calvinwan@google.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240906221853.257984-1-calvinwan@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240906222116.270196-6-calvinwan@google.com> Subject: [PATCH v3 6/6] Makefile: add option to build and test libgit-rs and libgit-rs-sys From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan , steadmon@google.com, spectral@google.com, emilyshaffer@google.com, emrass@google.com, rsbecker@nexbridge.com, gitster@pobox.com, mh@glandium.org, sandals@crustytoothpaste.net, Jason@zx2c4.com, dsimic@manjaro.org Add libgitrs, libgitrs-sys, libgitrs-test, and libgitrs-sys-test targets to their respective Makefiles so they can be built and tested without having to run cargo build/test. Add environment variable, INCLUDE_LIBGIT_RS, that when set, automatically builds and tests libgit-rs and libgit-rs-sys when `make all` is ran. Signed-off-by: Calvin Wan --- Makefile | 16 ++++++++++++++++ t/Makefile | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/Makefile b/Makefile index abeee01d9e..41ad458aef 100644 --- a/Makefile +++ b/Makefile @@ -3870,6 +3870,22 @@ build-unit-tests: $(UNIT_TEST_PROGS) unit-tests: $(UNIT_TEST_PROGS) t/helper/test-tool$X $(MAKE) -C t/ unit-tests +.PHONY: libgitrs-sys +libgitrs-sys: + $(QUIET)(\ + cd contrib/libgit-rs/libgit-sys && \ + cargo build \ + ) +.PHONY: libgitrs +libgitrs: + $(QUIET)(\ + cd contrib/libgit-rs && \ + cargo build \ + ) +ifdef INCLUDE_LIBGIT_RS +all:: libgitrs +endif + contrib/libgit-rs/libgit-sys/partial_symbol_export.o: contrib/libgit-rs/libgit-sys/public_symbol_export.o libgit.a reftable/libreftable.a xdiff/lib.a $(LD) -r $^ -o $@ diff --git a/t/Makefile b/t/Makefile index b2eb9f770b..066cb5c2b4 100644 --- a/t/Makefile +++ b/t/Makefile @@ -169,3 +169,19 @@ perf: .PHONY: pre-clean $(T) aggregate-results clean valgrind perf \ check-chainlint clean-chainlint test-chainlint $(UNIT_TESTS) + +.PHONY: libgitrs-sys-test +libgitrs-sys-test: + $(QUIET)(\ + cd ../contrib/libgit-rs/libgit-sys && \ + cargo test \ + ) +.PHONY: libgitrs-test +libgitrs-test: + $(QUIET)(\ + cd ../contrib/libgit-rs && \ + cargo test \ + ) +ifdef INCLUDE_LIBGIT_RS +all:: libgitrs-sys-test libgitrs-test +endif