@matheusbsbs / matheus-bagliano / commits / 605b4978a1
Fork 0

refactor fetch's ref matching to use refname_match()

The old rules used by fetch were coded as a series of ifs. The old rules are: 1) match full refname if it starts with "refs/" or matches "HEAD" 2) verify that full refname starts with "refs/" 3) match abbreviated name in "refs/" if it starts with "heads/", "tags/", or "remotes/". 4) match abbreviated name in "refs/heads/" This is replaced by the new rules a) match full refname b) match abbreviated name prefixed with "refs/" c) match abbreviated name prefixed with "refs/heads/" The details of the new rules are different from the old rules. We no longer verify that the full refname starts with "refs/". The new rule (a) matches any full string. The old rules (1) and (2) were stricter. Now, the caller is responsible for using sensible full refnames. This should be the case for the current code. The new rule (b) is less strict than old rule (3). The new rule accepts abbreviated names that start with a non-standard prefix below "refs/". Despite this modifications the new rules should handle all cases as expected. Two tests are added to verify that fetch does not resolve short tags or HEAD in remotes. We may even think about loosening the rules a bit more and unify them with the rev-parse rules. This would be done by replacing ref_ref_fetch_rules with ref_ref_parse_rules. Note, the two new test would break. Signed-off-by: Steffen Prohaska <prohaska@zib.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Steffen Prohaska committed Nov 11, 2007 at 15:01 UTC 605b4978a105e2f40a353513f616be7d20f91c15
4 files changed +35 -21
cache.h
+1
@@ -417,6 +417,7 @@ extern int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);
417
418 extern int refname_match(const char *abbrev_name, const char *full_name, const char **rules);
419 extern const char *ref_rev_parse_rules[];
420 +extern const char *ref_fetch_rules[];
421
422 extern int create_symref(const char *ref, const char *refs_heads_master, const char *logmsg);
423 extern int validate_headref(const char *ref);
refs.c
+7
@@ -653,6 +653,13 @@ const char *ref_rev_parse_rules[] = {
653 NULL
654 };
655
656 +const char *ref_fetch_rules[] = {
657 + "%.*s",
658 + "refs/%.*s",
659 + "refs/heads/%.*s",
660 + NULL
661 +};
662 +
663 int refname_match(const char *abbrev_name, const char *full_name, const char **rules)
664 {
665 const char **p;
remote.c
+2 -21
@@ -417,25 +417,6 @@ int remote_has_url(struct remote *remote, const char *url)
417 return 0;
418 }
419
420 -/*
421 - * Returns true if, under the matching rules for fetching, name is the
422 - * same as the given full name.
423 - */
424 -static int ref_matches_abbrev(const char *name, const char *full)
425 -{
426 - if (!prefixcmp(name, "refs/") || !strcmp(name, "HEAD"))
427 - return !strcmp(name, full);
428 - if (prefixcmp(full, "refs/"))
429 - return 0;
430 - if (!prefixcmp(name, "heads/") ||
431 - !prefixcmp(name, "tags/") ||
432 - !prefixcmp(name, "remotes/"))
433 - return !strcmp(name, full + 5);
434 - if (prefixcmp(full + 5, "heads/"))
435 - return 0;
436 - return !strcmp(full + 11, name);
437 -}
438 -
420 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
421 {
422 int find_src = refspec->src == NULL;
@@ -804,7 +785,7 @@ int branch_merge_matches(struct branch *branch,
785 {
786 if (!branch || i < 0 || i >= branch->merge_nr)
787 return 0;
807 - return ref_matches_abbrev(branch->merge[i]->src, refname);
788 + return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
789 }
790
791 static struct ref *get_expanded_map(struct ref *remote_refs,
@@ -843,7 +824,7 @@ 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) {
846 - if (ref_matches_abbrev(name, ref->name))
827 + if (refname_match(name, ref->name, ref_fetch_rules))
828 return ref;
829 }
830 return NULL;
t/t5510-fetch.sh
+25
@@ -95,6 +95,31 @@ test_expect_success 'fetch following tags' '
95
96 '
97
98 +test_expect_failure 'fetch must not resolve short tag name' '
99 +
100 + cd "$D" &&
101 +
102 + mkdir five &&
103 + cd five &&
104 + git init &&
105 +
106 + git fetch .. anno:five
107 +
108 +'
109 +
110 +test_expect_failure 'fetch must not resolve short remote name' '
111 +
112 + cd "$D" &&
113 + git-update-ref refs/remotes/six/HEAD HEAD
114 +
115 + mkdir six &&
116 + cd six &&
117 + git init &&
118 +
119 + git fetch .. six:six
120 +
121 +'
122 +
123 test_expect_success 'create bundle 1' '
124 cd "$D" &&
125 echo >file updated again by origin &&