From patchwork Wed Aug 7 18:21:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13756594 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 A610613D52E for ; Wed, 7 Aug 2024 18:21:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723054899; cv=none; b=Cb5EzFXAl1zPQj2sszLCzg/udNQ8PV/odRqvOVychXwOvFxjOhVeW00WroA0aG1qayVR1HovgI8gZFyOow1xQAkx9MUK9IaOvlZuQO448hdvCcXDq+iQ/T2n0eGjfCAH3GH/AS11FFq1aDYGEQVTCoSgU1Ezf+XJin23ddw1mkU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723054899; c=relaxed/simple; bh=L2NGPccQsJrinTXCD2PyyjIfOhtnoBY6BN16lfCK1io=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pyhgJBgsUh5LOR1jWB7DcQTjU3f4Vq7FgnXMLYGkMHidMvnlLZIn3+6WAnBvjiIdjVZLPBsEtADGrJNVw82YTHHkNmCrUqAWaIRYFt7fZv30MqSVzeMbg1jepTp7OKAEhT/gveVlvIO4FKD31j9FkMjmKx6TJ1F3dP9JwU1gmnE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Lah8rSb4; arc=none smtp.client-ip=209.85.219.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--steadmon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Lah8rSb4" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e0be2fa8f68so155837276.3 for ; Wed, 07 Aug 2024 11:21:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723054896; x=1723659696; 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=s9T0kI73iAVt04U6LFo3WC7rUOTJgxGB+Vr/cOIlEGs=; b=Lah8rSb45QENTx1E3M+Y/DBNSKHS2O0sSAMi0fMvdDjsNUoJdGa0UiuhtdELWdMKP7 kI1R2kk0dz5qjjPHXgGMbXUN0WXa7bzLQlK9P+E05/8uePWostfkVh2g01yIaRIRWpAX rRlbU/FhUVsV/0msY73hxp3afimfoOdMUa+sTjs+bI1KUdrA1FAVTdsnsagoVolQh9qf nffi3BoujvroriCw9h4BlNmF8MBBIth8jd7rfzgzcJASlM77MlNanlY52eKZaem631ql GtGOtgpfHzUS8t0eOpAaGBusZkWjmPz3hvNP4WBYppr4Lk2PheFXxZSkJqL8I6CUoXs/ 6jyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723054896; x=1723659696; 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=s9T0kI73iAVt04U6LFo3WC7rUOTJgxGB+Vr/cOIlEGs=; b=MUHCx+J0Rk1lyAMREx7+1XNlbkCRNqGkpfAMxGfrSs4kHSswX6trCghH7+THL1m7kZ dLasqtILMhNZfEooFsNKl1NdSxMeQkyiQCMICZUNA4iIOMA51TI7go/quH/NhXrAAuu8 Z9X8mKYjkP+sRfbsucm8XMnXasM6rqS83JCzuP2An1l2PJqqY1VBlliwbstIP88wWYPc 1mCQJACig4ejV8YdOdP2QJ9T0ihaRptU2a/7HY9zmQEgkSe4Fkt8WFrPrLNSmKgAV1/P oH9xGZxeeRcihG4FjMo0a9W7d7Smu7bRM/9QiQ0rmqUvhYebTZxHFWXilH+mio8wq3CC bSjw== X-Gm-Message-State: AOJu0YxSY/PnOeanbgGR4EuUiyjZCE4WK/FQXGHPLPMy4r3o4PAHi5cg fj13OUrGYIqdVLZCDWO/yKlye7YrcZFDGRVZcKbG8iqnNqidQP2CTbTzVauqS/zxao5cl0nH5Nl vnJS3wixsneVSJ8adQUmM8kpLjNQKRcAZR5BetR2h8JCQxZK6Oicnp8j17p7hAXey8VSKD0d07b G6Neq2owYZrfKqkvoKvMC6nynJJbQObw4vAre3IQQ= X-Google-Smtp-Source: AGHT+IFyLJiAafgi8YZErSEZ0D/cN/dI8z28Nvngd7frvAdfC37XQpwFLr0OzEe8RHpJ+QoJmoRNgZJPM6RWQA== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:c023:b8:a8df:17c3]) (user=steadmon job=sendgmr) by 2002:a05:6902:2b90:b0:e05:eccb:95dc with SMTP id 3f1490d57ef6-e0bde283222mr161918276.6.1723054896473; Wed, 07 Aug 2024 11:21:36 -0700 (PDT) Date: Wed, 7 Aug 2024 11:21:26 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <78c2aa2ef9351d977f31dbbb16b148baf254ad59.1723054623.git.steadmon@google.com> Subject: [RFC PATCH 1/6] common-main: split common_exit() into a new file From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, spectral@google.com, emilyshaffer@google.com, emrass@google.com, rsbecker@nexbridge.com 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. [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/ Signed-off-by: Josh Steadmon --- Makefile | 1 + common-exit.c | 26 ++++++++++++++++++++++++++ common-main.c | 24 ------------------------ 3 files changed, 27 insertions(+), 24 deletions(-) create mode 100644 common-exit.c diff --git a/Makefile b/Makefile index 3eab701b10..1cac51a4f7 100644 --- a/Makefile +++ b/Makefile @@ -979,6 +979,7 @@ 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 += 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-main.c b/common-main.c index 8e68ac9e42..af4dea049e 100644 --- a/common-main.c +++ b/common-main.c @@ -66,27 +66,3 @@ int main(int argc, const char **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 Wed Aug 7 18:21:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13756595 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 D1E77140E30 for ; Wed, 7 Aug 2024 18:21:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723054901; cv=none; b=EhY3Upj7Y6BxaAtQC1kQBMmNjePzPkqLmpr4FLQGuP6nbXjtTQuGHM8zT6m9QdGV9Q0HsJR33O25yRwDwhJvRHD/461hcywmXM2lnnk8rP9QqoJeM0YTYudk6vskTiVhLy4KtILJoAiJwL7QfTvvqlpQb5N//pfl2eYL+8LYjN8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723054901; c=relaxed/simple; bh=M7IxCcr23SNXCkMkwP25Vfz59X5Ym2BQSFUlzb+uTP0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=I3M3iUoiFqNgkmj1ievfNZPaWHJooJP6y1Y5JJvrm5McQcKWOx1ehoKFFQZlXoOWKgnNqA6e8YbqBjtAonG3+JvXL0hoVqEZsnJsV3HEzkuglFTU5Y0oHJXQgY8loLZmyd8e/Zd1lE5HReeEsLNbRMJoMRaPAA2P2AdhMCAQAh8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Zb2jNegs; arc=none smtp.client-ip=209.85.128.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--steadmon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Zb2jNegs" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6698f11853aso2787247b3.0 for ; Wed, 07 Aug 2024 11:21:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723054899; x=1723659699; 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=kQx7JATapEszW9af8iFYmXhJUq7ciAq8MVv7TpxDIqs=; b=Zb2jNegswo6Kh+YdXTQ8Vq+D5nTtXifnSpqCO1UMwPDOvp3J/VPxbkTt0q2/q3XbQF 3vOs6fJ/U295TJMLXSvfsNSCpDZQAtiWUazg/Dz3dUSsPcs703qkMNaX5qaKJYhariEI oBFMMuyIWu20mycuGM7eD43Ibv070ig1RFC3uWetVSNT9eMOod/8sRc2hnLQWD3wqSUR oT2UJJIM4BkPBRcQswRby2C4mW0Wdk5JVlfyYoeTuAVjpyOUjfYg5gnOvoMdWjLGTRDg v+4DidcN2pUuso2H19monlIxd1KTZcHyDzQs9wQOfzF5b5sXryWz1ULPixj6pgN6kvri onjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723054899; x=1723659699; 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=kQx7JATapEszW9af8iFYmXhJUq7ciAq8MVv7TpxDIqs=; b=eHn6H3uiyWyZM6wCnNNGW4WzeKIivK6Y+cJIPZ9IqB37yZk4TxRpHo64orV69XMOdf IBrXK9rKtHxvsGXRk1jZQdeQZmqxma21QvCmLQiugUwOsbq+mXoyoRn1z0GonlV/a8J7 9bBCLvRkXqgA0iwcx94hYglHZZPxhAdJpZd3b8W8bLrs+eT1tVCvoOmBRlsG8qAjyZ6L rppliIZHdQmcvciuOQFtJeO8f8Eh5lXedCchIQgVhDBayX1qXPyJDfws9JQq7UxMlgqc mubRbOtwEhGAeWOmypoK5GBN6TSBJqXU5nbPWchWAXDmw6SGDWxXzcJMzdooHTj55YoH zMmQ== X-Gm-Message-State: AOJu0Yyrp3HasxuKtBGQr+l0bNGeAq4A0lX0QCccl047kusRs2qOtKfz llfrEQDg2flr6nkuWpuzOP0B0/4PkuAM01132nHcTk0VPYU30/1xbwXFYqvoKSmwfx8hUCuzq9f xElsXcQvZ55cKmfj7bdKpKA96yGmhTyYqeJbJcfjoCy9UpnKi586h0wBDiTcFO8I4F1b3MjWaMY 2zZ1bTVkzIvRXyccwpk4VJOCJ4sFu/c1WFcj4t4s8= X-Google-Smtp-Source: AGHT+IHzHPf1a7iIPiDVNJcGawupvpITxoXNsVnHQ+qh3u0CTkjD8NX5RTpTPaDsY7nv74MFeygaokrwyH15IQ== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:c023:b8:a8df:17c3]) (user=steadmon job=sendgmr) by 2002:a05:6902:c01:b0:e05:6e45:84b6 with SMTP id 3f1490d57ef6-e0bde3eedeemr35208276.8.1723054898497; Wed, 07 Aug 2024 11:21:38 -0700 (PDT) Date: Wed, 7 Aug 2024 11:21:27 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <5f2e816cf6359725f2a86ce1d08e5e272fba4dac.1723054623.git.steadmon@google.com> Subject: [RFC PATCH 2/6] repository: add initialize_repo wrapper without pointer From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, spectral@google.com, emilyshaffer@google.com, emrass@google.com, rsbecker@nexbridge.com Non-C external consumers of libgit.a have to redefine the `repository` object in their own language if they want to call initialize_repository() to ensure memory for the object is allocated correctly. This is not ideal for external consumers that have no need for the entire `the_repository` object but need to call other functions from an initialized repository. Therefore, add a friendly initialize_repository() wrapper without a `the_repository` pointer. Co-authored-by: Calvin Wan Signed-off-by: Calvin Wan --- repository.c | 9 +++++++++ repository.h | 1 + 2 files changed, 10 insertions(+) diff --git a/repository.c b/repository.c index 9825a30899..15cb9ee735 100644 --- a/repository.c +++ b/repository.c @@ -81,6 +81,15 @@ void initialize_repository(struct repository *repo) set_default_hash_algo(repo); } +/* + * For non-C external consumers of libgit.a that do not need access + * to the entire `the_repository` object. + */ +void initialize_the_repository(void) +{ + initialize_repository(the_repository); +} + static void expand_base_dir(char **out, const char *in, const char *base_dir, const char *def_in) { diff --git a/repository.h b/repository.h index af6ea0a62c..65c9158866 100644 --- a/repository.h +++ b/repository.h @@ -227,6 +227,7 @@ void repo_set_compat_hash_algo(struct repository *repo, int compat_algo); void repo_set_ref_storage_format(struct repository *repo, enum ref_storage_format format); void initialize_repository(struct repository *repo); +void initialize_the_repository(void); RESULT_MUST_BE_USED int repo_init(struct repository *r, const char *gitdir, const char *worktree); From patchwork Wed Aug 7 18:21:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13756596 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 87F3B13D52E for ; Wed, 7 Aug 2024 18:21:41 +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=1723054903; cv=none; b=bylGrl/0Xftj4wEdxVe3zPdUSdFlzB64gPsY13aAeTvjtVu+W1GA9h8sF8EadzECZvRsWfkiN8mYxVTQJEMWTxBMm8xbB5L3Umkk2RCReDBpaEZX7LrWTBBkGywXlpuk8+ca7KVIsYmcYhuvjz3JIWOcCozA2dTAP2Ed5JojfPE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723054903; c=relaxed/simple; bh=Y17aJ8/R4RPf1LavwtDwcdDRFr3vPzUlnJ3i2ltEGFo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rfi6fJH4oe4qXaVEsdx6qeSEXqhNhotEl2L3u6twYQ0suRYj8mxG+XCaeInbnn1qfOH3LObwdesNtZsJ7Wd/gYfJpMHTWzCsTMmP/1LB8kV4ZgEPXknY0ltXY4uUNTE/SdtUZncC+ztBv0OwrzhQpA2n50/af/RrwxjaxXis5W0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=WR4qwM/T; 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--steadmon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WR4qwM/T" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e02b5792baaso162902276.2 for ; Wed, 07 Aug 2024 11:21:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723054900; x=1723659700; 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=3+BLo8+jhLu6o6opISMFG79Syv4fDQp+Eph2WfIbqEQ=; b=WR4qwM/TZlEtMDeydQQ2zlu8JEsbc5pJf0UncPu2VF2Kh4v50pnYayq8Pph6x1DZQ5 M7sqlE+L3FQmQjte7/aUYz3FS0uMGF8F8G7h8fNEuaI+AQcuwokyRxPDtxXEKByyIaYI 7kT2CmhKMMxKa0adJ63k9dAvjRKyFxO9+gfeN4RU+RbBF2if1lfZhubjtwnTUUUl01nd sP/vz+QKVFpNhZG0MhAzdYVSofyTqitKBvWA+a21OmP4ZEcmSgGhcAGiYUnlDpU40h0d Oo31g01iik2CWXf+KDf1ZUibijd3jaP7J2Ab24Dkfjx+ortfybKlgjh0R+moGdtS9ymy hKXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723054900; x=1723659700; 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=3+BLo8+jhLu6o6opISMFG79Syv4fDQp+Eph2WfIbqEQ=; b=cp7l8izApW0uNj29FmnfvV6I/pbs4qd66JjfRyLpqieRx20ndaLX4QTNHdfP1k+L4i coIki/u8x/wWmrYnZHjThJomYZZp1drVM58WLLbvKIaIjzP0x+7adA/ZcpC8UnKaTJbM Ev/wtR2YCmKJYR+hpvan6IPdhdPZAkF5u8u6lToIdygSPV3NJ44UrIez2AJ4tzojfQW7 uvdf8FRoEqjK96MUOUPoaY33HyF+BPcJQQOEyetohk1yAyjRxCD5lQlqmsh0IdUc+Hzd ByiIxKdY1bvCq8X4dGgGXPptTQh1v0lz9cEDLRQUlMnRBU+y/rd8aZ5jIcTblAiRErok BCKA== X-Gm-Message-State: AOJu0YwT06ILuYTb4HFkRQfGfSE3QeWWwX6RnpWzHbb2I5DINw0R3g+f nkEbynsDa0uMsSIE/QqaP89+qgfPHvaUJd04FgXa1T2ZpQp7rIgZAnkLuiQbDa3w+pdYSJBd0zf CPbxCyDWBZZvYiEHdDNVBVF+MCnmAtrQ/9mRlnUlJhrRforQfI/n3XWQ7ZIeoOIr2JvCPQWg3Il g9TDSsClkxyYcw4aA7ZUCyMh8MzYePzKJ7B+SI3xE= X-Google-Smtp-Source: AGHT+IHk7o2BG194lfcsF4KYdNwJUYDSGK6OYLthSwLsYH0wK85WT5UPFvMz16KUkVUKdSxvhKwpxQ+9U+w5Og== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:c023:b8:a8df:17c3]) (user=steadmon job=sendgmr) by 2002:a05:6902:c0e:b0:e03:2257:98b8 with SMTP id 3f1490d57ef6-e0bde1eacfcmr259177276.1.1723054900376; Wed, 07 Aug 2024 11:21:40 -0700 (PDT) Date: Wed, 7 Aug 2024 11:21:28 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <9a846c17c891e17566a9907b3627210a6a08ea76.1723054623.git.steadmon@google.com> Subject: [RFC PATCH 3/6] contrib/cgit-rs: introduce Rust wrapper for libgit.a From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, spectral@google.com, emilyshaffer@google.com, emrass@google.com, rsbecker@nexbridge.com Introduce cgit-rs, 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. A proof-of-concept library consumer is provided in contrib/cgit-rs/src/main.rs. This executable can be run with `cargo run` Symbols in cgit 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/cgit-rs/libcgit.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 Co-authored-by: Josh Steadmon Signed-off-by: Calvin Wan Signed-off-by: Kyle Lippincott Signed-off-by: Josh Steadmon --- .gitignore | 1 + Makefile | 13 ++++++++++ contrib/cgit-rs/Cargo.lock | 16 +++++++++++++ contrib/cgit-rs/Cargo.toml | 16 +++++++++++++ contrib/cgit-rs/README.md | 15 ++++++++++++ contrib/cgit-rs/build.rs | 33 ++++++++++++++++++++++++++ contrib/cgit-rs/public_symbol_export.c | 20 ++++++++++++++++ contrib/cgit-rs/public_symbol_export.h | 8 +++++++ contrib/cgit-rs/src/lib.rs | 7 ++++++ contrib/cgit-rs/src/main.rs | 10 ++++++++ 10 files changed, 139 insertions(+) create mode 100644 contrib/cgit-rs/Cargo.lock create mode 100644 contrib/cgit-rs/Cargo.toml create mode 100644 contrib/cgit-rs/README.md create mode 100644 contrib/cgit-rs/build.rs create mode 100644 contrib/cgit-rs/public_symbol_export.c create mode 100644 contrib/cgit-rs/public_symbol_export.h create mode 100644 contrib/cgit-rs/src/lib.rs create mode 100644 contrib/cgit-rs/src/main.rs diff --git a/.gitignore b/.gitignore index 8caf3700c2..7031012330 100644 --- a/.gitignore +++ b/.gitignore @@ -248,3 +248,4 @@ Release/ /git.VC.db *.dSYM /contrib/buildsystems/out +/contrib/cgit-rs/target diff --git a/Makefile b/Makefile index 1cac51a4f7..fcd06af123 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 @@ -2712,6 +2714,7 @@ OBJECTS += $(XDIFF_OBJS) OBJECTS += $(FUZZ_OBJS) OBJECTS += $(REFTABLE_OBJS) $(REFTABLE_TEST_OBJS) OBJECTS += $(UNIT_TEST_OBJS) +OBJECTS += contrib/cgit-rs/public_symbol_export.o ifndef NO_CURL OBJECTS += http.o http-walker.o remote-curl.o @@ -3719,6 +3722,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/cgit-rs/target ifndef NO_PERL $(RM) -r perl/build/ endif @@ -3864,3 +3868,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/cgit-rs/partial_symbol_export.o: contrib/cgit-rs/public_symbol_export.o libgit.a reftable/libreftable.a xdiff/lib.a + $(LD) -r $^ -o $@ + +contrib/cgit-rs/hidden_symbol_export.o: contrib/cgit-rs/partial_symbol_export.o + $(OBJCOPY) --localize-hidden $^ $@ + +contrib/cgit-rs/libcgit.a: contrib/cgit-rs/hidden_symbol_export.o + $(AR) $(ARFLAGS) $@ $^ diff --git a/contrib/cgit-rs/Cargo.lock b/contrib/cgit-rs/Cargo.lock new file mode 100644 index 0000000000..f50c593995 --- /dev/null +++ b/contrib/cgit-rs/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cgit" +version = "0.1.0" +dependencies = [ + "libc", +] + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" diff --git a/contrib/cgit-rs/Cargo.toml b/contrib/cgit-rs/Cargo.toml new file mode 100644 index 0000000000..7b55e6f3e1 --- /dev/null +++ b/contrib/cgit-rs/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "cgit" +version = "0.1.0" +edition = "2021" +build = "build.rs" +links = "git" + +[[bin]] +name = "cgit-test" +path = "src/main.rs" + +[lib] +path = "src/lib.rs" + +[dependencies] +libc = "0.2.155" diff --git a/contrib/cgit-rs/README.md b/contrib/cgit-rs/README.md new file mode 100644 index 0000000000..7a59602c30 --- /dev/null +++ b/contrib/cgit-rs/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/cgit-rs/build.rs b/contrib/cgit-rs/build.rs new file mode 100644 index 0000000000..0c1ec06737 --- /dev/null +++ b/contrib/cgit-rs/build.rs @@ -0,0 +1,33 @@ +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(&[ + "CC=clang", + "CFLAGS=-fvisibility=hidden", + "contrib/cgit-rs/libcgit.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("libcgit.a"), dst.join("libcgit.a"))?; + println!("cargo::rustc-link-search=native={}", dst.into_os_string().into_string().unwrap()); + println!("cargo::rustc-link-lib=cgit"); + println!("cargo::rustc-link-lib=z"); + println!("cargo::rerun-if-changed={}", git_root.into_os_string().into_string().unwrap()); + + Ok(()) +} diff --git a/contrib/cgit-rs/public_symbol_export.c b/contrib/cgit-rs/public_symbol_export.c new file mode 100644 index 0000000000..3d1cd6cc4f --- /dev/null +++ b/contrib/cgit-rs/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/cgit-rs/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/cgit-rs/public_symbol_export.h b/contrib/cgit-rs/public_symbol_export.h new file mode 100644 index 0000000000..a3372f93fa --- /dev/null +++ b/contrib/cgit-rs/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/cgit-rs/src/lib.rs b/contrib/cgit-rs/src/lib.rs new file mode 100644 index 0000000000..dc46e7ff42 --- /dev/null +++ b/contrib/cgit-rs/src/lib.rs @@ -0,0 +1,7 @@ +use libc::c_char; + +extern "C" { + // From version.c + pub fn libgit_user_agent() -> *const c_char; + pub fn libgit_user_agent_sanitized() -> *const c_char; +} diff --git a/contrib/cgit-rs/src/main.rs b/contrib/cgit-rs/src/main.rs new file mode 100644 index 0000000000..1794e3f43e --- /dev/null +++ b/contrib/cgit-rs/src/main.rs @@ -0,0 +1,10 @@ +use std::ffi::CStr; + +fn main() { + println!("Let's print some strings provided by Git"); + let c_buf = unsafe { cgit::libgit_user_agent() }; + let c_str = unsafe { CStr::from_ptr(c_buf) }; + println!("git_user_agent() = {:?}", c_str); + println!("git_user_agent_sanitized() = {:?}", + unsafe { CStr::from_ptr(cgit::libgit_user_agent_sanitized()) }); +} From patchwork Wed Aug 7 18:21:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13756597 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 E274A1422DD for ; Wed, 7 Aug 2024 18:21:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723054905; cv=none; b=PulKViEZxqiOU+s3/WECZ0ohGEBl6ng2bGEr1aG8XzWHkU0ht8aUBMzpvGb7s/l3jLk3DSIOYsWb9EuI6Pawz2N0jPxcAL0Ak3k0uZB7jEgBCjhMNyt+wQ48uomyyZhoKuAy6JueFWGQMHoW2CgF4FhLzU/EWMkWmeP4/6Ez7OM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723054905; c=relaxed/simple; bh=WuJUOZ+NIkaWJQOqSKaDaQ/zwm1Q8BFdBjlhth6OAyA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hV7Io1GpG72D5TeMUzIAmVnCgRsYYnMagmVb694MX3h3cAztCMboq7uHRkwuxMYnfltHS0+ZPqAA5o+N6X0Yah743eoAxHGNYBsDXi8B2Dyzkxea0kdbQPv4ROTb0fyrSnb2+cSiWoX9VR/rE67zFcBwwDBZ2taJrrdrgnoq3Fw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cZ7Yi336; arc=none smtp.client-ip=209.85.128.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--steadmon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cZ7Yi336" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-66628e9ec89so2641947b3.1 for ; Wed, 07 Aug 2024 11:21:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723054903; x=1723659703; 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=rk1iovIPdivophnoqxUPwNRc4SYTsh44XZockca6a/w=; b=cZ7Yi336rvoMYh1QlHPTuexGX36EN0Jctm55df1/2rgx6459B+8zRiKTeHYr4pwf3d NOE+A5P26waJnQ5xKlgzAUUwyyzDWNWZUgc/EOy3GQXml9JrrX+fs4ZGy4qmM0zMSmHW VthAvX+G53C99ovdx3jqLKyvqUziYjgvhZ7aQuIhYwAjC1uQjpc4TJVKK/FXyXiqe+Rr Hk1V3rXTNkcvsxrjcpZ6OLGbrfi9NN6EjF5xc50YjGqfwGnPvAx15c3JFQCzbWq0y6Jp +6/ScXitcep5E2RCnR/L1vLkKOpTbdkDLBKinb4QAr5sD8DrQ+pHqNU1Ov4OyqDNLjEY 37iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723054903; x=1723659703; 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=rk1iovIPdivophnoqxUPwNRc4SYTsh44XZockca6a/w=; b=q/mQjpuNfnCony2+YA26agHev3cn0UUAqrQYuCmTOWTjbyQNzxlTbzppJWrQveib0R PE3xRscAW8LZtvLov3dD6X5HtocZt7/RQtBjbzZSYQ8jbOoRH+6hJv6k12N9T3r9uJze H49STij7FG4/Qcydol0j5xot6pBcsMVfBnf5C/Oto0H5FeGTaaCoAUVTov7MSAFRnq1H 2K3gZJUF48dB+Hsz1exw4A1UhLxKw+Z2IfTQ/TMA80bWmzUSsKq0dteHcRyTWUQlOe1C zNwLfL7A4BNv2N2cIr8KuF8n3VNJlgn/fl+hFpSj2u5KwvuK5D1SfNenmJlxyYYEgXaP vz1Q== X-Gm-Message-State: AOJu0Yzo1RMW6Lo7mIJeM3XDBjCtrGrH3GCsri7p18ZRNQ5Zmy9L+3qF UNpYyr1UjhqQ/JxSDRJUcJkXs2TdBqR7ilB2mT2Hx8kUrOQ9a+HXgRxghG46dAjJHgCa9nc3ONS qVpkIRdCv4VdYwW/BUNYAyKTFcpNXxoHBuUWMEhkRZEL3txKk4z+tuapyfZl2oFsKzjrZFkDp72 ixk43pHclpLJy8JdDMh5KPTJBaLhaLBhD2oQXoxAI= X-Google-Smtp-Source: AGHT+IEsbTVJFB0r55ZwkDhdLx8oV9o/DGx6Kiw8+3R/BngdNQUluCZhx+hfBv2QgekvbnJqAFbAZCiq3BfLOA== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:c023:b8:a8df:17c3]) (user=steadmon job=sendgmr) by 2002:a25:8601:0:b0:e05:f1ad:a139 with SMTP id 3f1490d57ef6-e0bde435653mr29377276.11.1723054902377; Wed, 07 Aug 2024 11:21:42 -0700 (PDT) Date: Wed, 7 Aug 2024 11:21:29 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: Subject: [RFC PATCH 4/6] contrib/cgit-rs: add repo initialization and config access From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, spectral@google.com, emilyshaffer@google.com, emrass@google.com, rsbecker@nexbridge.com From: Calvin Wan Co-authored-by: Josh Steadmon Signed-off-by: Calvin Wan Signed-off-by: Josh Steadmon --- contrib/cgit-rs/public_symbol_export.c | 27 ++++++++++++++++++++++++++ contrib/cgit-rs/public_symbol_export.h | 8 ++++++++ contrib/cgit-rs/src/lib.rs | 13 ++++++++++++- contrib/cgit-rs/src/main.rs | 23 +++++++++++++++++++++- 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/contrib/cgit-rs/public_symbol_export.c b/contrib/cgit-rs/public_symbol_export.c index 3d1cd6cc4f..ab3401ac40 100644 --- a/contrib/cgit-rs/public_symbol_export.c +++ b/contrib/cgit-rs/public_symbol_export.c @@ -2,11 +2,35 @@ // 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/cgit-rs/public_symbol_export.h" +#include "attr.h" +#include "config.h" +#include "setup.h" #include "version.h" #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 git_config_get_int(key, dest); +} + +void libgit_initialize_the_repository(void) +{ + initialize_the_repository(); +} + +int libgit_parse_maybe_bool(const char *val) +{ + return git_parse_maybe_bool(val); +} + const char *libgit_user_agent(void) { return git_user_agent(); @@ -17,4 +41,7 @@ const char *libgit_user_agent_sanitized(void) return git_user_agent_sanitized(); } +const char *libgit_attr__true = git_attr__true; +const char *libgit_attr__false = git_attr__false; + #pragma GCC visibility pop diff --git a/contrib/cgit-rs/public_symbol_export.h b/contrib/cgit-rs/public_symbol_export.h index a3372f93fa..97e8e871ba 100644 --- a/contrib/cgit-rs/public_symbol_export.h +++ b/contrib/cgit-rs/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_initialize_the_repository(void); + +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/cgit-rs/src/lib.rs b/contrib/cgit-rs/src/lib.rs index dc46e7ff42..df350e758f 100644 --- a/contrib/cgit-rs/src/lib.rs +++ b/contrib/cgit-rs/src/lib.rs @@ -1,6 +1,17 @@ -use libc::c_char; +use libc::{c_char, c_int}; 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 repository.c + pub fn libgit_initialize_the_repository(); + + // 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; diff --git a/contrib/cgit-rs/src/main.rs b/contrib/cgit-rs/src/main.rs index 1794e3f43e..c5f8644fca 100644 --- a/contrib/cgit-rs/src/main.rs +++ b/contrib/cgit-rs/src/main.rs @@ -1,4 +1,4 @@ -use std::ffi::CStr; +use std::ffi::{CStr, CString}; fn main() { println!("Let's print some strings provided by Git"); @@ -7,4 +7,25 @@ fn main() { println!("git_user_agent() = {:?}", c_str); println!("git_user_agent_sanitized() = {:?}", unsafe { CStr::from_ptr(cgit::libgit_user_agent_sanitized()) }); + + println!("\nNow try passing args"); + let test_arg = CString::new("test_arg").unwrap(); + println!("git_parse_maybe_bool(...) = {:?}", + unsafe { cgit::libgit_parse_maybe_bool(test_arg.as_ptr()) }); + + println!("\nCan we get an int out of our config??"); + unsafe { + cgit::libgit_initialize_the_repository(); + cgit::libgit_setup_git_directory(); + let mut val: libc::c_int = 0; + let key = CString::new("trace2.eventNesting").unwrap(); + cgit::libgit_config_get_int( + key.as_ptr(), + &mut val as *mut i32 + ); + println!( + "git_config_get_int(\"trace2.eventNesting\") -> {:?}", + val + ); + }; } From patchwork Wed Aug 7 18:21:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13756598 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 E73D8142915 for ; Wed, 7 Aug 2024 18:21:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723054908; cv=none; b=fYti2JOfdQJQKZwdZUw2fiRWmHYr99qNOC0MCvRwAjMYQHUVI05bnY/ShSM27yF/nudt2VEULhYnr9mP/LFjc7Q7m8ctI1t51HQIXpWtJBOujbGXkNqx1uMdxuBWHmlhWlSlXIYp5CKAEQKZBn166VC4fUXVWFxzBKDUIDaAX4U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723054908; c=relaxed/simple; bh=sY/a2lB+pTHNyT+ewo0Gd08rVrB/lccEpzDLhyNBoOE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DIy1G7Nzqgb/iN+FxvdfHVCDFvxWA0rdHCBMA0wO3Rql3VExC5Qlw49OIMXDHbsdYe8tpTgR3b/VB5mkDFeJeye5/v63s7heVUwmtNKRb7d18ciZQSR+CAiPAHNy9khTeFxH7GWQwF42BYTKpMdgIioh/I2Rmojx/L9zGFWiUJo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Zxky6WxV; arc=none smtp.client-ip=209.85.128.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--steadmon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Zxky6WxV" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-66619cb2d3eso2953387b3.2 for ; Wed, 07 Aug 2024 11:21:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723054905; x=1723659705; 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=7hKnSfTd9nkbmSeMIrIDIRJtybACw09d26hyfkET1DI=; b=Zxky6WxVcm9Azl08Te1iLpqCW6G/R7HdUqlz/NeISn9C78wKdJbmcFYbRRhnbWIE/T gw6NcoVkUOIHdx/17s1SqmS0lSybP59LprZE0ju5yIJeHzuuj7XdcVLysqmeILaKKXud bzeJvFVU2e4RqOF3AuVFG0aGMPEH4EBwVjdln+ZwaoKWxvTzvdE3Uz/zY8JsvkjH2zrv PDqEe58W6EnmEaEwmHYB7RDddGsSrmcQpCOiJIl8dDYYJN54+TfQ0XnY8cNdLOvNHrhk p3YKJAY1crjAGKl7BbywvyX1vpPzs3nMRn8nUHNRzTletT7+xZlfApE3L+ds3H2oU8jK gJww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723054905; x=1723659705; 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=7hKnSfTd9nkbmSeMIrIDIRJtybACw09d26hyfkET1DI=; b=bpVnJM+1KJju+DysSkqSXcP3ig8ampa7SNUDYgUro9jJCCzpsxVWGQ9fSd89uRSRLE H6pqXUoUmZ+sjgRShN0UkaSytimsFhM7vmfTOeEVurokzx7uIM5hvsVgaa1t4DqAN5zP er0mZLEGnN1QqH6NdLukmhxm3sTMRJxGnYk0tmiR53EnDaD+P5KKR3hgt8ok8yEehrox 0mrBZZ6p4BxxfLskqzy5eftbDoKMoPmLKcTUNGm8OavGbqYk44ZoofEm6ClDoAD4S8Vo koVGWpoJmqQpSDO3qW6M47O0SqmumwrF59/4pPy2rvUwFI2j+SFhSZ8QFzEvcJhVJkxX PeWg== X-Gm-Message-State: AOJu0YxYFKcAAnFwfgma4sVBWlIZ84CMjzQKDVAbMy/i33kS4312yVHJ qtUn4fT7KXi1beHRYncIJ2ATMgwAMeXcvzjBL6lf9W7EX/YMkrFUDp6gfh5H2XdpVTYL8dPc+Ki UTjB6DiYzUyu4urFDE5z5loU8VXiSB8NMJPAGlj7LGohUXywoXWY3mGAWNUOmqNeZ0X6Tsx4UaL b+Ml0Y8LtivyQMDQEKB79bKfXkJVD2feiElM1dnAk= X-Google-Smtp-Source: AGHT+IEdWtQzock56n6ERxexdAw1Ifhy/OkYRNjr7DudFEsP8SpcktPpszna47/4hCxeBwAkV4vyhHDqsspdLA== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:c023:b8:a8df:17c3]) (user=steadmon job=sendgmr) by 2002:a05:6902:2b02:b0:e03:5b06:6db2 with SMTP id 3f1490d57ef6-e0bde2efa5emr34260276.3.1723054904363; Wed, 07 Aug 2024 11:21:44 -0700 (PDT) Date: Wed, 7 Aug 2024 11:21:30 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: Subject: [RFC PATCH 5/6] config: add git_configset_alloc From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, spectral@google.com, emilyshaffer@google.com, emrass@google.com, rsbecker@nexbridge.com Add git_configset_alloc so that non-C external consumers can use configset functions without redefining config_set. Co-authored-by: Calvin Wan Signed-off-by: Calvin Wan Signed-off-by: Josh Steadmon --- config.c | 5 +++++ config.h | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/config.c b/config.c index 6421894614..fcfe7fcf46 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); diff --git a/config.h b/config.h index 54b47dec9e..abb4415b80 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`. */ From patchwork Wed Aug 7 18:21:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13756599 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 5A3E613F426 for ; Wed, 7 Aug 2024 18:21:47 +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=1723054909; cv=none; b=YO0xNfiVe6aU4GgkSAoNj4AKrcX+jN+PF/KEiuEJ67HyJ0oA3/LKJ4FJxVg1kDOQJ4f3t5rO5i55b0lhyCqVF/GbxrH4ANAIzVrr9jWKpb4kJWAm5eMLQP6n9swosZBn+PxUGI9OT7Jzu4VG44vHG68nxRlU73jTXsb5ffuDlzs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723054909; c=relaxed/simple; bh=oSxAUUkXMkYhSI+noWqO7LdnyP7CFWgPfuCPKOF2ZEU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cHLsjpCvIUfFlAS1dJBfURJ/WUcSZM7vbw18yRY76Zq7kIWy1550BU/ctNxgP4rHGv2Gpy+VVtl2lML6AFqUX90MgeX/WFdFBUu6VysyIZXCSeY2mCHGHJ82Rvy0CO50LsoHiIjunAlGsO/zokOPipeA6FIWnJGRUsL6XVCVSoI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ZDUFyxKY; 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--steadmon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZDUFyxKY" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-690d5456d8aso2936577b3.2 for ; Wed, 07 Aug 2024 11:21:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723054906; x=1723659706; 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=s4NJbGmeNmuO3sNGFjFwxaYltFnOYBNux8hbi98MDCY=; b=ZDUFyxKYNupGhPHV7s1NwL53AsquoRU5Vt53zuPU8z1KBvsZKdzuHPBS8ev3IgWabE /k0JHTq8TWwGWDKZiaU3wmGpvHCGUX4hSuAMqfpGzCf5XMjahpXG/KwzyO9x4MEpaiL0 /woqhAUJZxR4eNeQRMrcM0E1todB35S2fgXNp9P1iMhx4n1gLkHU+2r36X6dAojNTTDc YwmhzfbEjQRRObOIkGlwF/IZ1VIFIUpLq1S4zScQcIrc6qU1WB6wiOIoRlcJ27GRaXRB y/+2IiAnsANAHU4hd/AUobfTHzwuKe/BW9DzWL+7i22XMJRj4r8k/rPxe/uAfGEDmi3X F3gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723054906; x=1723659706; 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=s4NJbGmeNmuO3sNGFjFwxaYltFnOYBNux8hbi98MDCY=; b=dq7F2510CYNYoxCQ0no0ybbJJz39srmd0u2MlAeyBFNfOAT7iySK1IoOXALnatsFvo cB/OJWvwXrlTAEeU/i4YB0ZsQUQat33FuSOcXEzz8HJqbAijxXoux97OfC1FPDswI164 q18OatomYDY+JWhvAVkU5HonMyPNQKx4FHd/tEY2iniwGvy/YmdRROE1FkXygKscRil7 5ecIjFXZfjenJ280Y5YLbMINX0nLa1OBZfN/TB5Mdg4ws8OXOSkVpZK5AVufYpRwrfoe HsI7I2t2dBw0oUwA+p9+rwV2MzVckFVeWhmB9/ziwaD0hBSrmjJqj6S1hen0zvI3Y4bU DUzg== X-Gm-Message-State: AOJu0YwqfOJ/Qh4BVKYnBKpiAT/b6fz57b23TTPlK9/5MmMjeTa7tBqF DJRtFc1HDenfae0Qw/46znGKX0Ckp5lYmod4HU/668Ocw9jFi+pBKNT+M+f1AC7yMB09r1QaCrd o0+oEVIj79Y+2vyPd16q9LlE7HUG7nV4f05QV6WWUQlgEuRR/fuVk9wnzW59hdXKWcDuvSd0GFd KZlPhh3PlfnblSW2BfiVjB08R9FuCkaHhYdSeGex8= X-Google-Smtp-Source: AGHT+IGAdopuhY9EEtsbCBp407T1amYiVUCYs+EOx7nzVPKITdwmaiObnKTbj/q8NzBUyA4zPyJgPiWKfWsFVA== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:c023:b8:a8df:17c3]) (user=steadmon job=sendgmr) by 2002:a05:6902:2b84:b0:e0b:bafe:a7ff with SMTP id 3f1490d57ef6-e0bde264493mr37549276.6.1723054906236; Wed, 07 Aug 2024 11:21:46 -0700 (PDT) Date: Wed, 7 Aug 2024 11:21:31 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <1e981a68802ac5aa7538381eb9469e524265ee40.1723054623.git.steadmon@google.com> Subject: [RFC PATCH 6/6] contrib/cgit-rs: add a subset of configset wrappers From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, spectral@google.com, emilyshaffer@google.com, emrass@google.com, rsbecker@nexbridge.com From: Calvin Wan Signed-off-by: Calvin Wan Signed-off-by: Josh Steadmon --- contrib/cgit-rs/Cargo.lock | 83 ++++++++++++++++++++++++++ contrib/cgit-rs/Cargo.toml | 1 + contrib/cgit-rs/public_symbol_export.c | 25 ++++++++ contrib/cgit-rs/public_symbol_export.h | 10 ++++ contrib/cgit-rs/src/lib.rs | 65 +++++++++++++++++++- contrib/cgit-rs/src/main.rs | 13 ++++ 6 files changed, 196 insertions(+), 1 deletion(-) diff --git a/contrib/cgit-rs/Cargo.lock b/contrib/cgit-rs/Cargo.lock index f50c593995..1d40ac5faa 100644 --- a/contrib/cgit-rs/Cargo.lock +++ b/contrib/cgit-rs/Cargo.lock @@ -6,11 +6,94 @@ version = 3 name = "cgit" version = "0.1.0" dependencies = [ + "home", "libc", ] +[[package]] +name = "home" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" +dependencies = [ + "windows-sys", +] + [[package]] name = "libc" version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/contrib/cgit-rs/Cargo.toml b/contrib/cgit-rs/Cargo.toml index 7b55e6f3e1..5768fce9e5 100644 --- a/contrib/cgit-rs/Cargo.toml +++ b/contrib/cgit-rs/Cargo.toml @@ -14,3 +14,4 @@ path = "src/lib.rs" [dependencies] libc = "0.2.155" +home = "0.5.9" diff --git a/contrib/cgit-rs/public_symbol_export.c b/contrib/cgit-rs/public_symbol_export.c index ab3401ac40..9641afca89 100644 --- a/contrib/cgit-rs/public_symbol_export.c +++ b/contrib/cgit-rs/public_symbol_export.c @@ -31,6 +31,31 @@ int libgit_parse_maybe_bool(const char *val) return git_parse_maybe_bool(val); } +struct config_set *libgit_configset_alloc(void) +{ + return git_configset_alloc(); +} + +void libgit_configset_init(struct config_set *cs) +{ + git_configset_init(cs); +} + +int libgit_configset_add_file(struct config_set *cs, const char *filename) +{ + return git_configset_add_file(cs, filename); +} + +int libgit_configset_get_int(struct config_set *cs, const char *key, int *dest) +{ + return git_configset_get_int(cs, key, dest); +} + +int libgit_configset_get_string(struct 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/cgit-rs/public_symbol_export.h b/contrib/cgit-rs/public_symbol_export.h index 97e8e871ba..f22937202a 100644 --- a/contrib/cgit-rs/public_symbol_export.h +++ b/contrib/cgit-rs/public_symbol_export.h @@ -9,6 +9,16 @@ void libgit_initialize_the_repository(void); int libgit_parse_maybe_bool(const char *val); +struct config_set *libgit_configset_alloc(void); + +void libgit_configset_init(struct config_set *cs); + +int libgit_configset_add_file(struct config_set *cs, const char *filename); + +int libgit_configset_get_int(struct config_set *cs, const char *key, int *dest); + +int libgit_configset_get_string(struct 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/cgit-rs/src/lib.rs b/contrib/cgit-rs/src/lib.rs index df350e758f..acfd3659e8 100644 --- a/contrib/cgit-rs/src/lib.rs +++ b/contrib/cgit-rs/src/lib.rs @@ -1,4 +1,57 @@ -use libc::{c_char, c_int}; +use std::ffi::{CStr, CString}; + +use libc::{c_char, c_int, c_void}; + +pub enum GitConfigSet {} + +pub struct ConfigSet(*mut GitConfigSet); +impl ConfigSet { + + pub fn new() -> Self { + unsafe { + // TODO: we need to handle freeing this when the ConfigSet is dropped + // git_configset_clear(ptr) and then libc::free(ptr) + let ptr = libgit_configset_alloc(); + libgit_configset_init(ptr); + ConfigSet(ptr) + } + } + + pub fn add_files(&mut self, files: &[&str]) { + for file in files { + let rs = CString::new(*file).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()); + libc::free(val as *mut c_void); // Free the xstrdup()ed pointer from the C side + Some(owned_str.unwrap()) + } + } +} extern "C" { pub fn libgit_setup_git_directory() -> *const c_char; @@ -15,4 +68,14 @@ extern "C" { // From version.c pub fn libgit_user_agent() -> *const c_char; pub fn libgit_user_agent_sanitized() -> *const c_char; + + fn libgit_configset_alloc() -> *mut GitConfigSet; + + fn libgit_configset_init(cs: *mut GitConfigSet); + + fn libgit_configset_add_file(cs: *mut GitConfigSet, filename: *const c_char) -> c_int; + + pub fn libgit_configset_get_int(cs: *mut GitConfigSet, key: *const c_char, int: *mut c_int) -> c_int; + pub fn libgit_configset_get_string(cs: *mut GitConfigSet, key: *const c_char, dest: *mut *mut c_char) -> c_int; + } diff --git a/contrib/cgit-rs/src/main.rs b/contrib/cgit-rs/src/main.rs index c5f8644fca..07c8a71a13 100644 --- a/contrib/cgit-rs/src/main.rs +++ b/contrib/cgit-rs/src/main.rs @@ -1,4 +1,5 @@ use std::ffi::{CStr, CString}; +use home::home_dir; fn main() { println!("Let's print some strings provided by Git"); @@ -28,4 +29,16 @@ fn main() { val ); }; + + println!("\nTry out our configset wrappers"); + let mut cs = cgit::ConfigSet::new(); + let mut path = home_dir().expect("cannot get home directory path"); + path.push(".gitconfig"); + let path:String = path.into_os_string().into_string().unwrap(); + cs.add_files(&["/etc/gitconfig", ".gitconfig", &path]); + /* + * Returns Some(x) if defined in local config, otherwise None + */ + println!("get_configset_get_int = {:?}", cs.get_int("trace2.eventNesting")); + println!("cs.get_str(\"garbage\") = {:?}", cs.get_str("this_string_does_not_exist")); }