Skip to content

Commit b6e3884

Browse files
chriscoolgitster
authored andcommitted
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>
1 parent f22166b commit b6e3884

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

builtin/replace.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,25 @@ static int delete_replace_ref(const char *name, const char *ref,
124124
return 0;
125125
}
126126

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+
127146
static int replace_object_sha1(const char *object_ref,
128147
unsigned char object[20],
129148
const char *replace_ref,
@@ -135,13 +154,6 @@ static int replace_object_sha1(const char *object_ref,
135154
char ref[PATH_MAX];
136155
struct ref_lock *lock;
137156

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-
145157
obj_type = sha1_object_info(object, NULL);
146158
repl_type = sha1_object_info(repl, NULL);
147159
if (!force && obj_type != repl_type)
@@ -151,10 +163,7 @@ static int replace_object_sha1(const char *object_ref,
151163
object_ref, typename(obj_type),
152164
replace_ref, typename(repl_type));
153165

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);
158167

159168
lock = lock_any_ref_for_update(ref, prev, 0, NULL);
160169
if (!lock)

0 commit comments

Comments
 (0)