@matheusbsbs / matheus-bagliano / commits / 79803322c1
Fork 0

add refname_match()

We use at least two rulesets for matching abbreviated refnames with full refnames (starting with 'refs/'). git-rev-parse and git-fetch use slightly different rules. This commit introduces a new function refname_match (const char *abbrev_name, const char *full_name, const char **rules). abbrev_name is expanded using the rules and matched against full_name. If a match is found the function returns true. rules is a NULL-terminate list of format patterns with "%.*s", for example: const char *ref_rev_parse_rules[] = { "%.*s", "refs/%.*s", "refs/tags/%.*s", "refs/heads/%.*s", "refs/remotes/%.*s", "refs/remotes/%.*s/HEAD", NULL }; Asterisks are included in the format strings because this is the form required in sha1_name.c. Sharing the list with the functions there is a good idea to avoid duplicating the rules. Hopefully this facilitates unified matching rules in the future. This commit makes the rules used by rev-parse for resolving refs to sha1s available for string comparison. Before this change, the rules were buried in get_sha1*() and dwim_ref(). A follow-up commit will refactor the rules used by fetch. refname_match() will be used for matching refspecs in git-send-pack. Thanks to Daniel Barkalow <barkalow@iabervon.org> for pointing out that ref_matches_abbrev in remote.c solves a similar problem and care should be taken to avoid confusion. 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 79803322c1d8d2f74e1a53d44f363d878180e0f5
3 files changed +29 -12
cache.h
+3
@@ -415,6 +415,9 @@ extern const char *resolve_ref(const char *path, unsigned char *sha1, int, int *
415 extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
416 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 +
421 extern int create_symref(const char *ref, const char *refs_heads_master, const char *logmsg);
422 extern int validate_headref(const char *ref);
423
refs.c
+24
@@ -643,6 +643,30 @@ int check_ref_format(const char *ref)
643 }
644 }
645
646 +const char *ref_rev_parse_rules[] = {
647 + "%.*s",
648 + "refs/%.*s",
649 + "refs/tags/%.*s",
650 + "refs/heads/%.*s",
651 + "refs/remotes/%.*s",
652 + "refs/remotes/%.*s/HEAD",
653 + NULL
654 +};
655 +
656 +int refname_match(const char *abbrev_name, const char *full_name, const char **rules)
657 +{
658 + const char **p;
659 + const int abbrev_name_len = strlen(abbrev_name);
660 +
661 + for (p = rules; *p; p++) {
662 + if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) {
663 + return 1;
664 + }
665 + }
666 +
667 + return 0;
668 +}
669 +
670 static struct ref_lock *verify_lock(struct ref_lock *lock,
671 const unsigned char *old_sha1, int mustexist)
672 {
sha1_name.c
+2 -12
@@ -239,23 +239,13 @@ static int ambiguous_path(const char *path, int len)
239 return slash;
240 }
241
242 -static const char *ref_fmt[] = {
243 - "%.*s",
244 - "refs/%.*s",
245 - "refs/tags/%.*s",
246 - "refs/heads/%.*s",
247 - "refs/remotes/%.*s",
248 - "refs/remotes/%.*s/HEAD",
249 - NULL
250 -};
251 -
242 int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
243 {
244 const char **p, *r;
245 int refs_found = 0;
246
247 *ref = NULL;
258 - for (p = ref_fmt; *p; p++) {
248 + for (p = ref_rev_parse_rules; *p; p++) {
249 unsigned char sha1_from_ref[20];
250 unsigned char *this_result;
251
@@ -277,7 +267,7 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
267 int logs_found = 0;
268
269 *log = NULL;
280 - for (p = ref_fmt; *p; p++) {
270 + for (p = ref_rev_parse_rules; *p; p++) {
271 struct stat st;
272 unsigned char hash[20];
273 char path[PATH_MAX];