replace: refactor checking ref validity
This will be useful in a following commit when we will want to check if the ref already exists before we let the user edit an object. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Christian Couder committed
May 17, 2014 at 14:16 UTC
b6e38840921ac4a0fe07e10e632f66736745da10
1 file changed
+20
-11
builtin/replace.c
+20
-11
@@ -124,6 +124,25 @@ static int delete_replace_ref(const char *name, const char *ref,
124
return 0;
125
}
126
127
+static void check_ref_valid(unsigned char object[20],
128
+ unsigned char prev[20],
129
+ char *ref,
130
+ int ref_size,
131
+ int force)
132
+{
133
+ if (snprintf(ref, ref_size,
134
+ "refs/replace/%s",
135
+ sha1_to_hex(object)) > ref_size - 1)
136
+ die("replace ref name too long: %.*s...", 50, ref);
137
+ if (check_refname_format(ref, 0))
138
+ die("'%s' is not a valid ref name.", ref);
139
+
140
+ if (read_ref(ref, prev))
141
+ hashclr(prev);
142
+ else if (!force)
143
+ die("replace ref '%s' already exists", ref);
144
+}
145
+
146
static int replace_object_sha1(const char *object_ref,
147
unsigned char object[20],
148
const char *replace_ref,
@@ -135,13 +154,6 @@ static int replace_object_sha1(const char *object_ref,
154
char ref[PATH_MAX];
155
struct ref_lock *lock;
156
138
- if (snprintf(ref, sizeof(ref),
139
- "refs/replace/%s",
140
- sha1_to_hex(object)) > sizeof(ref) - 1)
141
- die("replace ref name too long: %.*s...", 50, ref);
142
- if (check_refname_format(ref, 0))
143
- die("'%s' is not a valid ref name.", ref);
144
-
157
obj_type = sha1_object_info(object, NULL);
158
repl_type = sha1_object_info(repl, NULL);
159
if (!force && obj_type != repl_type)
@@ -151,10 +163,7 @@ static int replace_object_sha1(const char *object_ref,
163
object_ref, typename(obj_type),
164
replace_ref, typename(repl_type));
165
154
- if (read_ref(ref, prev))
155
- hashclr(prev);
156
- else if (!force)
157
- die("replace ref '%s' already exists", ref);
166
+ check_ref_valid(object, prev, ref, sizeof(ref), force);
167
168
lock = lock_any_ref_for_update(ref, prev, 0, NULL);
169
if (!lock)