receive-pack: move more work into write_head_info()

Move some more code from the calling site into write_head_info(), and inline add_alternate_refs() there. (Some more simplification is coming, and it is easier if all this code is in the same place.) Move some helper functions to avoid the need for forward declarations. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Jan 6, 2012 at 15:12 UTC b7a025d921e416a708eb2d664d9ff51b303edd97
1 file changed +18 -24
builtin/receive-pack.c
+18 -24
@@ -143,12 +143,30 @@ static int show_ref_cb(const char *path, const unsigned char *sha1, int flag, vo
143 return show_ref(path, sha1, flag, cb_data);
144 }
145
146 +static void add_one_alternate_sha1(const unsigned char sha1[20], void *unused)
147 +{
148 + add_extra_ref(".have", sha1, 0);
149 +}
150 +
151 +static void collect_one_alternate_ref(const struct ref *ref, void *data)
152 +{
153 + struct sha1_array *sa = data;
154 + sha1_array_append(sa, ref->old_sha1);
155 +}
156 +
157 static void write_head_info(void)
158 {
159 + struct sha1_array sa = SHA1_ARRAY_INIT;
160 + for_each_alternate_ref(collect_one_alternate_ref, &sa);
161 + sha1_array_for_each_unique(&sa, add_one_alternate_sha1, NULL);
162 + sha1_array_clear(&sa);
163 for_each_ref(show_ref_cb, NULL);
164 if (!sent_capabilities)
165 show_ref("capabilities^{}", null_sha1, 0, NULL);
166 + clear_extra_refs();
167
168 + /* EOF */
169 + packet_flush(1);
170 }
171
172 struct command {
@@ -870,25 +888,6 @@ static int delete_only(struct command *commands)
888 return 1;
889 }
890
873 -static void add_one_alternate_sha1(const unsigned char sha1[20], void *unused)
874 -{
875 - add_extra_ref(".have", sha1, 0);
876 -}
877 -
878 -static void collect_one_alternate_ref(const struct ref *ref, void *data)
879 -{
880 - struct sha1_array *sa = data;
881 - sha1_array_append(sa, ref->old_sha1);
882 -}
883 -
884 -static void add_alternate_refs(void)
885 -{
886 - struct sha1_array sa = SHA1_ARRAY_INIT;
887 - for_each_alternate_ref(collect_one_alternate_ref, &sa);
888 - sha1_array_for_each_unique(&sa, add_one_alternate_sha1, NULL);
889 - sha1_array_clear(&sa);
890 -}
891 -
891 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
892 {
893 int advertise_refs = 0;
@@ -938,12 +937,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
937 unpack_limit = receive_unpack_limit;
938
939 if (advertise_refs || !stateless_rpc) {
941 - add_alternate_refs();
940 write_head_info();
943 - clear_extra_refs();
944 -
945 - /* EOF */
946 - packet_flush(1);
941 }
942 if (advertise_refs)
943 return 0;