@matheusbsbs / matheus-bagliano / commits / d71ab17470
Fork 0

Add matching and parsing for fetch-side refspec rules

Also exports parse_ref_spec(). Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Daniel Barkalow committed Sep 10, 2007 at 23:03 UTC d71ab17470ab2011c7cb398b75385bb0d0c4bdb4
2 files changed +148 -1
remote.c
+124 -1
@@ -208,6 +208,7 @@ static void read_branches_file(struct remote *remote)
208 }
209 add_uri(remote, p);
210 add_fetch_refspec(remote, branch);
211 + remote->fetch_tags = 1; /* always auto-follow */
212 }
213
214 static int handle_config(const char *key, const char *value)
@@ -274,6 +275,9 @@ static int handle_config(const char *key, const char *value)
275 remote->uploadpack = xstrdup(value);
276 else
277 error("more than one uploadpack given, using the first");
278 + } else if (!strcmp(subkey, ".tagopt")) {
279 + if (!strcmp(value, "--no-tags"))
280 + remote->fetch_tags = -1;
281 }
282 return 0;
283 }
@@ -296,7 +300,7 @@ static void read_config(void)
300 git_config(handle_config);
301 }
302
299 -static struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
303 +struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
304 {
305 int i;
306 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
@@ -352,6 +356,10 @@ struct remote *remote_get(const char *name)
356 add_uri(ret, name);
357 if (!ret->uri)
358 return NULL;
359 + if (!strcmp(name, ".")) {
360 + // we always fetch "refs/*:refs/*", which is trivial
361 + add_fetch_refspec(ret, "refs/*:refs/*");
362 + }
363 ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
364 ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
365 return ret;
@@ -454,6 +462,14 @@ struct ref *alloc_ref(unsigned namelen)
462 return ret;
463 }
464
465 +static struct ref *copy_ref(struct ref *ref)
466 +{
467 + struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
468 + memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
469 + ret->next = NULL;
470 + return ret;
471 +}
472 +
473 void free_refs(struct ref *ref)
474 {
475 struct ref *next;
@@ -771,3 +787,110 @@ int branch_merges(struct branch *branch, const char *refname)
787 }
788 return 0;
789 }
790 +
791 +static struct ref *get_expanded_map(struct ref *remote_refs,
792 + const struct refspec *refspec)
793 +{
794 + struct ref *ref;
795 + struct ref *ret = NULL;
796 + struct ref **tail = &ret;
797 +
798 + int remote_prefix_len = strlen(refspec->src);
799 + int local_prefix_len = strlen(refspec->dst);
800 +
801 + for (ref = remote_refs; ref; ref = ref->next) {
802 + if (strchr(ref->name, '^'))
803 + continue; /* a dereference item */
804 + if (!prefixcmp(ref->name, refspec->src)) {
805 + char *match;
806 + struct ref *cpy = copy_ref(ref);
807 + match = ref->name + remote_prefix_len;
808 +
809 + cpy->peer_ref = alloc_ref(local_prefix_len +
810 + strlen(match) + 1);
811 + sprintf(cpy->peer_ref->name, "%s%s",
812 + refspec->dst, match);
813 + if (refspec->force)
814 + cpy->peer_ref->force = 1;
815 + *tail = cpy;
816 + tail = &cpy->next;
817 + }
818 + }
819 +
820 + return ret;
821 +}
822 +
823 +static struct ref *find_ref_by_name_abbrev(struct ref *refs, const char *name)
824 +{
825 + struct ref *ref;
826 + for (ref = refs; ref; ref = ref->next) {
827 + if (ref_matches_abbrev(name, ref->name))
828 + return ref;
829 + }
830 + return NULL;
831 +}
832 +
833 +struct ref *get_remote_ref(struct ref *remote_refs, const char *name)
834 +{
835 + struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
836 +
837 + if (!ref)
838 + die("Couldn't find remote ref %s\n", name);
839 +
840 + return copy_ref(ref);
841 +}
842 +
843 +static struct ref *get_local_ref(const char *name)
844 +{
845 + struct ref *ret;
846 + if (!name)
847 + return NULL;
848 +
849 + if (!prefixcmp(name, "refs/")) {
850 + ret = alloc_ref(strlen(name) + 1);
851 + strcpy(ret->name, name);
852 + return ret;
853 + }
854 +
855 + if (!prefixcmp(name, "heads/") ||
856 + !prefixcmp(name, "tags/") ||
857 + !prefixcmp(name, "remotes/")) {
858 + ret = alloc_ref(strlen(name) + 6);
859 + sprintf(ret->name, "refs/%s", name);
860 + return ret;
861 + }
862 +
863 + ret = alloc_ref(strlen(name) + 12);
864 + sprintf(ret->name, "refs/heads/%s", name);
865 + return ret;
866 +}
867 +
868 +int get_fetch_map(struct ref *remote_refs,
869 + const struct refspec *refspec,
870 + struct ref ***tail)
871 +{
872 + struct ref *ref_map, *rm;
873 +
874 + if (refspec->pattern) {
875 + ref_map = get_expanded_map(remote_refs, refspec);
876 + } else {
877 + ref_map = get_remote_ref(remote_refs,
878 + refspec->src[0] ?
879 + refspec->src : "HEAD");
880 +
881 + ref_map->peer_ref = get_local_ref(refspec->dst);
882 +
883 + if (refspec->force)
884 + ref_map->peer_ref->force = 1;
885 + }
886 +
887 + for (rm = ref_map; rm; rm = rm->next) {
888 + if (rm->peer_ref && check_ref_format(rm->peer_ref->name + 5))
889 + die("* refusing to create funny ref '%s' locally",
890 + rm->peer_ref->name);
891 + }
892 +
893 + tail_link_ref(ref_map, tail);
894 +
895 + return 0;
896 +}
remote.h
+24
@@ -15,6 +15,14 @@ struct remote {
15 struct refspec *fetch;
16 int fetch_refspec_nr;
17
18 + /*
19 + * -1 to never fetch tags
20 + * 0 to auto-follow tags on heuristic (default)
21 + * 1 to always auto-follow tags
22 + * 2 to always fetch tags
23 + */
24 + int fetch_tags;
25 +
26 const char *receivepack;
27 const char *uploadpack;
28 };
@@ -41,9 +49,25 @@ struct ref *alloc_ref(unsigned namelen);
49 */
50 void free_refs(struct ref *ref);
51
52 +struct refspec *parse_ref_spec(int nr_refspec, const char **refspec);
53 +
54 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
55 int nr_refspec, char **refspec, int all);
56
57 +/*
58 + * Given a list of the remote refs and the specification of things to
59 + * fetch, makes a (separate) list of the refs to fetch and the local
60 + * refs to store into.
61 + *
62 + * *tail is the pointer to the tail pointer of the list of results
63 + * beforehand, and will be set to the tail pointer of the list of
64 + * results afterward.
65 + */
66 +int get_fetch_map(struct ref *remote_refs, const struct refspec *refspec,
67 + struct ref ***tail);
68 +
69 +struct ref *get_remote_ref(struct ref *remote_refs, const char *name);
70 +
71 /*
72 * For the given remote, reads the refspec's src and sets the other fields.
73 */