1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-09 12:36:08 +02:00

Merge branch 'ie/config-includeif-hostname' into seen

The conditional inclusion mechanism for configuration files learned
to switch on the hostname.

* ie/config-includeif-hostname:
  config: learn the "hostname:" includeIf condition
  t: add a test helper for getting hostname
This commit is contained in:
Junio C Hamano 2024-04-26 09:28:37 -07:00
commit a71906e14d
8 changed files with 85 additions and 2 deletions

View File

@ -188,6 +188,12 @@ As for the naming of this keyword, it is for forwards compatibility with
a naming scheme that supports more variable-based include conditions,
but currently Git only supports the exact keyword described above.
`hostname`::
The data that follows the keyword `hostname:` is taken to be a
pattern with standard globbing wildcards. If the current
hostname (output of gethostname(2)) matches the
pattern, the include condition is met.
A few more notes on matching via `gitdir` and `gitdir/i`:
* Symlinks in `$GIT_DIR` are not resolved before matching.
@ -263,6 +269,10 @@ Example
path = foo.inc
[remote "origin"]
url = https://example.com/git
; include only if the hostname of the machine matches some-hostname
[includeIf "hostname:some-hostname"]
path = foo.inc
----
Values

View File

@ -854,6 +854,7 @@ TEST_BUILTINS_OBJS += test-userdiff.o
TEST_BUILTINS_OBJS += test-wildmatch.o
TEST_BUILTINS_OBJS += test-windows-named-pipe.o
TEST_BUILTINS_OBJS += test-write-cache.o
TEST_BUILTINS_OBJS += test-xgethostname.o
TEST_BUILTINS_OBJS += test-xml-encode.o
# Do not add more tests here unless they have extra dependencies. Add

View File

@ -317,6 +317,21 @@ static int include_by_branch(const char *cond, size_t cond_len)
return ret;
}
static int include_by_hostname(const char *cond, size_t cond_len)
{
int ret;
char my_host[HOST_NAME_MAX + 1];
struct strbuf pattern = STRBUF_INIT;
if (xgethostname(my_host, sizeof(my_host)))
return 0;
strbuf_add(&pattern, cond, cond_len);
ret = !wildmatch(pattern.buf, my_host, 0);
strbuf_release(&pattern);
return ret;
}
static int add_remote_url(const char *var, const char *value,
const struct config_context *ctx UNUSED, void *data)
{
@ -406,6 +421,8 @@ static int include_condition_is_true(const struct key_value_info *kvi,
else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond,
&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 */
return 0;

View File

@ -88,6 +88,7 @@ static struct test_cmd cmds[] = {
{ "truncate", cmd__truncate },
{ "userdiff", cmd__userdiff },
{ "urlmatch-normalization", cmd__urlmatch_normalization },
{ "xgethostname", cmd__xgethostname },
{ "xml-encode", cmd__xml_encode },
{ "wildmatch", cmd__wildmatch },
#ifdef GIT_WINDOWS_NATIVE

View File

@ -81,6 +81,7 @@ int cmd__trace2(int argc, const char **argv);
int cmd__truncate(int argc, const char **argv);
int cmd__userdiff(int argc, const char **argv);
int cmd__urlmatch_normalization(int argc, const char **argv);
int cmd__xgethostname(int argc, const char **argv);
int cmd__xml_encode(int argc, const char **argv);
int cmd__wildmatch(int argc, const char **argv);
#ifdef GIT_WINDOWS_NATIVE

View File

@ -0,0 +1,12 @@
#include "test-tool.h"
int cmd__xgethostname(int argc, const char **argv)
{
char hostname[HOST_NAME_MAX + 1];
if (xgethostname(hostname, sizeof(hostname)))
die("unable to get the host name");
puts(hostname);
return 0;
}

View File

@ -357,4 +357,46 @@ test_expect_success 'include cycles are detected' '
grep "exceeded maximum include depth" stderr
'
test_expect_success 'conditional include, hostname' '
cat >>.git/config <<-EOF &&
[includeIf "hostname:$(test-tool xgethostname)a"]
path = bar12
EOF
cat >>.git/bar12 <<-EOF &&
[test]
twelve=12
EOF
test_must_fail git config test.twelve &&
cat >>.git/config <<-EOF &&
[includeIf "hostname:$(test-tool xgethostname)"]
path = bar12
EOF
echo 12 >expect &&
git config test.twelve >actual &&
test_cmp expect actual
'
test_expect_success 'conditional include, hostname, wildcard' '
cat >>.git/config <<-EOF &&
[includeIf "hostname:$(test-tool xgethostname)a*"]
path = bar13
EOF
cat >>.git/bar13 <<-EOF &&
[test]
thirteen = 13
EOF
test_must_fail git config test.thirteen &&
cat >>.git/config <<-EOF &&
[includeIf "hostname:$(test-tool xgethostname)*"]
path = bar13
EOF
echo 13 >expect &&
git config test.thirteen >actual &&
test_cmp expect actual
'
test_done

View File

@ -379,7 +379,6 @@ test_expect_success 'background auto gc respects lock for all operations' '
# now fake a concurrent gc that holds the lock; we can use our
# shell pid so that it looks valid.
hostname=$(hostname || echo unknown) &&
shell_pid=$$ &&
if test_have_prereq MINGW && test -f /proc/$shell_pid/winpid
then
@ -388,7 +387,7 @@ test_expect_success 'background auto gc respects lock for all operations' '
# the Windows PID in this case.
shell_pid=$(cat /proc/$shell_pid/winpid)
fi &&
printf "%d %s" "$shell_pid" "$hostname" >.git/gc.pid &&
printf "%d %s" "$shell_pid" "$(test-tool xgethostname)" >.git/gc.pid &&
# our gc should exit zero without doing anything
run_and_wait_for_auto_gc &&