config: add "hostname" condition to includeIf
PATCH 1/1 config: add "hostname" condition to includeIf
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.
--- config.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-)
config.c +31 -6
@@ -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 ; }
Signature · 1 line
2.43.0
Hi,
Just a gentle ping to see if there is any feedback on this patch.
Thanks,
Ayush Sharma
Quoted 88 lines · monarch
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.homeThis allows users to write host-specific configuration without separate branches.
Signed-off-by monarch <[email protected]>--- config.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-)config.c +31
-7@@ -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
Quoted 27 lines · Ayush Sharma
Quoted 26 lines · monarch
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.homeThis allows users to write host-specific configuration without separate branches.
Signed-off-by monarch <[email protected]>---
config.c +13
-0+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.
Quoted 8 lines
config.c static int include_by_remote_url(struct config_include_data *inc, · line 413
Quoted 6 lines
config.c +6
-0+if (strcmp (actual_hostname, target_hostname.buf) ==0 ) + ret =1 ;// Success, the hostnames match! + +strbuf_release (&target_hostname); +return ret; +}Just a gentle ping to see if there is any feedback on this patch.
You can increase the likelihood of feedback by:
* Studying the related email thread pointed out by Junio in his response[1] to your initial inquiry, and taking all the open discussion points into consideration and providing answers for them in your proposed submission. (For instance, one of the open discussion points[2] was how a user would know the exact hostname to use with includeIf [hostname "..."]; a solution needs to be provided in order to move forward with the proposal.)
* Including with your patch the necessary documentation update (so users will be able to discover this new capability), as well as new tests.
* Studying Documentation/CodingGuidelines (for instance, this project uses /*...*/ comments, not //), and Documentation/SubmittingPatches (for instance, avoid changes, such as inserting unnecessary blank lines or arbitrarily reformatting code, unrelated to the purpose of your patch; use your proper name in the
Signed-off-by: footer).
* Consult the Git source code to see whether there is a better way to obtain the hostname than rolling your own (for instance, don't fall prey to gethostname() potentially omitting the terminating NUL from the buffer; instead use xgethostname() from Git's wrapper.c).
[1]: https://lore.kernel.org/git/[email protected]/
[2]: https://lore.kernel.org/git/CAPig+cT4fpX7Kczu0+H5TZnmpVqqq0h8nBafj4UqDs7Xv2Nf4A@mail.gmail.com/