Code

Disallow '\' in ref names
[git.git] / refs.c
diff --git a/refs.c b/refs.c
index 82afb8768473f605bdbce17698b219484890f4f9..fc33bc6ac4b4abcffd814afcd6ee76d4cc73c28c 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -682,12 +682,13 @@ int for_each_rawref(each_ref_fn fn, void *cb_data)
  * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
  * - it ends with a "/".
  * - it ends with ".lock"
+ * - it contains a "\" (backslash)
  */
 
 static inline int bad_ref_char(int ch)
 {
        if (((unsigned) ch) <= ' ' ||
-           ch == '~' || ch == '^' || ch == ':')
+           ch == '~' || ch == '^' || ch == ':' || ch == '\\')
                return 1;
        /* 2.13 Pattern Matching Notation */
        if (ch == '?' || ch == '[') /* Unsupported */
@@ -1681,7 +1682,7 @@ static void gen_scanf_fmt(char *scanf_fmt, const char *rule)
        return;
 }
 
-char *shorten_unambiguous_ref(const char *ref)
+char *shorten_unambiguous_ref(const char *ref, int strict)
 {
        int i;
        static char **scanf_fmts;
@@ -1718,6 +1719,7 @@ char *shorten_unambiguous_ref(const char *ref)
        /* skip first rule, it will always match */
        for (i = nr_rules - 1; i > 0 ; --i) {
                int j;
+               int rules_to_fail = i;
                int short_name_len;
 
                if (1 != sscanf(ref, scanf_fmts[i], short_name))
@@ -1725,15 +1727,26 @@ char *shorten_unambiguous_ref(const char *ref)
 
                short_name_len = strlen(short_name);
 
+               /*
+                * in strict mode, all (except the matched one) rules
+                * must fail to resolve to a valid non-ambiguous ref
+                */
+               if (strict)
+                       rules_to_fail = nr_rules;
+
                /*
                 * check if the short name resolves to a valid ref,
                 * but use only rules prior to the matched one
                 */
-               for (j = 0; j < i; j++) {
+               for (j = 0; j < rules_to_fail; j++) {
                        const char *rule = ref_rev_parse_rules[j];
                        unsigned char short_objectname[20];
                        char refname[PATH_MAX];
 
+                       /* skip matched rule */
+                       if (i == j)
+                               continue;
+
                        /*
                         * the short name is ambiguous, if it resolves
                         * (with this previous rule) to a valid ref
@@ -1749,7 +1762,7 @@ char *shorten_unambiguous_ref(const char *ref)
                 * short name is non-ambiguous if all previous rules
                 * haven't resolved to a valid ref
                 */
-               if (j == i)
+               if (j == rules_to_fail)
                        return short_name;
        }