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.