From: Isabella Caselli Date: Mon, 12 Oct 2026 01:58:44 GMT Subject: [PATCH 1/3] var: add GIT_HOSTNAME Message-ID: <20261012015846.2649843-2-bellacaselli20@gmail.com> In-Reply-To: <20261012015846.2649843-1-bellacaselli20@gmail.com> Git asks the system for the name of the machine it runs on in a few places. For example, "git gc" records it in "gc.pid" to tell whether the gc holding the lock runs on the same machine. Users have no way to see which name Git uses, and depending on how the system is configured, it may be a short name ("myhost") or a fully qualified one ("myhost.example.com"). This matters more once the config can depend on the hostname, which a later commit adds as an includeIf condition. Without a way to see the value Git uses, users would have to guess what to write in their config file. Add a GIT_HOSTNAME variable to "git var" that prints the hostname Git sees. If the hostname can't be determined, "git var GIT_HOSTNAME" exits with a non-zero status, and "git var -l" omits the variable instead of failing. An option to "git config" to show the hostname was also discussed, but "git var" was chosen, since the hostname is not specific to the configuration code. The test only checks that a non-empty value is printed. The test has no independent way to know the expected hostname, as the "hostname" command may not agree with what Git sees on every platform. The includeIf tests added later check that the value matches what the configuration code uses. Suggested-by: Eric Sunshine Suggested-by: Jeff King Signed-off-by: Isabella Caselli --- Documentation/git-var.adoc | 3 +++ builtin/var.c | 13 +++++++++++++ t/t0007-git-var.sh | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/Documentation/git-var.adoc b/Documentation/git-var.adoc index 697c10aded..e813ab4fd2 100644 --- a/Documentation/git-var.adoc +++ b/Documentation/git-var.adoc @@ -84,6 +84,9 @@ endif::git-default-pager[] `GIT_CONFIG_GLOBAL`:: The path to the global (per-user) configuration files, if any. +`GIT_HOSTNAME`:: + The hostname of the machine, as reported by the operating system. + Most path values contain only one value. However, some can contain multiple values, which are separated by newlines, and are listed in order from highest to lowest priority. Callers should be prepared for any such path value to contain diff --git a/builtin/var.c b/builtin/var.c index cc3a43cde2..b427ebd61e 100644 --- a/builtin/var.c +++ b/builtin/var.c @@ -115,6 +115,15 @@ static char *git_config_val_global(int ident_flag UNUSED) return strbuf_detach(&buf, &unused); } +static char *hostname(int ident_flag UNUSED) +{ + char buf[HOST_NAME_MAX + 1]; + + if (xgethostname(buf, sizeof(buf))) + return NULL; + return xstrdup(buf); +} + struct git_var { const char *name; char *(*read)(int); @@ -166,6 +175,10 @@ static struct git_var git_vars[] = { .read = git_config_val_global, .multivalued = 1, }, + { + .name = "GIT_HOSTNAME", + .read = hostname, + }, { .name = "", .read = NULL, diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh index 2b60317758..f50c62a25e 100755 --- a/t/t0007-git-var.sh +++ b/t/t0007-git-var.sh @@ -224,6 +224,11 @@ test_expect_success 'GIT_CONFIG_GLOBAL points to the correct location' ' ) ' +test_expect_success 'GIT_HOSTNAME is not empty' ' + hostname=$(git var GIT_HOSTNAME) && + test -n "$hostname" +' + # For git var -l, we check only a representative variable; # testing the whole output would make our test too brittle with # respect to unrelated changes in the test suite's environment. -- 2.43.0