@matheusbsbs / matheus-bagliano / commits / 6cb4e6cc0f
Fork 0

remote: simplify guess_remote_head()

This function had complications which made it hard to extend. - It used to do two things: find the HEAD ref, and then find a matching ref, optionally returning the former via assignment to a passed-in pointer. Since finding HEAD is a one-liner, just have a caller do it themselves and pass it as an argument. - It used to manually search through the ref list for refs/heads/master; this can be a one-line call to find_ref_by_name. Originally contributed by Jeff King along with the next 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 25, 2009 at 03:32 UTC 6cb4e6cc0f5b2de1998492b0178eeb0f99d4a800
3 files changed +16 -32
builtin-clone.c
+2 -2
@@ -509,8 +509,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
509
510 mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
511
512 - head_points_at = guess_remote_head(refs, mapped_refs,
513 - &remote_head);
512 + remote_head = find_ref_by_name(refs, "HEAD");
513 + head_points_at = guess_remote_head(remote_head, mapped_refs);
514 }
515 else {
516 warning("You appear to have cloned an empty repository.");
remote.c
+8 -23
@@ -1452,37 +1452,22 @@ struct ref *get_local_heads(void)
1452 return local_refs;
1453 }
1454
1455 -const struct ref *guess_remote_head(const struct ref *refs,
1456 - const struct ref *mapped_refs,
1457 - const struct ref **remote_head_p)
1455 +const struct ref *guess_remote_head(const struct ref *head,
1456 + const struct ref *refs)
1457 {
1459 - const struct ref *remote_head = NULL;
1460 - const struct ref *remote_master = NULL;
1458 const struct ref *r;
1462 - for (r = refs; r; r = r->next)
1463 - if (!strcmp(r->name, "HEAD"))
1464 - remote_head = r;
1465 -
1466 - for (r = mapped_refs; r; r = r->next)
1467 - if (!strcmp(r->name, "refs/heads/master"))
1468 - remote_master = r;
1459
1470 - if (remote_head_p)
1471 - *remote_head_p = remote_head;
1472 -
1473 - /* If there's no HEAD value at all, never mind. */
1474 - if (!remote_head)
1460 + if (!head)
1461 return NULL;
1462
1463 /* If refs/heads/master could be right, it is. */
1478 - if (remote_master && !hashcmp(remote_master->old_sha1,
1479 - remote_head->old_sha1))
1480 - return remote_master;
1464 + r = find_ref_by_name(refs, "refs/heads/master");
1465 + if (r && !hashcmp(r->old_sha1, head->old_sha1))
1466 + return r;
1467
1468 /* Look for another ref that points there */
1483 - for (r = mapped_refs; r; r = r->next)
1484 - if (r != remote_head &&
1485 - !hashcmp(r->old_sha1, remote_head->old_sha1))
1469 + for (r = refs; r; r = r->next)
1470 + if (r != head && !hashcmp(r->old_sha1, head->old_sha1))
1471 return r;
1472
1473 /* Nothing is the same */
remote.h
+6 -7
@@ -139,13 +139,12 @@ 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 +
143 /*
143 - * Look in refs for HEAD. Then look for a matching SHA1 in mapped_refs,
144 - * first checking if refs/heads/master matches. Return NULL if nothing matches
145 - * or if there is no HEAD in refs. remote_head_p is assigned HEAD if not NULL.
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.
147 */
147 -const struct ref *guess_remote_head(const struct ref *refs,
148 - const struct ref *mapped_refs,
149 - const struct ref **remote_head_p);
150 -
148 +const struct ref *guess_remote_head(const struct ref *head,
149 + const struct ref *refs);
150 #endif