upload-pack: optionally allow fetching reachable sha1

With uploadpack.allowReachableSHA1InWant configuration option set on the server side, "git fetch" can make a request with a "want" line that names an object that has not been advertised (likely to have been obtained out of band or from a submodule pointer). Only objects reachable from the branch tips, i.e. the union of advertised branches and branches hidden by transfer.hideRefs, will be processed. Note that there is an associated cost of having to walk back the history to check the reachability. This feature can be used when obtaining the content of a certain commit, for which the sha1 is known, without the need of cloning the whole repository, especially if a shallow fetch is used. Useful cases are e.g. repositories containing large files in the history, fetching only the needed data for a submodule checkout, when sharing a sha1 without telling which exact branch it belongs to and in Gerrit, if you think in terms of commits instead of change numbers. (The Gerrit case has already been solved through allowTipSHA1InWant as every Gerrit change has a ref.) Signed-off-by: Fredrik Medley <fredrik.medley@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Fredrik Medley committed May 21, 2015 at 22:23 UTC 68ee628932c2196742b77d2961c5e16360734a62
6 files changed +97 -6
Documentation/config.txt
+6
@@ -2538,6 +2538,12 @@ uploadpack.allowTipSHA1InWant::
2538 of a hidden ref (by default, such a request is rejected).
2539 see also `uploadpack.hideRefs`.
2540
2541 +uploadpack.allowReachableSHA1InWant::
2542 + Allow `upload-pack` to accept a fetch request that asks for an
2543 + object that is reachable from any ref tip. However, note that
2544 + calculating object reachability is computationally expensive.
2545 + Defaults to `false`.
2546 +
2547 uploadpack.keepAlive::
2548 When `upload-pack` has started `pack-objects`, there may be a
2549 quiet period while `pack-objects` prepares the pack. Normally
Documentation/technical/http-protocol.txt
+2 -1
@@ -319,7 +319,8 @@ Servers SHOULD support all capabilities defined here.
319 Clients MUST send at least one "want" command in the request body.
320 Clients MUST NOT reference an id in a "want" command which did not
321 appear in the response obtained through ref discovery unless the
322 -server advertises capability `allow-tip-sha1-in-want`.
322 +server advertises capability `allow-tip-sha1-in-want` or
323 +`allow-reachable-sha1-in-want`.
324
325 compute_request = want_list
326 have_list
Documentation/technical/protocol-capabilities.txt
+7
@@ -260,6 +260,13 @@ If the upload-pack server advertises this capability, fetch-pack may
260 send "want" lines with SHA-1s that exist at the server but are not
261 advertised by upload-pack.
262
263 +allow-reachable-sha1-in-want
264 +----------------------------
265 +
266 +If the upload-pack server advertises this capability, fetch-pack may
267 +send "want" lines with SHA-1s that exist at the server but are not
268 +advertised by upload-pack.
269 +
270 push-cert=<nonce>
271 -----------------
272
fetch-pack.c
+9 -1
@@ -46,6 +46,8 @@ static struct prio_queue rev_list = { compare_commits_by_commit_date };
46 static int non_common_revs, multi_ack, use_sideband;
47 /* Allow specifying sha1 if it is a ref tip. */
48 #define ALLOW_TIP_SHA1 01
49 +/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50 +#define ALLOW_REACHABLE_SHA1 02
51 static unsigned int allow_unadvertised_object_request;
52
53 static void rev_list_push(struct commit *commit, int mark)
@@ -545,7 +547,8 @@ static void filter_refs(struct fetch_pack_args *args,
547 }
548
549 /* Append unmatched requests to the list */
548 - if ((allow_unadvertised_object_request & ALLOW_TIP_SHA1)) {
550 + if ((allow_unadvertised_object_request &
551 + (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1))) {
552 for (i = 0; i < nr_sought; i++) {
553 unsigned char sha1[20];
554
@@ -826,6 +829,11 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
829 fprintf(stderr, "Server supports allow-tip-sha1-in-want\n");
830 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
831 }
832 + if (server_supports("allow-reachable-sha1-in-want")) {
833 + if (args->verbose)
834 + fprintf(stderr, "Server supports allow-reachable-sha1-in-want\n");
835 + allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
836 + }
837 if (!server_supports("thin-pack"))
838 args->use_thin_pack = 0;
839 if (!server_supports("no-progress"))
t/t5516-fetch-push.sh
+55
@@ -1120,6 +1120,61 @@ test_expect_success 'fetch exact SHA1' '
1120 )
1121 '
1122
1123 +for configallowtipsha1inwant in true false
1124 +do
1125 + test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1126 + mk_empty testrepo &&
1127 + (
1128 + cd testrepo &&
1129 + git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1130 + git commit --allow-empty -m foo &&
1131 + git commit --allow-empty -m bar
1132 + ) &&
1133 + SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1134 + mk_empty shallow &&
1135 + (
1136 + cd shallow &&
1137 + test_must_fail git fetch --depth=1 ../testrepo/.git $SHA1 &&
1138 + git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1139 + git fetch --depth=1 ../testrepo/.git $SHA1 &&
1140 + git cat-file commit $SHA1
1141 + )
1142 + '
1143 +
1144 + test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1145 + mk_empty testrepo &&
1146 + (
1147 + cd testrepo &&
1148 + git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1149 + git commit --allow-empty -m foo &&
1150 + git commit --allow-empty -m bar &&
1151 + git commit --allow-empty -m xyz
1152 + ) &&
1153 + SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1154 + SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1155 + SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1156 + (
1157 + cd testrepo &&
1158 + git reset --hard $SHA1_2 &&
1159 + git cat-file commit $SHA1_1 &&
1160 + git cat-file commit $SHA1_3
1161 + ) &&
1162 + mk_empty shallow &&
1163 + (
1164 + cd shallow &&
1165 + test_must_fail git fetch ../testrepo/.git $SHA1_3 &&
1166 + test_must_fail git fetch ../testrepo/.git $SHA1_1 &&
1167 + git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1168 + git fetch ../testrepo/.git $SHA1_1 &&
1169 + git cat-file commit $SHA1_1 &&
1170 + test_must_fail git cat-file commit $SHA1_2 &&
1171 + git fetch ../testrepo/.git $SHA1_2 &&
1172 + git cat-file commit $SHA1_2 &&
1173 + test_must_fail git fetch ../testrepo/.git $SHA1_3
1174 + )
1175 + '
1176 +done
1177 +
1178 test_expect_success 'fetch follows tags by default' '
1179 mk_test testrepo heads/master &&
1180 rm -fr src dst &&
upload-pack.c
+18 -4
@@ -37,6 +37,8 @@ static int use_thin_pack, use_ofs_delta, use_include_tag;
37 static int no_progress, daemon_mode;
38 /* Allow specifying sha1 if it is a ref tip. */
39 #define ALLOW_TIP_SHA1 01
40 +/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
41 +#define ALLOW_REACHABLE_SHA1 02
42 static unsigned int allow_unadvertised_object_request;
43 static int shallow_nr;
44 static struct object_array have_obj;
@@ -444,7 +446,8 @@ static int get_common_commits(void)
446
447 static int is_our_ref(struct object *o)
448 {
447 - int allow_hidden_ref = (allow_unadvertised_object_request & ALLOW_TIP_SHA1);
449 + int allow_hidden_ref = (allow_unadvertised_object_request &
450 + (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
451 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
452 }
453
@@ -458,8 +461,12 @@ static void check_non_tip(void)
461 char namebuf[42]; /* ^ + SHA-1 + LF */
462 int i;
463
461 - /* In the normal in-process case non-tip request can never happen */
462 - if (!stateless_rpc)
464 + /*
465 + * In the normal in-process case without
466 + * uploadpack.allowReachableSHA1InWant,
467 + * non-tip requests can never happen.
468 + */
469 + if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
470 goto error;
471
472 cmd.argv = argv;
@@ -726,11 +733,13 @@ static int send_ref(const char *refname, const unsigned char *sha1, int flag, vo
733 struct strbuf symref_info = STRBUF_INIT;
734
735 format_symref_info(&symref_info, cb_data);
729 - packet_write(1, "%s %s%c%s%s%s%s agent=%s\n",
736 + packet_write(1, "%s %s%c%s%s%s%s%s agent=%s\n",
737 sha1_to_hex(sha1), refname_nons,
738 0, capabilities,
739 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
740 " allow-tip-sha1-in-want" : "",
741 + (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
742 + " allow-reachable-sha1-in-want" : "",
743 stateless_rpc ? " no-done" : "",
744 symref_info.buf,
745 git_user_agent_sanitized());
@@ -795,6 +804,11 @@ static int upload_pack_config(const char *var, const char *value, void *unused)
804 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
805 else
806 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
807 + } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
808 + if (git_config_bool(var, value))
809 + allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
810 + else
811 + allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
812 } else if (!strcmp("uploadpack.keepalive", var)) {
813 keepalive = git_config_int(var, value);
814 if (!keepalive)