@matheusbsbs / matheus-bagliano / commits / 4229f1fa32
Fork 0

remote: let guess_remote_head() optionally return all matches

Determining HEAD is ambiguous since it is done by comparing SHA1s. In the case of multiple matches we return refs/heads/master if it matches, else we return the first match we encounter. builtin-remote needs all matches returned to it, so add a flag for it to request such. To be simple and consistent, the return value is now a copy (including peer_ref) of the matching refs. Originally contributed by Jeff King along with the prior commit as a single patch. Signed-off-by: Jay Soffian <jaysoffian@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jay Soffian committed Feb 27, 2009 at 14:10 UTC 4229f1fa325870d6b24fe2a4c7d2ed5f14c6f771
3 files changed +28 -17
builtin-clone.c
+1 -1
@@ -510,7 +510,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
510 mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
511
512 remote_head = find_ref_by_name(refs, "HEAD");
513 - head_points_at = guess_remote_head(remote_head, mapped_refs);
513 + head_points_at = guess_remote_head(remote_head, mapped_refs, 0);
514 }
515 else {
516 warning("You appear to have cloned an empty repository.");
remote.c
+19 -10
@@ -1460,24 +1460,33 @@ struct ref *get_local_heads(void)
1460 return local_refs;
1461 }
1462
1463 -const struct ref *guess_remote_head(const struct ref *head,
1464 - const struct ref *refs)
1463 +struct ref *guess_remote_head(const struct ref *head,
1464 + const struct ref *refs,
1465 + int all)
1466 {
1467 const struct ref *r;
1468 + struct ref *list = NULL;
1469 + struct ref **tail = &list;
1470
1471 if (!head)
1472 return NULL;
1473
1474 /* If refs/heads/master could be right, it is. */
1472 - r = find_ref_by_name(refs, "refs/heads/master");
1473 - if (r && !hashcmp(r->old_sha1, head->old_sha1))
1474 - return r;
1475 + if (!all) {
1476 + r = find_ref_by_name(refs, "refs/heads/master");
1477 + if (r && !hashcmp(r->old_sha1, head->old_sha1))
1478 + return copy_ref(r);
1479 + }
1480
1481 /* Look for another ref that points there */
1477 - for (r = refs; r; r = r->next)
1478 - if (r != head && !hashcmp(r->old_sha1, head->old_sha1))
1479 - return r;
1482 + for (r = refs; r; r = r->next) {
1483 + if (r != head && !hashcmp(r->old_sha1, head->old_sha1)) {
1484 + *tail = copy_ref(r);
1485 + tail = &((*tail)->next);
1486 + if (!all)
1487 + break;
1488 + }
1489 + }
1490
1481 - /* Nothing is the same */
1482 - return NULL;
1491 + return list;
1492 }
remote.h
+8 -6
@@ -139,12 +139,14 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs);
139 int format_tracking_info(struct branch *branch, struct strbuf *sb);
140
141 struct ref *get_local_heads(void);
142 -
142 /*
144 - * Look for a ref in refs whose SHA1 matches head, first checking if
145 - * refs/heads/master matches. Return NULL if nothing matches or if head
146 - * is NULL.
143 + * Find refs from a list which are likely to be pointed to by the given HEAD
144 + * ref. If 'all' is false, returns the most likely ref; otherwise, returns a
145 + * list of all candidate refs. If no match is found (or 'head' is NULL),
146 + * returns NULL. All returns are newly allocated and should be freed.
147 */
148 -const struct ref *guess_remote_head(const struct ref *head,
149 - const struct ref *refs);
148 +struct ref *guess_remote_head(const struct ref *head,
149 + const struct ref *refs,
150 + int all);
151 +
152 #endif