move locate_head() to remote.c
[gitweb.git] / remote.c
index d7079c6dd871dc1b482d347d013438fe30cc0908..49a183eb5ad52061331367c51caa15019af2e0fe 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -5,6 +5,7 @@
 #include "diff.h"
 #include "revision.h"
 #include "dir.h"
+#include "tag.h"
 
 static struct refspec s_tag_refspec = {
        0,
@@ -1269,6 +1270,54 @@ int resolve_remote_symref(struct ref *ref, struct ref *list)
        return 1;
 }
 
+static void unmark_and_free(struct commit_list *list, unsigned int mark)
+{
+       while (list) {
+               struct commit_list *temp = list;
+               temp->item->object.flags &= ~mark;
+               list = temp->next;
+               free(temp);
+       }
+}
+
+int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
+{
+       struct object *o;
+       struct commit *old, *new;
+       struct commit_list *list, *used;
+       int found = 0;
+
+       /* Both new and old must be commit-ish and new is descendant of
+        * old.  Otherwise we require --force.
+        */
+       o = deref_tag(parse_object(old_sha1), NULL, 0);
+       if (!o || o->type != OBJ_COMMIT)
+               return 0;
+       old = (struct commit *) o;
+
+       o = deref_tag(parse_object(new_sha1), NULL, 0);
+       if (!o || o->type != OBJ_COMMIT)
+               return 0;
+       new = (struct commit *) o;
+
+       if (parse_commit(new) < 0)
+               return 0;
+
+       used = list = NULL;
+       commit_list_insert(new, &list);
+       while (list) {
+               new = pop_most_recent_commit(&list, TMP_MARK);
+               commit_list_insert(new, &used);
+               if (new == old) {
+                       found = 1;
+                       break;
+               }
+       }
+       unmark_and_free(list, TMP_MARK);
+       unmark_and_free(used, TMP_MARK);
+       return found;
+}
+
 /*
  * Return true if there is anything to report, otherwise false.
  */
@@ -1376,3 +1425,66 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
                            base, num_ours, num_theirs);
        return 1;
 }
+
+static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
+{
+       struct ref ***local_tail = cb_data;
+       struct ref *ref;
+       int len;
+
+       /* we already know it starts with refs/ to get here */
+       if (check_ref_format(refname + 5))
+               return 0;
+
+       len = strlen(refname) + 1;
+       ref = xcalloc(1, sizeof(*ref) + len);
+       hashcpy(ref->new_sha1, sha1);
+       memcpy(ref->name, refname, len);
+       **local_tail = ref;
+       *local_tail = &ref->next;
+       return 0;
+}
+
+struct ref *get_local_heads(void)
+{
+       struct ref *local_refs, **local_tail = &local_refs;
+       for_each_ref(one_local_ref, &local_tail);
+       return local_refs;
+}
+
+const struct ref *guess_remote_head(const struct ref *refs,
+                                   const struct ref *mapped_refs,
+                                   const struct ref **remote_head_p)
+{
+       const struct ref *remote_head = NULL;
+       const struct ref *remote_master = NULL;
+       const struct ref *r;
+       for (r = refs; r; r = r->next)
+               if (!strcmp(r->name, "HEAD"))
+                       remote_head = r;
+
+       for (r = mapped_refs; r; r = r->next)
+               if (!strcmp(r->name, "refs/heads/master"))
+                       remote_master = r;
+
+       if (remote_head_p)
+               *remote_head_p = remote_head;
+
+       /* If there's no HEAD value at all, never mind. */
+       if (!remote_head)
+               return NULL;
+
+       /* If refs/heads/master could be right, it is. */
+       if (remote_master && !hashcmp(remote_master->old_sha1,
+                                     remote_head->old_sha1))
+               return remote_master;
+
+       /* Look for another ref that points there */
+       for (r = mapped_refs; r; r = r->next)
+               if (r != remote_head &&
+                   !hashcmp(r->old_sha1, remote_head->old_sha1))
+                       return r;
+
+       /* Nothing is the same */
+       return NULL;
+}