Commit ac9f92316b4fcace1c591a33d20fa4514f3ec2c6

Russell Belfer 2014-01-03T14:40:25

Merge pull request #2022 from KTXSoftware/development submodule branch option + little VS2013 fix

diff --git a/include/git2/submodule.h b/include/git2/submodule.h
index 4b4ba6e..907e5a1 100644
--- a/include/git2/submodule.h
+++ b/include/git2/submodule.h
@@ -284,6 +284,14 @@ GIT_EXTERN(const char *) git_submodule_path(git_submodule *submodule);
 GIT_EXTERN(const char *) git_submodule_url(git_submodule *submodule);
 
 /**
+* Get the branch for the submodule.
+*
+* @param submodule Pointer to submodule object
+* @return Pointer to the submodule branch
+*/
+GIT_EXTERN(const char *) git_submodule_branch(git_submodule *submodule);
+
+/**
  * Set the URL for the submodule.
  *
  * This sets the URL in memory for the submodule. This will be used for
diff --git a/src/config_file.c b/src/config_file.c
index 0971aa7..2e78f7c 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -213,7 +213,7 @@ static int config_refresh(git_config_backend *cfg)
 	int res = 0, updated = 0, any_updated = 0;
 	diskfile_backend *b = (diskfile_backend *)cfg;
 	git_strmap *old_values;
-	struct reader *reader;
+	struct reader *reader = NULL;
 	uint32_t i;
 
 	for (i = 0; i < git_array_size(b->readers); i++) {
diff --git a/src/submodule.c b/src/submodule.c
index 26dfe2c..3ffbfdb 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -472,6 +472,10 @@ int git_submodule_save(git_submodule *submodule)
 		(error = git_config_file_set_string(mods, key.ptr, submodule->url)) < 0)
 		goto cleanup;
 
+	if ((error = submodule_config_key_trunc_puts(&key, "branch")) < 0 ||
+		(error = git_config_file_set_string(mods, key.ptr, submodule->branch)) < 0)
+		goto cleanup;
+
 	if (!(error = submodule_config_key_trunc_puts(&key, "update")) &&
 		(val = git_submodule_update_to_str(submodule->update)) != NULL)
 		error = git_config_file_set_string(mods, key.ptr, val);
@@ -528,6 +532,12 @@ const char *git_submodule_url(git_submodule *submodule)
 	return submodule->url;
 }
 
+const char *git_submodule_branch(git_submodule *submodule)
+{
+	assert(submodule);
+	return submodule->branch;
+}
+
 int git_submodule_set_url(git_submodule *submodule, const char *url)
 {
 	assert(submodule && url);
@@ -992,6 +1002,7 @@ static git_submodule *submodule_alloc(git_repository *repo, const char *name)
 	sm->update = sm->update_default = GIT_SUBMODULE_UPDATE_CHECKOUT;
 	sm->fetch_recurse = GIT_SUBMODULE_RECURSE_YES;
 	sm->repo   = repo;
+	sm->branch = NULL;
 
 	return sm;
 }
@@ -1190,6 +1201,15 @@ static int submodule_load_from_config(
 			goto done;
 		}
 	}
+	else if (strcasecmp(property, "branch") == 0) {
+		git__free(sm->branch);
+		sm->branch = NULL;
+
+		if (value != NULL && (sm->branch = git__strdup(value)) == NULL) {
+			error = -1;
+			goto done;
+		}
+	}
 	else if (strcasecmp(property, "update") == 0) {
 		if ((error = git_submodule_parse_update(&sm->update, value)) < 0)
 			goto done;
diff --git a/src/submodule.h b/src/submodule.h
index 2a610e1..94748ac 100644
--- a/src/submodule.h
+++ b/src/submodule.h
@@ -81,6 +81,7 @@ struct git_submodule {
 	char *name;
 	char *path; /* important: may just point to "name" string */
 	char *url;
+	char *branch;
 	git_submodule_update_t update;
 	git_submodule_update_t update_default;
 	git_submodule_ignore_t ignore;