lock_any_ref_for_update(): reject wildcard return from check_ref_format
Recent check_ref_format() returns -3 as well as -1 (general error) and -2 (less than two levels). The caller was explicitly checking for -1, to allow "HEAD" but still needed to disallow bogus refs. This introduces symbolic constants for the return values from check_ref_format() to make them read better and more meaningful. Normal ref creation codepath can still treat non-zero return values as errors. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Jan 1, 2008 at 22:33 UTC
5f7b202a7fa8ffbf076c456106750b2bb7732ba4
2 files changed
+22
-10
refs.c
+18
-9
@@ -613,32 +613,37 @@ int check_ref_format(const char *ref)
613
while ((ch = *cp++) == '/')
614
; /* tolerate duplicated slashes */
615
if (!ch)
616
- return -1; /* should not end with slashes */
616
+ /* should not end with slashes */
617
+ return CHECK_REF_FORMAT_ERROR;
618
619
/* we are at the beginning of the path component */
620
if (ch == '.')
620
- return -1;
621
+ return CHECK_REF_FORMAT_ERROR;
622
bad_type = bad_ref_char(ch);
623
if (bad_type) {
623
- return (bad_type == 2 && !*cp) ? -3 : -1;
624
+ return (bad_type == 2 && !*cp)
625
+ ? CHECK_REF_FORMAT_WILDCARD
626
+ : CHECK_REF_FORMAT_ERROR;
627
}
628
629
/* scan the rest of the path component */
630
while ((ch = *cp++) != 0) {
631
bad_type = bad_ref_char(ch);
632
if (bad_type) {
630
- return (bad_type == 2 && !*cp) ? -3 : -1;
633
+ return (bad_type == 2 && !*cp)
634
+ ? CHECK_REF_FORMAT_WILDCARD
635
+ : CHECK_REF_FORMAT_ERROR;
636
}
637
if (ch == '/')
638
break;
639
if (ch == '.' && *cp == '.')
635
- return -1;
640
+ return CHECK_REF_FORMAT_ERROR;
641
}
642
level++;
643
if (!ch) {
644
if (level < 2)
640
- return -2; /* at least of form "heads/blah" */
641
- return 0;
645
+ return CHECK_REF_FORMAT_ONELEVEL;
646
+ return CHECK_REF_FORMAT_OK;
647
}
648
}
649
}
@@ -816,9 +821,13 @@ struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
821
822
struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1, int flags)
823
{
819
- if (check_ref_format(ref) == -1)
824
+ switch (check_ref_format(ref)) {
825
+ case CHECK_REF_FORMAT_ERROR:
826
+ case CHECK_REF_FORMAT_WILDCARD:
827
return NULL;
821
- return lock_ref_sha1_basic(ref, old_sha1, flags, NULL);
828
+ default:
829
+ return lock_ref_sha1_basic(ref, old_sha1, flags, NULL);
830
+ }
831
}
832
833
static struct lock_file packlock;
refs.h
+4
-1
@@ -52,7 +52,10 @@ int for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data);
52
*/
53
extern int for_each_reflog(each_ref_fn, void *);
54
55
-/** Returns 0 if target has the right format for a ref. **/
55
+#define CHECK_REF_FORMAT_OK 0
56
+#define CHECK_REF_FORMAT_ERROR (-1)
57
+#define CHECK_REF_FORMAT_ONELEVEL (-2)
58
+#define CHECK_REF_FORMAT_WILDCARD (-3)
59
extern int check_ref_format(const char *target);
60
61
/** rename ref, return 0 on success **/