upload-pack: optionally allow fetching from the tips of hidden refs
[gitweb.git] / upload-pack.c
index 6142421ea1172f7fe6b57486a4f41b81d92bab85..c0390af82017f7c9d9737a6341bd7b7ec784583b 100644 (file)
@@ -12,6 +12,7 @@
 #include "run-command.h"
 #include "sigchain.h"
 #include "version.h"
+#include "string-list.h"
 
 static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
 
@@ -25,13 +26,15 @@ static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<
 #define SHALLOW                (1u << 16)
 #define NOT_SHALLOW    (1u << 17)
 #define CLIENT_SHALLOW (1u << 18)
+#define HIDDEN_REF     (1u << 19)
 
 static unsigned long oldest_have;
 
-static int multi_ack, nr_our_refs;
+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;
 static int shallow_nr;
 static struct object_array have_obj;
 static struct object_array want_obj;
@@ -139,7 +142,6 @@ static void create_pack_file(void)
 {
        struct async rev_list;
        struct child_process pack_objects;
-       int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
        char data[8193], progress[128];
        char abort_msg[] = "aborting due to possible repository "
                "corruption on the remote side.";
@@ -151,9 +153,7 @@ static void create_pack_file(void)
        argv[arg++] = "pack-objects";
        if (!shallow_nr) {
                argv[arg++] = "--revs";
-               if (create_full_pack)
-                       argv[arg++] = "--all";
-               else if (use_thin_pack)
+               if (use_thin_pack)
                        argv[arg++] = "--thin";
        }
 
@@ -185,15 +185,15 @@ static void create_pack_file(void)
        }
        else {
                FILE *pipe_fd = xfdopen(pack_objects.in, "w");
-               if (!create_full_pack) {
-                       int i;
-                       for (i = 0; i < want_obj.nr; i++)
-                               fprintf(pipe_fd, "%s\n", sha1_to_hex(want_obj.objects[i].item->sha1));
-                       fprintf(pipe_fd, "--not\n");
-                       for (i = 0; i < have_obj.nr; i++)
-                               fprintf(pipe_fd, "%s\n", sha1_to_hex(have_obj.objects[i].item->sha1));
-               }
+               int i;
 
+               for (i = 0; i < want_obj.nr; i++)
+                       fprintf(pipe_fd, "%s\n",
+                               sha1_to_hex(want_obj.objects[i].item->sha1));
+               fprintf(pipe_fd, "--not\n");
+               for (i = 0; i < have_obj.nr; i++)
+                       fprintf(pipe_fd, "%s\n",
+                               sha1_to_hex(have_obj.objects[i].item->sha1));
                fprintf(pipe_fd, "\n");
                fflush(pipe_fd);
                fclose(pipe_fd);
@@ -489,6 +489,12 @@ 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);
+}
+
 static void check_non_tip(void)
 {
        static const char *argv[] = {
@@ -525,7 +531,7 @@ static void check_non_tip(void)
                o = get_indexed_object(--i);
                if (!o)
                        continue;
-               if (!(o->flags & OUR_REF))
+               if (!is_our_ref(o))
                        continue;
                memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
                if (write_in_full(cmd.in, namebuf, 42) < 0)
@@ -534,7 +540,7 @@ static void check_non_tip(void)
        namebuf[40] = '\n';
        for (i = 0; i < want_obj.nr; i++) {
                o = want_obj.objects[i].item;
-               if (o->flags & OUR_REF)
+               if (is_our_ref(o))
                        continue;
                memcpy(namebuf, sha1_to_hex(o->sha1), 40);
                if (write_in_full(cmd.in, namebuf, 41) < 0)
@@ -568,7 +574,7 @@ static void check_non_tip(void)
        /* Pick one of them (we know there at least is one) */
        for (i = 0; i < want_obj.nr; i++) {
                o = want_obj.objects[i].item;
-               if (!(o->flags & OUR_REF))
+               if (!is_our_ref(o))
                        die("git upload-pack: not our ref %s",
                            sha1_to_hex(o->sha1));
        }
@@ -603,6 +609,8 @@ static void receive_needs(void)
                        object = parse_object(sha1);
                        if (!object)
                                die("did not find object for %s", line);
+                       if (object->type != OBJ_COMMIT)
+                               die("invalid shallow object %s", sha1_to_hex(sha1));
                        object->flags |= CLIENT_SHALLOW;
                        add_object_array(object, NULL, &shallows);
                        continue;
@@ -646,7 +654,7 @@ static void receive_needs(void)
                            sha1_to_hex(sha1_buf));
                if (!(o->flags & WANTED)) {
                        o->flags |= WANTED;
-                       if (!(o->flags & OUR_REF))
+                       if (!is_our_ref(o))
                                has_non_tip = 1;
                        add_object_array(o, NULL, &want_obj);
                }
@@ -720,45 +728,47 @@ static void receive_needs(void)
        free(shallows.objects);
 }
 
+/* return non-zero if the ref is hidden, otherwise 0 */
+static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
+{
+       struct object *o = lookup_unknown_object(sha1);
+
+       if (ref_is_hidden(refname)) {
+               o->flags |= HIDDEN_REF;
+               return 1;
+       }
+       if (!o)
+               die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
+       o->flags |= OUR_REF;
+       return 0;
+}
+
 static int send_ref(const char *refname, const unsigned char *sha1, 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";
-       struct object *o = lookup_unknown_object(sha1);
        const char *refname_nons = strip_namespace(refname);
        unsigned char peeled[20];
 
+       if (mark_our_ref(refname, sha1, flag, cb_data))
+               return 0;
+
        if (capabilities)
-               packet_write(1, "%s %s%c%s%s agent=%s\n",
+               packet_write(1, "%s %s%c%s%s%s agent=%s\n",
                             sha1_to_hex(sha1), refname_nons,
                             0, capabilities,
+                            allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "",
                             stateless_rpc ? " no-done" : "",
                             git_user_agent_sanitized());
        else
                packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
        capabilities = NULL;
-       if (!(o->flags & OUR_REF)) {
-               o->flags |= OUR_REF;
-               nr_our_refs++;
-       }
        if (!peel_ref(refname, peeled))
                packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
        return 0;
 }
 
-static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
-{
-       struct object *o = parse_object(sha1);
-       if (!o)
-               die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
-       if (!(o->flags & OUR_REF)) {
-               o->flags |= OUR_REF;
-               nr_our_refs++;
-       }
-       return 0;
-}
-
 static void upload_pack(void)
 {
        if (advertise_refs || !stateless_rpc) {
@@ -780,6 +790,13 @@ 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);
+       return parse_hide_refs_config(var, value, "uploadpack");
+}
+
 int main(int argc, char **argv)
 {
        char *dir;
@@ -831,6 +848,7 @@ int main(int argc, char **argv)
                die("'%s' does not appear to be a git repository", dir);
        if (is_repository_shallow())
                die("attempt to fetch/clone from a shallow repository");
+       git_config(upload_pack_config, NULL);
        if (getenv("GIT_DEBUG_SEND_PACK"))
                debug_fd = atoi(getenv("GIT_DEBUG_SEND_PACK"));
        upload_pack();