3030
3131static int dwim_refspecs (git_vector * out , git_vector * refspecs , git_vector * refs );
3232static int lookup_remote_prune_config (git_remote * remote , git_config * config , const char * name );
33- char * apply_insteadof (git_config * config , const char * url , int direction );
33+ char * apply_insteadof (git_config * config , const char * url , int direction , int * matched );
3434
3535static int add_refspec_to (git_vector * vector , const char * string , bool is_fetch )
3636{
@@ -210,7 +210,9 @@ int git_remote_create_with_opts(git_remote **out, const char *url, const git_rem
210210 git_str var = GIT_STR_INIT ;
211211 git_str specbuf = GIT_STR_INIT ;
212212 const git_remote_create_options dummy_opts = GIT_REMOTE_CREATE_OPTIONS_INIT ;
213+ char * tmp ;
213214 int error = -1 ;
215+ int matched ;
214216
215217 GIT_ASSERT_ARG (out );
216218 GIT_ASSERT_ARG (url );
@@ -245,7 +247,12 @@ int git_remote_create_with_opts(git_remote **out, const char *url, const git_rem
245247 goto on_error ;
246248
247249 if (opts -> repository && !(opts -> flags & GIT_REMOTE_CREATE_SKIP_INSTEADOF )) {
248- remote -> url = apply_insteadof (config_ro , canonical_url .ptr , GIT_DIRECTION_FETCH );
250+ remote -> url = apply_insteadof (config_ro , canonical_url .ptr , GIT_DIRECTION_FETCH , & matched );
251+ tmp = apply_insteadof (config_ro , canonical_url .ptr , GIT_DIRECTION_PUSH , & matched );
252+ if (matched ) {
253+ remote -> pushurl = tmp ;
254+ GIT_ERROR_CHECK_ALLOC (remote -> pushurl );
255+ }
249256 } else {
250257 remote -> url = git__strdup (canonical_url .ptr );
251258 }
@@ -457,7 +464,9 @@ int git_remote_lookup(git_remote **out, git_repository *repo, const char *name)
457464 git_remote * remote = NULL ;
458465 git_str buf = GIT_STR_INIT ;
459466 const char * val ;
467+ char * tmp ;
460468 int error = 0 ;
469+ int matched ;
461470 git_config * config ;
462471 struct refspec_cb_data data = { NULL };
463472 bool optional_setting_found = false, found ;
@@ -498,8 +507,13 @@ int git_remote_lookup(git_remote **out, git_repository *repo, const char *name)
498507 remote -> download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO ;
499508
500509 if (found && strlen (val ) > 0 ) {
501- remote -> url = apply_insteadof (config , val , GIT_DIRECTION_FETCH );
510+ remote -> url = apply_insteadof (config , val , GIT_DIRECTION_FETCH , & matched );
502511 GIT_ERROR_CHECK_ALLOC (remote -> url );
512+ tmp = apply_insteadof (config , val , GIT_DIRECTION_PUSH , & matched );
513+ if (matched ) {
514+ remote -> pushurl = tmp ;
515+ GIT_ERROR_CHECK_ALLOC (remote -> pushurl );
516+ }
503517 }
504518
505519 val = NULL ;
@@ -518,7 +532,10 @@ int git_remote_lookup(git_remote **out, git_repository *repo, const char *name)
518532 }
519533
520534 if (found && strlen (val ) > 0 ) {
521- remote -> pushurl = apply_insteadof (config , val , GIT_DIRECTION_PUSH );
535+ if (remote -> pushurl ) {
536+ git__free (remote -> pushurl );
537+ }
538+ remote -> pushurl = apply_insteadof (config , val , GIT_DIRECTION_FETCH , & matched );
522539 GIT_ERROR_CHECK_ALLOC (remote -> pushurl );
523540 }
524541
@@ -2719,7 +2736,7 @@ int git_remote_push(git_remote *remote, const git_strarray *refspecs, const git_
27192736#define SUFFIX_FETCH "insteadof"
27202737#define SUFFIX_PUSH "pushinsteadof"
27212738
2722- char * apply_insteadof (git_config * config , const char * url , int direction )
2739+ char * apply_insteadof (git_config * config , const char * url , int direction , int * matched )
27232740{
27242741 size_t match_length , prefix_length , suffix_length ;
27252742 char * replacement = NULL ;
@@ -2732,6 +2749,8 @@ char *apply_insteadof(git_config *config, const char *url, int direction)
27322749 GIT_ASSERT_ARG_WITH_RETVAL (config , NULL );
27332750 GIT_ASSERT_ARG_WITH_RETVAL (url , NULL );
27342751 GIT_ASSERT_ARG_WITH_RETVAL (direction == GIT_DIRECTION_FETCH || direction == GIT_DIRECTION_PUSH , NULL );
2752+ GIT_ASSERT_ARG_WITH_RETVAL (matched , NULL );
2753+ * matched = 0 ;
27352754
27362755 /* Add 1 to prefix/suffix length due to the additional escaped dot */
27372756 prefix_length = strlen (PREFIX ) + 1 ;
@@ -2777,6 +2796,7 @@ char *apply_insteadof(git_config *config, const char *url, int direction)
27772796
27782797 git__free (replacement );
27792798
2799+ * matched = 1 ;
27802800 return result .ptr ;
27812801}
27822802
0 commit comments