replace.c: use the ref transaction functions for updates

Update replace.c to use ref transactions for updates. Signed-off-by: Ronnie Sahlberg <sahlberg@google.com> Reviewed-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ronnie Sahlberg committed Apr 16, 2014 at 15:32 UTC 867c2fac0aef75d8c883010780eb163e82aaa266
1 file changed +8 -6
builtin/replace.c
+8 -6
@@ -153,7 +153,8 @@ static int replace_object_sha1(const char *object_ref,
153 unsigned char prev[20];
154 enum object_type obj_type, repl_type;
155 char ref[PATH_MAX];
156 - struct ref_lock *lock;
156 + struct ref_transaction *transaction;
157 + struct strbuf err = STRBUF_INIT;
158
159 obj_type = sha1_object_info(object, NULL);
160 repl_type = sha1_object_info(repl, NULL);
@@ -166,12 +167,13 @@ static int replace_object_sha1(const char *object_ref,
167
168 check_ref_valid(object, prev, ref, sizeof(ref), force);
169
169 - lock = lock_any_ref_for_update(ref, prev, 0, NULL);
170 - if (!lock)
171 - die("%s: cannot lock the ref", ref);
172 - if (write_ref_sha1(lock, repl, NULL) < 0)
173 - die("%s: cannot update the ref", ref);
170 + transaction = ref_transaction_begin(&err);
171 + if (!transaction ||
172 + ref_transaction_update(transaction, ref, repl, prev, 0, 1, &err) ||
173 + ref_transaction_commit(transaction, NULL, &err))
174 + die("%s", err.buf);
175
176 + ref_transaction_free(transaction);
177 return 0;
178 }
179