upload-pack: add deepen-since to cut shallow repos based on time

This should allow the user to say "create a shallow clone containing the work from last year" (once the client side is fixed up, of course). In theory deepen-since and deepen (aka --depth) can be used together to draw the shallow boundary (whether it's intersection or union is up to discussion, but if rev-list is used, it's likely intersection). However, because deepen goes with a custom commit walker, we can't mix the two yet. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jun 12, 2016 at 17:53 UTC 569e554be9cb88047d9f2752750e0c260241f446
3 files changed +54 -3
Documentation/technical/pack-protocol.txt
+2 -1
@@ -219,7 +219,8 @@ out of what the server said it could do with the first 'want' line.
219
220 shallow-line = PKT-LINE("shallow" SP obj-id)
221
222 - depth-request = PKT-LINE("deepen" SP depth)
222 + depth-request = PKT-LINE("deepen" SP depth) /
223 + PKT-LINE("deepen-since" SP timestamp)
224
225 first-want = PKT-LINE("want" SP obj-id SP capability-list)
226 additional-want = PKT-LINE("want" SP obj-id)
Documentation/technical/protocol-capabilities.txt
+9
@@ -179,6 +179,15 @@ This capability adds "deepen", "shallow" and "unshallow" commands to
179 the fetch-pack/upload-pack protocol so clients can request shallow
180 clones.
181
182 +deepen-since
183 +------------
184 +
185 +This capability adds "deepen-since" command to fetch-pack/upload-pack
186 +protocol so the client can request shallow clones that are cut at a
187 +specific time, instead of depth. Internally it's equivalent of doing
188 +"rev-list --max-age=<timestamp>" on the server side. "deepen-since"
189 +cannot be used with "deepen".
190 +
191 no-progress
192 -----------
193
upload-pack.c
+43 -2
@@ -14,6 +14,7 @@
14 #include "sigchain.h"
15 #include "version.h"
16 #include "string-list.h"
17 +#include "argv-array.h"
18
19 static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
20
@@ -629,11 +630,25 @@ static void deepen(int depth, const struct object_array *shallows)
630 packet_flush(1);
631 }
632
633 +static void deepen_by_rev_list(int ac, const char **av,
634 + struct object_array *shallows)
635 +{
636 + struct commit_list *result;
637 +
638 + result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
639 + send_shallow(result);
640 + free_commit_list(result);
641 + send_unshallow(shallows);
642 + packet_flush(1);
643 +}
644 +
645 static void receive_needs(void)
646 {
647 struct object_array shallows = OBJECT_ARRAY_INIT;
648 int depth = 0;
649 int has_non_tip = 0;
650 + unsigned long deepen_since = 0;
651 + int deepen_rev_list = 0;
652
653 shallow_nr = 0;
654 for (;;) {
@@ -670,6 +685,16 @@ static void receive_needs(void)
685 die("Invalid deepen: %s", line);
686 continue;
687 }
688 + if (skip_prefix(line, "deepen-since ", &arg)) {
689 + char *end = NULL;
690 + deepen_since = strtoul(arg, &end, 0);
691 + if (!end || *end || !deepen_since ||
692 + /* revisions.c's max_age -1 is special */
693 + deepen_since == -1)
694 + die("Invalid deepen-since: %s", line);
695 + deepen_rev_list = 1;
696 + continue;
697 + }
698 if (!skip_prefix(line, "want ", &arg) ||
699 get_sha1_hex(arg, sha1_buf))
700 die("git upload-pack: protocol error, "
@@ -721,10 +746,26 @@ static void receive_needs(void)
746 if (!use_sideband && daemon_mode)
747 no_progress = 1;
748
724 - if (depth == 0 && shallows.nr == 0)
749 + if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
750 return;
751 + if (depth > 0 && deepen_rev_list)
752 + die("git upload-pack: deepen and deepen-since cannot be used together");
753 if (depth > 0)
754 deepen(depth, &shallows);
755 + else if (deepen_rev_list) {
756 + struct argv_array av = ARGV_ARRAY_INIT;
757 + int i;
758 +
759 + argv_array_push(&av, "rev-list");
760 + if (deepen_since)
761 + argv_array_pushf(&av, "--max-age=%lu", deepen_since);
762 + for (i = 0; i < want_obj.nr; i++) {
763 + struct object *o = want_obj.objects[i].item;
764 + argv_array_push(&av, oid_to_hex(&o->oid));
765 + }
766 + deepen_by_rev_list(av.argc, av.argv, &shallows);
767 + argv_array_clear(&av);
768 + }
769 else
770 if (shallows.nr > 0) {
771 int i;
@@ -773,7 +814,7 @@ static int send_ref(const char *refname, const struct object_id *oid,
814 int flag, void *cb_data)
815 {
816 static const char *capabilities = "multi_ack thin-pack side-band"
776 - " side-band-64k ofs-delta shallow no-progress"
817 + " side-band-64k ofs-delta shallow deepen-since no-progress"
818 " include-tag multi_ack_detailed";
819 const char *refname_nons = strip_namespace(refname);
820 struct object_id peeled;