resolve_ref(): extract a function get_packed_ref()
[gitweb.git] / refs.c
diff --git a/refs.c b/refs.c
index d2aac24a366f946ea5a256a7bc47f10d36f98a88..473f7f6bc6b01c69d12dc3997232aa0df2a7ce16 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -465,6 +465,23 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *re
        return retval;
 }
 
+/*
+ * Try to read ref from the packed references.  On success, set sha1
+ * and return 0; otherwise, return -1.
+ */
+static int get_packed_ref(const char *ref, unsigned char *sha1)
+{
+       struct ref_list *list = get_packed_refs(NULL);
+       while (list) {
+               if (!strcmp(ref, list->name)) {
+                       hashcpy(sha1, list->sha1);
+                       return 0;
+               }
+               list = list->next;
+       }
+       return -1;
+}
+
 /*
  * If the "reading" argument is set, this function finds out what _object_
  * the ref points at by "reading" the ref.  The ref, if it is not symbolic,
@@ -497,29 +514,36 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
                        return NULL;
 
                git_snpath(path, sizeof(path), "%s", ref);
-               /* Special case: non-existing file. */
+
                if (lstat(path, &st) < 0) {
-                       struct ref_list *list = get_packed_refs(NULL);
-                       while (list) {
-                               if (!strcmp(ref, list->name)) {
-                                       hashcpy(sha1, list->sha1);
-                                       if (flag)
-                                               *flag |= REF_ISPACKED;
-                                       return ref;
-                               }
-                               list = list->next;
+                       if (errno != ENOENT)
+                               return NULL;
+                       /*
+                        * The loose reference file does not exist;
+                        * check for a packed reference.
+                        */
+                       if (!get_packed_ref(ref, sha1)) {
+                               if (flag)
+                                       *flag |= REF_ISPACKED;
+                               return ref;
                        }
-                       if (reading || errno != ENOENT)
+                       /* The reference is not a packed reference, either. */
+                       if (reading) {
                                return NULL;
-                       hashclr(sha1);
-                       return ref;
+                       } else {
+                               hashclr(sha1);
+                               return ref;
+                       }
                }
 
                /* Follow "normalized" - ie "refs/.." symlinks by hand */
                if (S_ISLNK(st.st_mode)) {
                        len = readlink(path, buffer, sizeof(buffer)-1);
-                       if (len >= 5 && !memcmp("refs/", buffer, 5)) {
-                               buffer[len] = 0;
+                       if (len < 0)
+                               return NULL;
+                       buffer[len] = 0;
+                       if (!prefixcmp(buffer, "refs/") &&
+                                       !check_refname_format(buffer, 0)) {
                                strcpy(ref_buffer, buffer);
                                ref = ref_buffer;
                                if (flag)
@@ -543,25 +567,25 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
                        return NULL;
                len = read_in_full(fd, buffer, sizeof(buffer)-1);
                close(fd);
+               if (len < 0)
+                       return NULL;
+               while (len && isspace(buffer[len-1]))
+                       len--;
+               buffer[len] = '\0';
 
                /*
                 * Is it a symbolic ref?
                 */
-               if (len < 4 || memcmp("ref:", buffer, 4))
+               if (prefixcmp(buffer, "ref:"))
                        break;
                buf = buffer + 4;
-               len -= 4;
-               while (len && isspace(*buf))
-                       buf++, len--;
-               while (len && isspace(buf[len-1]))
-                       len--;
-               buf[len] = 0;
-               memcpy(ref_buffer, buf, len + 1);
-               ref = ref_buffer;
+               while (isspace(*buf))
+                       buf++;
+               ref = strcpy(ref_buffer, buf);
                if (flag)
                        *flag |= REF_ISSYMREF;
        }
-       if (len < 40 || get_sha1_hex(buffer, sha1))
+       if (get_sha1_hex(buffer, sha1))
                return NULL;
        return ref;
 }