@matheusbsbs / matheus-bagliano / commits / 8ef517337d
Fork 0

move locate_head() to remote.c

Move locate_head() to remote.c and rename it to guess_remote_head() to more accurately reflect what it does. This is in preparation for being able to call it from builtin-remote.c 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 8ef517337dc684a333111b46d88c3217202f48c3
3 files changed +49 -38
builtin-clone.c
+3 -38
@@ -20,6 +20,7 @@
20 #include "dir.h"
21 #include "pack-refs.h"
22 #include "sigchain.h"
23 +#include "remote.h"
24
25 /*
26 * Overall FIXMEs:
@@ -293,43 +294,6 @@ static void remove_junk_on_signal(int signo)
294 raise(signo);
295 }
296
296 -static const struct ref *locate_head(const struct ref *refs,
297 - const struct ref *mapped_refs,
298 - const struct ref **remote_head_p)
299 -{
300 - const struct ref *remote_head = NULL;
301 - const struct ref *remote_master = NULL;
302 - const struct ref *r;
303 - for (r = refs; r; r = r->next)
304 - if (!strcmp(r->name, "HEAD"))
305 - remote_head = r;
306 -
307 - for (r = mapped_refs; r; r = r->next)
308 - if (!strcmp(r->name, "refs/heads/master"))
309 - remote_master = r;
310 -
311 - if (remote_head_p)
312 - *remote_head_p = remote_head;
313 -
314 - /* If there's no HEAD value at all, never mind. */
315 - if (!remote_head)
316 - return NULL;
317 -
318 - /* If refs/heads/master could be right, it is. */
319 - if (remote_master && !hashcmp(remote_master->old_sha1,
320 - remote_head->old_sha1))
321 - return remote_master;
322 -
323 - /* Look for another ref that points there */
324 - for (r = mapped_refs; r; r = r->next)
325 - if (r != remote_head &&
326 - !hashcmp(r->old_sha1, remote_head->old_sha1))
327 - return r;
328 -
329 - /* Nothing is the same */
330 - return NULL;
331 -}
332 -
297 static struct ref *write_remote_refs(const struct ref *refs,
298 struct refspec *refspec, const char *reflog)
299 {
@@ -545,7 +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
548 - head_points_at = locate_head(refs, mapped_refs, &remote_head);
512 + head_points_at = guess_remote_head(refs, mapped_refs,
513 + &remote_head);
514 }
515 else {
516 warning("You appear to have cloned an empty repository.");
remote.c
+37
@@ -1451,3 +1451,40 @@ struct ref *get_local_heads(void)
1451 for_each_ref(one_local_ref, &local_tail);
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)
1458 +{
1459 + const struct ref *remote_head = NULL;
1460 + const struct ref *remote_master = NULL;
1461 + 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;
1469 +
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)
1475 + return NULL;
1476 +
1477 + /* 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;
1481 +
1482 + /* 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))
1486 + return r;
1487 +
1488 + /* Nothing is the same */
1489 + return NULL;
1490 +}
remote.h
+9
@@ -139,4 +139,13 @@ 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 + * 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.
146 + */
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 +
151 #endif