174
struct ref *refs,
175
struct ref **matched_ref)
176
{
177
- int match;
177
int patlen = strlen(pattern);
178
+ struct ref *matched_weak = NULL;
179
+ struct ref *matched = NULL;
180
+ int weak_match = 0;
181
+ int match = 0;
182
180
- for (match = 0; refs; refs = refs->next) {
183
+ for (weak_match = match = 0; refs; refs = refs->next) {
184
char *name = refs->name;
185
int namelen = strlen(name);
186
+ int weak_match;
187
+
188
if (namelen < patlen ||
189
memcmp(name + namelen - patlen, pattern, patlen))
190
continue;
191
if (namelen != patlen && name[namelen - patlen - 1] != '/')
192
continue;
188
- match++;
189
- *matched_ref = refs;
193
+
194
+ /* A match is "weak" if it is with refs outside
195
+ * heads or tags, and did not specify the pattern
196
+ * in full (e.g. "refs/remotes/origin/master") or at
197
+ * least from the toplevel (e.g. "remotes/origin/master");
198
+ * otherwise "git push $URL master" would result in
199
+ * ambiguity between remotes/origin/master and heads/master
200
+ * at the remote site.
201
+ */
202
+ if (namelen != patlen &&
203
+ patlen != namelen - 5 &&
204
+ strncmp(name, "refs/heads/", 11) &&
205
+ strncmp(name, "refs/tags/", 10)) {
206
+ /* We want to catch the case where only weak
207
+ * matches are found and there are multiple
208
+ * matches, and where more than one strong
209
+ * matches are found, as ambiguous. One
210
+ * strong match with zero or more weak matches
211
+ * are acceptable as a unique match.
212
+ */
213
+ matched_weak = refs;
214
+ weak_match++;
215
+ }
216
+ else {
217
+ matched = refs;
218
+ match++;
219
+ }
220
+ }
221
+ if (!matched) {
222
+ *matched_ref = matched_weak;
223
+ return weak_match;
224
+ }
225
+ else {
226
+ *matched_ref = matched;
227
+ return match;
228
}
191
- return match;
229
}
230
231
static void link_dst_tail(struct ref *ref, struct ref ***tail)