* Locks a ref returning the lock on success and NULL on failure. */
| 1208 | * Locks a ref returning the lock on success and NULL on failure. |
| 1209 | */ |
| 1210 | static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs, |
| 1211 | const char *refname, |
| 1212 | struct strbuf *err) |
| 1213 | { |
| 1214 | struct strbuf ref_file = STRBUF_INIT; |
| 1215 | struct ref_lock *lock; |
| 1216 | struct create_reflock_cb cb_data; |
| 1217 | |
| 1218 | files_assert_main_repository(refs, "lock_ref_oid_basic"); |
| 1219 | assert(err); |
| 1220 | |
| 1221 | CALLOC_ARRAY(lock, 1); |
| 1222 | |
| 1223 | files_ref_path(refs, &ref_file, refname); |
| 1224 | |
| 1225 | /* |
| 1226 | * If the ref did not exist and we are creating it, make sure |
| 1227 | * there is no existing packed ref whose name begins with our |
| 1228 | * refname, nor a packed ref whose name is a proper prefix of |
| 1229 | * our refname. |
| 1230 | */ |
| 1231 | if (is_null_oid(&lock->old_oid) && |
| 1232 | refs_verify_refname_available(refs->packed_ref_store, refname, |
| 1233 | NULL, NULL, 0, err)) |
| 1234 | goto error_return; |
| 1235 | |
| 1236 | lock->ref_name = xstrdup(refname); |
| 1237 | lock->count = 1; |
| 1238 | cb_data.lk = &lock->lk; |
| 1239 | cb_data.repo = refs->base.repo; |
| 1240 | |
| 1241 | if (raceproof_create_file(ref_file.buf, create_reflock, &cb_data)) { |
| 1242 | unable_to_lock_message(ref_file.buf, errno, err); |
| 1243 | goto error_return; |
| 1244 | } |
| 1245 | |
| 1246 | if (!refs_resolve_ref_unsafe(&refs->base, lock->ref_name, 0, |
| 1247 | &lock->old_oid, NULL)) |
| 1248 | oidclr(&lock->old_oid, refs->base.repo->hash_algo); |
| 1249 | goto out; |
| 1250 | |
| 1251 | error_return: |
| 1252 | unlock_ref(lock); |
| 1253 | lock = NULL; |
| 1254 | |
| 1255 | out: |
| 1256 | strbuf_release(&ref_file); |
| 1257 | return lock; |
| 1258 | } |
| 1259 | |
| 1260 | struct ref_to_prune { |
| 1261 | struct ref_to_prune *next; |
no test coverage detected