]> git.proxmox.com Git - libgit2.git/commitdiff
remote: validate refspecs before adding to config
authorCarlos Martín Nieto <cmn@dwim.me>
Sun, 17 May 2015 13:19:22 +0000 (15:19 +0200)
committerCarlos Martín Nieto <cmn@dwim.me>
Thu, 28 May 2015 13:32:20 +0000 (15:32 +0200)
When we moved from acting on the instance to acting on the
configuration, we dropped the validation of the passed refspec, which
can lead to writing an invalid refspec to the configuration. Bring that
validation back.

include/git2/remote.h
src/remote.c
tests/network/remote/remotes.c

index 86498a31a10c2852a68d6711e6c828c6ecc1fc61..577a1c853d397e1b13283d1b7aa4bdbf8937298e 100644 (file)
@@ -168,7 +168,7 @@ GIT_EXTERN(int) git_remote_set_pushurl(git_repository *repo, const char *remote,
  * @param repo the repository in which to change the configuration
  * @param remote the name of the remote to change
  * @param refspec the new fetch refspec
- * @return 0 or an error value
+ * @return 0, GIT_EINVALIDSPEC if refspec is invalid or an error value
  */
 GIT_EXTERN(int) git_remote_add_fetch(git_repository *repo, const char *remote, const char *refspec);
 
@@ -192,7 +192,7 @@ GIT_EXTERN(int) git_remote_get_fetch_refspecs(git_strarray *array, const git_rem
  * @param repo the repository in which to change the configuration
  * @param remote the name of the remote to change
  * @param refspec the new push refspec
- * @return 0 or an error value
+ * @return 0, GIT_EINVALIDSPEC if refspec is invalid or an error value
  */
 GIT_EXTERN(int) git_remote_add_push(git_repository *repo, const char *remote, const char *refspec);
 
index d58927f285adb65b2e59c9c86a357f56827c29de..99b5bacc02163563cf0191de2ff9ff4130fd48b0 100644 (file)
@@ -97,6 +97,7 @@ static int write_add_refspec(git_repository *repo, const char *name, const char
 {
        git_config *cfg;
        git_buf var = GIT_BUF_INIT;
+       git_refspec spec;
        const char *fmt;
        int error;
 
@@ -108,6 +109,15 @@ static int write_add_refspec(git_repository *repo, const char *name, const char
        if ((error = ensure_remote_name_is_valid(name)) < 0)
                return error;
 
+       if ((error = git_refspec__parse(&spec, refspec, fetch)) < 0) {
+               if (giterr_last()->klass != GITERR_NOMEMORY)
+                       error = GIT_EINVALIDSPEC;
+
+               return error;
+       }
+
+       git_refspec__free(&spec);
+
        if ((error = git_buf_printf(&var, fmt, name)) < 0)
                return error;
 
index 8619f2e00992b7c897db7e6f8e39fe828c8f369d..2fa21d460714e5b12dc72bd5a83b1fd63df7c1d7 100644 (file)
@@ -128,6 +128,8 @@ void test_network_remote_remotes__add_fetchspec(void)
        cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*");
        cl_assert_equal_s(git_refspec_string(_refspec), "refs/*:refs/*");
        cl_assert_equal_b(_refspec->push, false);
+
+       cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_add_fetch(_repo, "test", "refs/*/foo/*:refs/*"));
 }
 
 void test_network_remote_remotes__dup(void)