@@ -429,6 +429,16 @@ static struct refspec *parse_refspec_internal(int nr_refspec, const char **refsp
429429 }
430430
431431 rhs = strrchr (lhs , ':' );
432+
433+ /*
434+ * Before going on, special case ":" (or "+:") as a refspec
435+ * for matching refs.
436+ */
437+ if (!fetch && rhs == lhs && rhs [1 ] == '\0' ) {
438+ rs [i ].matching = 1 ;
439+ continue ;
440+ }
441+
432442 if (rhs ) {
433443 rhs ++ ;
434444 rlen = strlen (rhs );
@@ -842,7 +852,7 @@ static int match_explicit(struct ref *src, struct ref *dst,
842852 const char * dst_value = rs -> dst ;
843853 char * dst_guess ;
844854
845- if (rs -> pattern )
855+ if (rs -> pattern || rs -> matching )
846856 return errs ;
847857
848858 matched_src = matched_dst = NULL ;
@@ -932,13 +942,23 @@ static const struct refspec *check_pattern_match(const struct refspec *rs,
932942 const struct ref * src )
933943{
934944 int i ;
945+ int matching_refs = -1 ;
935946 for (i = 0 ; i < rs_nr ; i ++ ) {
947+ if (rs [i ].matching &&
948+ (matching_refs == -1 || rs [i ].force )) {
949+ matching_refs = i ;
950+ continue ;
951+ }
952+
936953 if (rs [i ].pattern &&
937954 !prefixcmp (src -> name , rs [i ].src ) &&
938955 src -> name [strlen (rs [i ].src )] == '/' )
939956 return rs + i ;
940957 }
941- return NULL ;
958+ if (matching_refs != -1 )
959+ return rs + matching_refs ;
960+ else
961+ return NULL ;
942962}
943963
944964/*
@@ -949,11 +969,16 @@ static const struct refspec *check_pattern_match(const struct refspec *rs,
949969int match_refs (struct ref * src , struct ref * dst , struct ref * * * dst_tail ,
950970 int nr_refspec , const char * * refspec , int flags )
951971{
952- struct refspec * rs =
953- parse_push_refspec (nr_refspec , (const char * * ) refspec );
972+ struct refspec * rs ;
954973 int send_all = flags & MATCH_REFS_ALL ;
955974 int send_mirror = flags & MATCH_REFS_MIRROR ;
975+ static const char * default_refspec [] = { ":" , 0 };
956976
977+ if (!nr_refspec ) {
978+ nr_refspec = 1 ;
979+ refspec = default_refspec ;
980+ }
981+ rs = parse_push_refspec (nr_refspec , (const char * * ) refspec );
957982 if (match_explicit_refs (src , dst , dst_tail , rs , nr_refspec ))
958983 return -1 ;
959984
@@ -964,48 +989,50 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
964989 char * dst_name ;
965990 if (src -> peer_ref )
966991 continue ;
967- if ( nr_refspec ) {
968- pat = check_pattern_match (rs , nr_refspec , src );
969- if (!pat )
970- continue ;
971- }
972- else if (! send_mirror && prefixcmp ( src -> name , "refs/heads/" ))
992+
993+ pat = check_pattern_match (rs , nr_refspec , src );
994+ if (!pat )
995+ continue ;
996+
997+ if (pat -> matching ) {
973998 /*
974999 * "matching refs"; traditionally we pushed everything
9751000 * including refs outside refs/heads/ hierarchy, but
9761001 * that does not make much sense these days.
9771002 */
978- continue ;
1003+ if (!send_mirror && prefixcmp (src -> name , "refs/heads/" ))
1004+ continue ;
1005+ dst_name = xstrdup (src -> name );
9791006
980- if ( pat ) {
1007+ } else {
9811008 const char * dst_side = pat -> dst ? pat -> dst : pat -> src ;
9821009 dst_name = xmalloc (strlen (dst_side ) +
9831010 strlen (src -> name ) -
9841011 strlen (pat -> src ) + 2 );
9851012 strcpy (dst_name , dst_side );
9861013 strcat (dst_name , src -> name + strlen (pat -> src ));
987- } else
988- dst_name = xstrdup (src -> name );
1014+ }
9891015 dst_peer = find_ref_by_name (dst , dst_name );
990- if (dst_peer && dst_peer -> peer_ref )
991- /* We're already sending something to this ref. */
992- goto free_name ;
1016+ if (dst_peer ) {
1017+ if (dst_peer -> peer_ref )
1018+ /* We're already sending something to this ref. */
1019+ goto free_name ;
1020+
1021+ } else {
1022+ if (pat -> matching && !(send_all || send_mirror ))
1023+ /*
1024+ * Remote doesn't have it, and we have no
1025+ * explicit pattern, and we don't have
1026+ * --all nor --mirror.
1027+ */
1028+ goto free_name ;
9931029
994- if (!dst_peer && !nr_refspec && !(send_all || send_mirror ))
995- /*
996- * Remote doesn't have it, and we have no
997- * explicit pattern, and we don't have
998- * --all nor --mirror.
999- */
1000- goto free_name ;
1001- if (!dst_peer ) {
10021030 /* Create a new one and link it */
10031031 dst_peer = make_linked_ref (dst_name , dst_tail );
10041032 hashcpy (dst_peer -> new_sha1 , src -> new_sha1 );
10051033 }
10061034 dst_peer -> peer_ref = src ;
1007- if (pat )
1008- dst_peer -> force = pat -> force ;
1035+ dst_peer -> force = pat -> force ;
10091036 free_name :
10101037 free (dst_name );
10111038 }
0 commit comments