public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: monarch <ayushoffinfo17@gmail•com>
To: git@vger•kernel.org
Cc: monarch <ayushoffinfo17@gmail•com>
Subject: [PATCH] config: add "hostname" condition to includeIf
Date: Fri, 22 Aug 2025 14:19:09 +0530	[thread overview]
Message-ID: <20250822084910.98308-1-ayushoffinfo17@gmail.com> (raw)

Teach "includeIf" to include configuration based on the machine's hostname,
as returned by gethostname(2).

Example:

    [includeIf "hostname:work-laptop"]
        path = ~/.gitconfig.work

    [includeIf "hostname:home-pc"]
        path = ~/.gitconfig.home

This allows users to write host-specific configuration without separate branches.

Signed-off-by: monarch <ayushoffinfo17@gmail•com>
---
 config.c | 37 +++++++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)

diff --git a/config.c b/config.c
index e0ff35d426..dbc1a2bc75 100644
--- a/config.c
+++ b/config.c
@@ -7,6 +7,7 @@
  */
 
 #include "git-compat-util.h"
+#include <unistd.h>
 #include "abspath.h"
 #include "date.h"
 #include "branch.h"
@@ -391,23 +392,47 @@ static int include_by_remote_url(struct config_include_data *inc,
 					     inc->remote_urls);
 }
 
+static int include_by_hostname(const char *cond, size_t cond_len)
+{
+    char actual_hostname[1024];
+    struct strbuf target_hostname = STRBUF_INIT;
+    int ret = 0;
+
+    // Make sure the call to gethostname is correct and its return value is checked.
+    if (gethostname(actual_hostname, sizeof(actual_hostname)) != 0)
+        return 0; // If it fails, the condition is false.
+
+    strbuf_add(&target_hostname, cond, cond_len);
+
+    // The core of the logic: strcmp returns 0 when strings are equal.
+    if (strcmp(actual_hostname, target_hostname.buf) == 0)
+        ret = 1; // Success, the hostnames match!
+
+    strbuf_release(&target_hostname);
+    return ret;
+}
+
 static int include_condition_is_true(const struct key_value_info *kvi,
-				     struct config_include_data *inc,
-				     const char *cond, size_t cond_len)
+	struct config_include_data *inc,
+	const char *cond, size_t cond_len)
 {
-	const struct config_options *opts = inc->opts;
+const struct config_options *opts = inc->opts;
+
+
 
 	if (skip_prefix_mem(cond, cond_len, "gitdir:", &cond, &cond_len))
 		return include_by_gitdir(kvi, opts, cond, cond_len, 0);
 	else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len))
 		return include_by_gitdir(kvi, opts, cond, cond_len, 1);
-	else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len))
+	else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, cond_len))
 		return include_by_branch(inc, cond, cond_len);
 	else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond,
-				   &cond_len))
+  &cond_len))
 		return include_by_remote_url(inc, cond, cond_len);
+	else if (skip_prefix_mem(cond, cond_len, "hostname:", &cond, &cond_len))
+		return include_by_hostname(cond, cond_len);
 
-	/* unknown conditionals are always false */
+/* unknown conditionals are always false */
 	return 0;
 }
 
-- 
2.43.0


             reply	other threads:[~2025-08-22  8:49 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-22  8:49 monarch [this message]
2025-08-27 19:23 ` [PATCH] config: add "hostname" condition to includeIf Ayush Sharma
2025-08-27 20:54   ` Eric Sunshine
  -- strict thread matches above, loose matches on Subject: below --
2025-08-22  8:35 monarch
2025-08-22  8:22 monarch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250822084910.98308-1-ayushoffinfo17@gmail.com \
    --to=ayushoffinfo17@gmail$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox