upload-pack: factor out processing lines

Factor out the logic for processing shallow, deepen, deepen_since, and deepen_not lines into their own functions to simplify the 'receive_needs()' function in addition to making it easier to reuse some of this logic when implementing protocol_v2. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Mar 14, 2018 at 11:31 UTC ae2948f30c7de56e2805d26f78ea8205f6c98682
1 file changed +74 -39
upload-pack.c
+74 -39
@@ -724,6 +724,75 @@ static void deepen_by_rev_list(int ac, const char **av,
724 packet_flush(1);
725 }
726
727 +static int process_shallow(const char *line, struct object_array *shallows)
728 +{
729 + const char *arg;
730 + if (skip_prefix(line, "shallow ", &arg)) {
731 + struct object_id oid;
732 + struct object *object;
733 + if (get_oid_hex(arg, &oid))
734 + die("invalid shallow line: %s", line);
735 + object = parse_object(&oid);
736 + if (!object)
737 + return 1;
738 + if (object->type != OBJ_COMMIT)
739 + die("invalid shallow object %s", oid_to_hex(&oid));
740 + if (!(object->flags & CLIENT_SHALLOW)) {
741 + object->flags |= CLIENT_SHALLOW;
742 + add_object_array(object, NULL, shallows);
743 + }
744 + return 1;
745 + }
746 +
747 + return 0;
748 +}
749 +
750 +static int process_deepen(const char *line, int *depth)
751 +{
752 + const char *arg;
753 + if (skip_prefix(line, "deepen ", &arg)) {
754 + char *end = NULL;
755 + *depth = (int)strtol(arg, &end, 0);
756 + if (!end || *end || *depth <= 0)
757 + die("Invalid deepen: %s", line);
758 + return 1;
759 + }
760 +
761 + return 0;
762 +}
763 +
764 +static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
765 +{
766 + const char *arg;
767 + if (skip_prefix(line, "deepen-since ", &arg)) {
768 + char *end = NULL;
769 + *deepen_since = parse_timestamp(arg, &end, 0);
770 + if (!end || *end || !deepen_since ||
771 + /* revisions.c's max_age -1 is special */
772 + *deepen_since == -1)
773 + die("Invalid deepen-since: %s", line);
774 + *deepen_rev_list = 1;
775 + return 1;
776 + }
777 + return 0;
778 +}
779 +
780 +static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
781 +{
782 + const char *arg;
783 + if (skip_prefix(line, "deepen-not ", &arg)) {
784 + char *ref = NULL;
785 + struct object_id oid;
786 + if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
787 + die("git upload-pack: ambiguous deepen-not: %s", line);
788 + string_list_append(deepen_not, ref);
789 + free(ref);
790 + *deepen_rev_list = 1;
791 + return 1;
792 + }
793 + return 0;
794 +}
795 +
796 static void receive_needs(void)
797 {
798 struct object_array shallows = OBJECT_ARRAY_INIT;
@@ -745,49 +814,15 @@ static void receive_needs(void)
814 if (!line)
815 break;
816
748 - if (skip_prefix(line, "shallow ", &arg)) {
749 - struct object_id oid;
750 - struct object *object;
751 - if (get_oid_hex(arg, &oid))
752 - die("invalid shallow line: %s", line);
753 - object = parse_object(&oid);
754 - if (!object)
755 - continue;
756 - if (object->type != OBJ_COMMIT)
757 - die("invalid shallow object %s", oid_to_hex(&oid));
758 - if (!(object->flags & CLIENT_SHALLOW)) {
759 - object->flags |= CLIENT_SHALLOW;
760 - add_object_array(object, NULL, &shallows);
761 - }
817 + if (process_shallow(line, &shallows))
818 continue;
763 - }
764 - if (skip_prefix(line, "deepen ", &arg)) {
765 - char *end = NULL;
766 - depth = strtol(arg, &end, 0);
767 - if (!end || *end || depth <= 0)
768 - die("Invalid deepen: %s", line);
819 + if (process_deepen(line, &depth))
820 continue;
770 - }
771 - if (skip_prefix(line, "deepen-since ", &arg)) {
772 - char *end = NULL;
773 - deepen_since = parse_timestamp(arg, &end, 0);
774 - if (!end || *end || !deepen_since ||
775 - /* revisions.c's max_age -1 is special */
776 - deepen_since == -1)
777 - die("Invalid deepen-since: %s", line);
778 - deepen_rev_list = 1;
821 + if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
822 continue;
780 - }
781 - if (skip_prefix(line, "deepen-not ", &arg)) {
782 - char *ref = NULL;
783 - struct object_id oid;
784 - if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
785 - die("git upload-pack: ambiguous deepen-not: %s", line);
786 - string_list_append(&deepen_not, ref);
787 - free(ref);
788 - deepen_rev_list = 1;
823 + if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
824 continue;
790 - }
825 +
826 if (!skip_prefix(line, "want ", &arg) ||
827 get_oid_hex(arg, &oid_buf))
828 die("git upload-pack: protocol error, "