replace: factor object resolution out of replace_object

As we add new options that operate on objects before replacing them, we'll want to be able to feed raw sha1s straight into replace_object. Split replace_object into the object-resolution part and the actual replacement. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 26, 2014 at 22:00 UTC 479bd75751d81b6f3ffc29fcece56f9a200493a1
1 file changed +18 -8
builtin/replace.c
+18 -8
@@ -123,19 +123,17 @@ static int delete_replace_ref(const char *name, const char *ref,
123 return 0;
124 }
125
126 -static int replace_object(const char *object_ref, const char *replace_ref,
127 - int force)
126 +static int replace_object_sha1(const char *object_ref,
127 + unsigned char object[20],
128 + const char *replace_ref,
129 + unsigned char repl[20],
130 + int force)
131 {
129 - unsigned char object[20], prev[20], repl[20];
132 + unsigned char prev[20];
133 enum object_type obj_type, repl_type;
134 char ref[PATH_MAX];
135 struct ref_lock *lock;
136
134 - if (get_sha1(object_ref, object))
135 - die("Failed to resolve '%s' as a valid ref.", object_ref);
136 - if (get_sha1(replace_ref, repl))
137 - die("Failed to resolve '%s' as a valid ref.", replace_ref);
138 -
137 if (snprintf(ref, sizeof(ref),
138 "refs/replace/%s",
139 sha1_to_hex(object)) > sizeof(ref) - 1)
@@ -166,6 +164,18 @@ static int replace_object(const char *object_ref, const char *replace_ref,
164 return 0;
165 }
166
167 +static int replace_object(const char *object_ref, const char *replace_ref, int force)
168 +{
169 + unsigned char object[20], repl[20];
170 +
171 + if (get_sha1(object_ref, object))
172 + die("Failed to resolve '%s' as a valid ref.", object_ref);
173 + if (get_sha1(replace_ref, repl))
174 + die("Failed to resolve '%s' as a valid ref.", replace_ref);
175 +
176 + return replace_object_sha1(object_ref, object, replace_ref, repl, force);
177 +}
178 +
179 int cmd_replace(int argc, const char **argv, const char *prefix)
180 {
181 int force = 0;