upload-pack: strip refs before calling ref_is_hidden()
[gitweb.git] / upload-pack.c
index aa845765009ac7fb9105851306c3288b3f602369..4ca960ec9dc1966d735f908b41a1793a7eb36ae0 100644 (file)
@@ -35,7 +35,11 @@ static int multi_ack;
 static int no_done;
 static int use_thin_pack, use_ofs_delta, use_include_tag;
 static int no_progress, daemon_mode;
-static int allow_tip_sha1_in_want;
+/* Allow specifying sha1 if it is a ref tip. */
+#define ALLOW_TIP_SHA1 01
+/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
+#define ALLOW_REACHABLE_SHA1   02
+static unsigned int allow_unadvertised_object_request;
 static int shallow_nr;
 static struct object_array have_obj;
 static struct object_array want_obj;
@@ -74,7 +78,7 @@ static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
 {
        FILE *fp = cb_data;
        if (graft->nr_parent == -1)
-               fprintf(fp, "--shallow %s\n", sha1_to_hex(graft->sha1));
+               fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
        return 0;
 }
 
@@ -312,10 +316,8 @@ static int reachable(struct commit *want)
 
        commit_list_insert_by_date(want, &work);
        while (work) {
-               struct commit_list *list = work->next;
-               struct commit *commit = work->item;
-               free(work);
-               work = list;
+               struct commit_list *list;
+               struct commit *commit = pop_commit(&work);
 
                if (commit->object.flags & THEY_HAVE) {
                        want->object.flags |= COMMON_KNOWN;
@@ -442,8 +444,9 @@ static int get_common_commits(void)
 
 static int is_our_ref(struct object *o)
 {
-       return o->flags &
-               ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF);
+       int allow_hidden_ref = (allow_unadvertised_object_request &
+                       (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
+       return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
 }
 
 static void check_non_tip(void)
@@ -456,8 +459,12 @@ static void check_non_tip(void)
        char namebuf[42]; /* ^ + SHA-1 + LF */
        int i;
 
-       /* In the normal in-process case non-tip request can never happen */
-       if (!stateless_rpc)
+       /*
+        * In the normal in-process case without
+        * uploadpack.allowReachableSHA1InWant,
+        * non-tip requests can never happen.
+        */
+       if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
                goto error;
 
        cmd.argv = argv;
@@ -681,11 +688,11 @@ static void receive_needs(void)
 }
 
 /* return non-zero if the ref is hidden, otherwise 0 */
-static int mark_our_ref(const char *refname, const unsigned char *sha1)
+static int mark_our_ref(const char *refname, const struct object_id *oid)
 {
-       struct object *o = lookup_unknown_object(sha1);
+       struct object *o = lookup_unknown_object(oid->hash);
 
-       if (ref_is_hidden(refname)) {
+       if (refname && ref_is_hidden(refname)) {
                o->flags |= HIDDEN_REF;
                return 1;
        }
@@ -693,9 +700,10 @@ static int mark_our_ref(const char *refname, const unsigned char *sha1)
        return 0;
 }
 
-static int check_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
+static int check_ref(const char *refname, const struct object_id *oid,
+                    int flag, void *cb_data)
 {
-       mark_our_ref(refname, sha1);
+       mark_our_ref(strip_namespace(refname), oid);
        return 0;
 }
 
@@ -709,48 +717,52 @@ static void format_symref_info(struct strbuf *buf, struct string_list *symref)
                strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
 }
 
-static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
+static int send_ref(const char *refname, const struct object_id *oid,
+                   int flag, void *cb_data)
 {
        static const char *capabilities = "multi_ack thin-pack side-band"
                " side-band-64k ofs-delta shallow no-progress"
                " include-tag multi_ack_detailed";
        const char *refname_nons = strip_namespace(refname);
-       unsigned char peeled[20];
+       struct object_id peeled;
 
-       if (mark_our_ref(refname, sha1))
+       if (mark_our_ref(refname_nons, oid))
                return 0;
 
        if (capabilities) {
                struct strbuf symref_info = STRBUF_INIT;
 
                format_symref_info(&symref_info, cb_data);
-               packet_write(1, "%s %s%c%s%s%s%s agent=%s\n",
-                            sha1_to_hex(sha1), refname_nons,
+               packet_write(1, "%s %s%c%s%s%s%s%s agent=%s\n",
+                            oid_to_hex(oid), refname_nons,
                             0, capabilities,
-                            allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "",
+                            (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
+                                    " allow-tip-sha1-in-want" : "",
+                            (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
+                                    " allow-reachable-sha1-in-want" : "",
                             stateless_rpc ? " no-done" : "",
                             symref_info.buf,
                             git_user_agent_sanitized());
                strbuf_release(&symref_info);
        } else {
-               packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
+               packet_write(1, "%s %s\n", oid_to_hex(oid), refname_nons);
        }
        capabilities = NULL;
-       if (!peel_ref(refname, peeled))
-               packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
+       if (!peel_ref(refname, peeled.hash))
+               packet_write(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
        return 0;
 }
 
-static int find_symref(const char *refname, const unsigned char *sha1, int flag,
-                      void *cb_data)
+static int find_symref(const char *refname, const struct object_id *oid,
+                      int flag, void *cb_data)
 {
        const char *symref_target;
        struct string_list_item *item;
-       unsigned char unused[20];
+       struct object_id unused;
 
        if ((flag & REF_ISSYMREF) == 0)
                return 0;
-       symref_target = resolve_ref_unsafe(refname, 0, unused, &flag);
+       symref_target = resolve_ref_unsafe(refname, 0, unused.hash, &flag);
        if (!symref_target || (flag & REF_ISSYMREF) == 0)
                die("'%s' is a symref but it is not?", refname);
        item = string_list_append(cb_data, refname);
@@ -787,9 +799,17 @@ static void upload_pack(void)
 
 static int upload_pack_config(const char *var, const char *value, void *unused)
 {
-       if (!strcmp("uploadpack.allowtipsha1inwant", var))
-               allow_tip_sha1_in_want = git_config_bool(var, value);
-       else if (!strcmp("uploadpack.keepalive", var)) {
+       if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
+               if (git_config_bool(var, value))
+                       allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
+               else
+                       allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
+       } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
+               if (git_config_bool(var, value))
+                       allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
+               else
+                       allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
+       } else if (!strcmp("uploadpack.keepalive", var)) {
                keepalive = git_config_int(var, value);
                if (!keepalive)
                        keepalive = -1;