5 static struct refspec_item s_tag_refspec
= {
14 /* See TAG_REFSPEC for the string version */
15 const struct refspec_item
*tag_refspec
= &s_tag_refspec
;
17 static struct refspec_item
*parse_refspec_internal(int nr_refspec
, const char **refspec
, int fetch
, int verify
)
20 struct refspec_item
*rs
= xcalloc(nr_refspec
, sizeof(*rs
));
22 for (i
= 0; i
< nr_refspec
; i
++) {
25 const char *lhs
, *rhs
;
36 rhs
= strrchr(lhs
, ':');
39 * Before going on, special case ":" (or "+:") as a refspec
40 * for pushing matching refs.
42 if (!fetch
&& rhs
== lhs
&& rhs
[1] == '\0') {
48 size_t rlen
= strlen(++rhs
);
49 is_glob
= (1 <= rlen
&& strchr(rhs
, '*'));
50 rs
[i
].dst
= xstrndup(rhs
, rlen
);
53 llen
= (rhs ?
(rhs
- lhs
- 1) : strlen(lhs
));
54 if (1 <= llen
&& memchr(lhs
, '*', llen
)) {
55 if ((rhs
&& !is_glob
) || (!rhs
&& fetch
))
58 } else if (rhs
&& is_glob
) {
62 rs
[i
].pattern
= is_glob
;
63 rs
[i
].src
= xstrndup(lhs
, llen
);
64 flags
= REFNAME_ALLOW_ONELEVEL
| (is_glob ? REFNAME_REFSPEC_PATTERN
: 0);
67 struct object_id unused
;
71 ; /* empty is ok; it means "HEAD" */
72 else if (llen
== GIT_SHA1_HEXSZ
&& !get_oid_hex(rs
[i
].src
, &unused
))
73 rs
[i
].exact_sha1
= 1; /* ok */
74 else if (!check_refname_format(rs
[i
].src
, flags
))
75 ; /* valid looking ref is ok */
80 ; /* missing is ok; it is the same as empty */
82 ; /* empty is ok; it means "do not store" */
83 else if (!check_refname_format(rs
[i
].dst
, flags
))
84 ; /* valid looking ref is ok */
90 * - empty is allowed; it means delete.
91 * - when wildcarded, it must be a valid looking ref.
92 * - otherwise, it must be an extended SHA-1, but
93 * there is no existing way to validate this.
98 if (check_refname_format(rs
[i
].src
, flags
))
102 ; /* anything goes, for now */
105 * - missing is allowed, but LHS then must be a
107 * - empty is not allowed.
108 * - otherwise it must be a valid looking ref.
111 if (check_refname_format(rs
[i
].src
, flags
))
113 } else if (!*rs
[i
].dst
) {
116 if (check_refname_format(rs
[i
].dst
, flags
))
126 * nr_refspec must be greater than zero and i must be valid
127 * since it is only possible to reach this point from within
128 * the for loop above.
130 free_refspec(i
+1, rs
);
133 die("Invalid refspec '%s'", refspec
[i
]);
136 int valid_fetch_refspec(const char *fetch_refspec_str
)
138 struct refspec_item
*refspec
;
140 refspec
= parse_refspec_internal(1, &fetch_refspec_str
, 1, 1);
141 free_refspec(1, refspec
);
145 struct refspec_item
*parse_fetch_refspec(int nr_refspec
, const char **refspec
)
147 return parse_refspec_internal(nr_refspec
, refspec
, 1, 0);
150 struct refspec_item
*parse_push_refspec(int nr_refspec
, const char **refspec
)
152 return parse_refspec_internal(nr_refspec
, refspec
, 0, 0);
155 void free_refspec(int nr_refspec
, struct refspec_item
*refspec
)
162 for (i
= 0; i
< nr_refspec
; i
++) {
163 free(refspec
[i
].src
);
164 free(refspec
[i
].dst
);