summaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-12-18 11:48:57 -0800
committerRussell Belfer <rb@github.com>2014-04-01 09:45:20 -0700
commit8286300a1e2d24dfe184316c0f9798f2c69d0ef4 (patch)
treeb0ffb64c15a84f0cb1d138587e395986da7b1c7a /src/config.c
parent18234b14ad55157581ca26ec763afc1af3ec6e76 (diff)
downloadlibgit2-8286300a1e2d24dfe184316c0f9798f2c69d0ef4.tar.gz
Fix git_submodule_sync and add new config helper
This fixes `git_submodule_sync` to correctly update the remote URL of the default branch of the submodule along with the URL in the parent repository config (i.e. match core Git's behavior). Also move some useful helper logic from the submodule code into a shared config API `git_config__update_entry` that can either set or delete an entry with constraints like not overwriting or not creating a new entry. I used that helper to update a couple other places in the code.
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/config.c b/src/config.c
index 1a205fe13..b3168f735 100644
--- a/src/config.c
+++ b/src/config.c
@@ -615,6 +615,36 @@ int git_config_set_string(git_config *cfg, const char *name, const char *value)
return error;
}
+int git_config__update_entry(
+ git_config *config,
+ const char *key,
+ const char *value,
+ bool overwrite_existing,
+ bool only_if_existing)
+{
+ int error = 0;
+ const git_config_entry *ce = NULL;
+
+ if ((error = git_config__lookup_entry(&ce, config, key, false)) < 0)
+ return error;
+
+ if (!ce && only_if_existing) /* entry doesn't exist */
+ return 0;
+ if (ce && !overwrite_existing) /* entry would be overwritten */
+ return 0;
+ if (value && ce && ce->value && !strcmp(ce->value, value)) /* no change */
+ return 0;
+ if (!value && (!ce || !ce->value)) /* asked to delete absent entry */
+ return 0;
+
+ if (!value)
+ error = git_config_delete_entry(config, key);
+ else
+ error = git_config_set_string(config, key, value);
+
+ return error;
+}
+
/***********
* Getters
***********/