@@ -230,6 +230,7 @@ static int create_internal(git_remote **out, const char *url, const git_remote_c
230230 git_config * config_ro = NULL , * config_rw ;
231231 git_buf canonical_url = GIT_BUF_INIT ;
232232 git_buf var = GIT_BUF_INIT ;
233+ git_buf specbuf = GIT_BUF_INIT ;
233234 const git_remote_create_options dummy_opts = GIT_REMOTE_CREATE_OPTIONS_INIT ;
234235 int error = -1 ;
235236
@@ -282,13 +283,24 @@ static int create_internal(git_remote **out, const char *url, const git_remote_c
282283 goto on_error ;
283284 }
284285
285- if (opts -> fetchspec != NULL ) {
286- if ((error = add_refspec (remote , opts -> fetchspec , true)) < 0 )
286+ if (opts -> fetchspec != NULL ||
287+ (opts -> name && !(opts -> flags & GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC ))) {
288+ const char * fetch = NULL ;
289+ if (opts -> fetchspec ) {
290+ fetch = opts -> fetchspec ;
291+ } else {
292+ if ((error = default_fetchspec_for_name (& specbuf , opts -> name )) < 0 )
293+ goto on_error ;
294+
295+ fetch = git_buf_cstr (& specbuf );
296+ }
297+
298+ if ((error = add_refspec (remote , fetch , true)) < 0 )
287299 goto on_error ;
288300
289301 /* only write for named remotes with a repository */
290302 if (opts -> repository && opts -> name &&
291- ((error = write_add_refspec (opts -> repository , opts -> name , opts -> fetchspec , true)) < 0 ||
303+ ((error = write_add_refspec (opts -> repository , opts -> name , fetch , true)) < 0 ||
292304 (error = lookup_remote_prune_config (remote , config_ro , opts -> name )) < 0 ))
293305 goto on_error ;
294306
@@ -314,6 +326,7 @@ static int create_internal(git_remote **out, const char *url, const git_remote_c
314326 git_remote_free (remote );
315327
316328 git_config_free (config_ro );
329+ git_buf_dispose (& specbuf );
317330 git_buf_dispose (& canonical_url );
318331 git_buf_dispose (& var );
319332 return error ;
@@ -336,7 +349,6 @@ int git_remote_create(git_remote **out, git_repository *repo, const char *name,
336349
337350 opts .repository = repo ;
338351 opts .name = name ;
339- opts .fetchspec = git_buf_cstr (& buf );
340352
341353 error = create_internal (out , url , & opts );
342354
@@ -361,6 +373,7 @@ int git_remote_create_with_fetchspec(git_remote **out, git_repository *repo, con
361373 opts .repository = repo ;
362374 opts .name = name ;
363375 opts .fetchspec = fetch ;
376+ opts .flags = GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC ;
364377
365378 return create_internal (out , url , & opts );
366379}
0 commit comments