Commit 4e6e2ff26f5a04a4628aa0d81e5d5d73acf28ec4

Vicent Marti 2013-05-30T03:47:10

...Aaaand this works

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
diff --git a/include/git2/refs.h b/include/git2/refs.h
index 44b658d..cec8306 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -257,11 +257,6 @@ GIT_EXTERN(int) git_reference_set_target(
  * The new name will be checked for validity.
  * See `git_reference_create_symbolic()` for rules about valid names.
  *
- * On success, the given git_reference will be deleted from disk and a
- * new `git_reference` will be returned.
- *
- * The reference will be immediately renamed in-memory and on disk.
- *
  * If the `force` flag is not enabled, and there's already
  * a reference with the given name, the renaming will fail.
  *
@@ -277,7 +272,7 @@ GIT_EXTERN(int) git_reference_set_target(
  *
  */
 GIT_EXTERN(int) git_reference_rename(
-	git_reference **out,
+	git_reference **new_ref,
 	git_reference *ref,
 	const char *new_name,
 	int force);
diff --git a/include/git2/sys/refdb_backend.h b/include/git2/sys/refdb_backend.h
index a78d226..9b457b0 100644
--- a/include/git2/sys/refdb_backend.h
+++ b/include/git2/sys/refdb_backend.h
@@ -92,13 +92,18 @@ struct git_refdb_backend {
 	 * Writes the given reference to the refdb.  A refdb implementation
 	 * must provide this function.
 	 */
-	int (*write)(git_refdb_backend *backend, const git_reference *ref);
+	int (*write)(git_refdb_backend *backend,
+		const git_reference *ref, int force);
+
+	int (*rename)(
+		git_reference **out, git_refdb_backend *backend,
+		const char *old_name, const char *new_name, int force);
 
 	/**
 	 * Deletes the given reference from the refdb.  A refdb implementation
 	 * must provide this function.
 	 */
-	int (*delete)(git_refdb_backend *backend, const git_reference *ref);
+	int (*delete)(git_refdb_backend *backend, const char *ref_name);
 
 	/**
 	 * Suggests that the given refdb compress or optimize its references.
diff --git a/src/branch.c b/src/branch.c
index 84efada..de38e33 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -182,18 +182,21 @@ int git_branch_move(
 	if (!git_reference_is_branch(branch))
 		return not_a_local_branch(git_reference_name(branch));
 
-	if ((error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0 ||
-		(error = git_buf_printf(&old_config_section, "branch.%s", git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR))) < 0 ||
-		(error = git_buf_printf(&new_config_section, "branch.%s", new_branch_name)) < 0)
+	error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name);
+	if (error < 0)
 		goto done;
 
+	git_buf_printf(&old_config_section,
+		"branch.%s", git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR));
+
+	git_buf_printf(&new_config_section, "branch.%s", new_branch_name);
+
 	if ((error = git_config_rename_section(git_reference_owner(branch),
 		git_buf_cstr(&old_config_section),
 		git_buf_cstr(&new_config_section))) < 0)
 		goto done;
 
-	if ((error = git_reference_rename(out, branch, git_buf_cstr(&new_reference_name), force)) < 0)
-		goto done;
+	error = git_reference_rename(out, branch, git_buf_cstr(&new_reference_name), force);
 
 done:
 	git_buf_free(&new_reference_name);
diff --git a/src/refdb.c b/src/refdb.c
index e0701d3..d8daa77 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -165,14 +165,40 @@ void git_refdb_iterator_free(git_reference_iterator *iter)
 	iter->free(iter);
 }
 
-int git_refdb_write(git_refdb *db, const git_reference *ref)
+int git_refdb_write(git_refdb *db, git_reference *ref, int force)
 {
 	assert(db && db->backend);
-	return db->backend->write(db->backend, ref);
+
+	GIT_REFCOUNT_INC(db);
+	ref->db = db;
+
+	return db->backend->write(db->backend, ref, force);
+}
+
+int git_refdb_rename(
+	git_reference **out,
+	git_refdb *db,
+	const char *old_name,
+	const char *new_name,
+	int force)
+{
+	int error;
+
+	assert(db && db->backend);
+	error = db->backend->rename(out, db->backend, old_name, new_name, force);
+	if (error < 0)
+		return error;
+
+	if (out) {
+		GIT_REFCOUNT_INC(db);
+		(*out)->db = db;
+	}
+
+	return 0;
 }
 
-int git_refdb_delete(struct git_refdb *db, const git_reference *ref)
+int git_refdb_delete(struct git_refdb *db, const char *ref_name)
 {
 	assert(db && db->backend);
-	return db->backend->delete(db->backend, ref);
+	return db->backend->delete(db->backend, ref_name);
 }
diff --git a/src/refdb.h b/src/refdb.h
index 1dcd70d..62068db 100644
--- a/src/refdb.h
+++ b/src/refdb.h
@@ -26,12 +26,19 @@ int git_refdb_lookup(
 	git_refdb *refdb,
 	const char *ref_name);
 
+int git_refdb_rename(
+	git_reference **out,
+	git_refdb *db,
+	const char *old_name,
+	const char *new_name,
+	int force);
+
 int git_refdb_iterator(git_reference_iterator **out, git_refdb *db, const char *glob);
 int git_refdb_iterator_next(git_reference **out, git_reference_iterator *iter);
 int git_refdb_iterator_next_name(const char **out, git_reference_iterator *iter);
 void git_refdb_iterator_free(git_reference_iterator *iter);
 
-int git_refdb_write(git_refdb *refdb, const git_reference *ref);
-int git_refdb_delete(git_refdb *refdb, const git_reference *ref);
+int git_refdb_write(git_refdb *refdb, git_reference *ref, int force);
+int git_refdb_delete(git_refdb *refdb, const char *ref_name);
 
 #endif
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index ecd033d..c40c524 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -116,11 +116,8 @@ static int packed_parse_oid(
 
 	git_oid_cpy(&ref->oid, &id);
 
-	ref->flags = 0;
-
 	*ref_out = ref;
 	*buffer_out = refname_end + 1;
-
 	return 0;
 
 corrupt:
@@ -735,6 +732,58 @@ static int refdb_fs_backend__iterator(
 	return 0;
 }
 
+static bool ref_is_available(
+	const char *old_ref, const char *new_ref, const char *this_ref)
+{
+	if (old_ref == NULL || strcmp(old_ref, this_ref)) {
+		size_t reflen = strlen(this_ref);
+		size_t newlen = strlen(new_ref);
+		size_t cmplen = reflen < newlen ? reflen : newlen;
+		const char *lead = reflen < newlen ? new_ref : this_ref;
+
+		if (!strncmp(new_ref, this_ref, cmplen) && lead[cmplen] == '/') {
+			return false;
+		}
+	}
+
+	return true;
+}
+
+static int reference_path_available(
+	refdb_fs_backend *backend,
+	const char *new_ref,
+	const char* old_ref,
+	int force)
+{
+	struct packref *this_ref;
+
+	if (packed_load(backend) < 0)
+		return -1;
+
+	if (!force) {
+		int exists;
+
+		if (refdb_fs_backend__exists(&exists, (git_refdb_backend *)backend, new_ref) < 0)
+			return -1;
+
+		if (exists) {
+			giterr_set(GITERR_REFERENCE,
+				"Failed to write reference '%s': a reference with "
+				" that name already exists.", new_ref);
+			return GIT_EEXISTS;
+		}
+	}
+
+	git_strmap_foreach_value(backend->refcache.packfile, this_ref, {
+		if (!ref_is_available(old_ref, new_ref, this_ref->name)) {
+			giterr_set(GITERR_REFERENCE,
+				"The path to reference '%s' collides with an existing one", new_ref);
+			return -1;
+		}
+	});
+	
+	return 0;
+}
 
 static int loose_write(refdb_fs_backend *backend, const git_reference *ref)
 {
@@ -744,8 +793,7 @@ static int loose_write(refdb_fs_backend *backend, const git_reference *ref)
 	/* Remove a possibly existing empty directory hierarchy
 	 * which name would collide with the reference name
 	 */
-	if (git_futils_rmdir_r(ref->name, backend->path,
-		GIT_RMDIR_SKIP_NONEMPTY) < 0)
+	if (git_futils_rmdir_r(ref->name, backend->path, GIT_RMDIR_SKIP_NONEMPTY) < 0)
 		return -1;
 
 	if (git_buf_joinpath(&ref_path, backend->path, ref->name) < 0)
@@ -1005,37 +1053,40 @@ cleanup_memory:
 
 static int refdb_fs_backend__write(
 	git_refdb_backend *_backend,
-	const git_reference *ref)
+	const git_reference *ref,
+	int force)
 {
 	refdb_fs_backend *backend;
+	int error;
 
 	assert(_backend);
 	backend = (refdb_fs_backend *)_backend;
 
+	error = reference_path_available(backend, ref->name, NULL, force);
+	if (error < 0)
+		return error;
+
 	return loose_write(backend, ref);
 }
 
 static int refdb_fs_backend__delete(
 	git_refdb_backend *_backend,
-	const git_reference *ref)
+	const char *ref_name)
 {
 	refdb_fs_backend *backend;
-	git_repository *repo;
 	git_buf loose_path = GIT_BUF_INIT;
 	struct packref *pack_ref;
 	khiter_t pack_ref_pos;
-	int error = 0, pack_error;
+	int error = 0;
 	bool loose_deleted = 0;
 
 	assert(_backend);
-	assert(ref);
+	assert(ref_name);
 
 	backend = (refdb_fs_backend *)_backend;
-	repo = backend->repo;
 
 	/* If a loose reference exists, remove it from the filesystem */
-
-	if (git_buf_joinpath(&loose_path, repo->path_repository, ref->name) < 0)
+	if (git_buf_joinpath(&loose_path, backend->path, ref_name) < 0)
 		return -1;
 
 	if (git_path_isfile(loose_path.ptr)) {
@@ -1049,22 +1100,66 @@ static int refdb_fs_backend__delete(
 		return error;
 
 	/* If a packed reference exists, remove it from the packfile and repack */
+	error = packed_map_entry(&pack_ref, &pack_ref_pos, backend, ref_name);
+
+	if (error == GIT_ENOTFOUND)
+		return loose_deleted ? 0 : GIT_ENOTFOUND;
 
-	if ((pack_error = packed_map_entry(&pack_ref, &pack_ref_pos, backend, ref->name)) == 0) {
+	if (error == 0) {
 		git_strmap_delete_at(backend->refcache.packfile, pack_ref_pos);
 		git__free(pack_ref);
-
 		error = packed_write(backend);
 	}
 
-	if (pack_error == GIT_ENOTFOUND)
-		error = loose_deleted ? 0 : GIT_ENOTFOUND;
-	else
-		error = pack_error;
-
 	return error;
 }
 
+static int refdb_fs_backend__rename(
+	git_reference **out,
+	git_refdb_backend *_backend,
+	const char *old_name,
+	const char *new_name,
+	int force)
+{
+	refdb_fs_backend *backend;
+	git_reference *old, *new;
+	int error;
+
+	assert(_backend);
+	backend = (refdb_fs_backend *)_backend;
+
+	error = reference_path_available(backend, new_name, old_name, force);
+	if (error < 0)
+		return error;
+
+	error = refdb_fs_backend__lookup(&old, _backend, old_name);
+	if (error < 0)
+		return error;
+
+	error = refdb_fs_backend__delete(_backend, old_name);
+	if (error < 0) {
+		git_reference_free(old);
+		return error;
+	}
+
+	new = realloc(old, sizeof(git_reference) + strlen(new_name) + 1);
+	memcpy(new->name, new_name, strlen(new_name) + 1);
+
+	error = loose_write(backend, new);
+	if (error < 0) {
+		git_reference_free(new);
+		return error;
+	}
+
+	if (out) {
+		*out = new;
+	} else {
+		git_reference_free(new);
+	}
+
+	return 0;
+}
+
 static int refdb_fs_backend__compress(git_refdb_backend *_backend)
 {
 	refdb_fs_backend *backend;
@@ -1172,6 +1267,7 @@ int git_refdb_backend_fs(
 	backend->parent.iterator = &refdb_fs_backend__iterator;
 	backend->parent.write = &refdb_fs_backend__write;
 	backend->parent.delete = &refdb_fs_backend__delete;
+	backend->parent.rename = &refdb_fs_backend__rename;
 	backend->parent.compress = &refdb_fs_backend__compress;
 	backend->parent.free = &refdb_fs_backend__free;
 
diff --git a/src/refs.c b/src/refs.c
index a8583de..c60e042 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -98,122 +98,9 @@ void git_reference_free(git_reference *reference)
 	git__free(reference);
 }
 
-struct reference_available_t {
-	const char *new_ref;
-	const char *old_ref;
-	int available;
-};
-
-static int _reference_available_cb(const char *refname, void *data)
-{
-	struct reference_available_t *d;
-
-	assert(refname && data);
-	d = (struct reference_available_t *)data;
-
-	if (!d->old_ref || strcmp(d->old_ref, refname)) {
-		size_t reflen = strlen(refname);
-		size_t newlen = strlen(d->new_ref);
-		size_t cmplen = reflen < newlen ? reflen : newlen;
-		const char *lead = reflen < newlen ? d->new_ref : refname;
-
-		if (!strncmp(d->new_ref, refname, cmplen) && lead[cmplen] == '/') {
-			d->available = 0;
-			return -1;
-		}
-	}
-
-	return 0;
-}
-
-/**
- * TODO: this should be part of the FS backend
- */
-static int reference_path_available(
-	git_repository *repo,
-	const char *ref,
-	const char* old_ref)
-{
-	int error;
-	struct reference_available_t data;
-
-	data.new_ref = ref;
-	data.old_ref = old_ref;
-	data.available = 1;
-
-	error = git_reference_foreach_name(repo, _reference_available_cb, (void *)&data);
-	if (error < 0)
-		return error;
-
-	if (!data.available) {
-		giterr_set(GITERR_REFERENCE,
-			"The path to reference '%s' collides with an existing one", ref);
-		return -1;
-	}
-
-	return 0;
-}
-
-/*
- * Check if a reference could be written to disk, based on:
- *
- *	- Whether a reference with the same name already exists,
- *	and we are allowing or disallowing overwrites
- *
- *	- Whether the name of the reference would collide with
- *	an existing path
- */
-static int reference_can_write(
-	git_repository *repo,
-	const char *refname,
-	const char *previous_name,
-	int force)
-{
-	git_refdb *refdb;
-
-	if (git_repository_refdb__weakptr(&refdb, repo) < 0)
-		return -1;
-
-	/* see if the reference shares a path with an existing reference;
-	 * if a path is shared, we cannot create the reference, even when forcing */
-	if (reference_path_available(repo, refname, previous_name) < 0)
-		return -1;
-
-	/* check if the reference actually exists, but only if we are not forcing
-	 * the rename. If we are forcing, it's OK to overwrite */
-	if (!force) {
-		int exists;
-
-		if (git_refdb_exists(&exists, refdb, refname) < 0)
-			return -1;
-
-		/* We cannot proceed if the reference already exists and we're not forcing
-		 * the rename; the existing one would be overwritten */
-		if (exists) {
-			giterr_set(GITERR_REFERENCE,
-				"A reference with that name (%s) already exists", refname);
-			return GIT_EEXISTS;
-		}
-	}
-
-	/* FIXME: if the reference exists and we are forcing, do we really need to
-	 * remove the reference first?
-	 *
-	 * Two cases:
-	 *
-	 *	- the reference already exists and is loose: not a problem, the file
-	 *	gets overwritten on disk
-	 *
-	 *	- the reference already exists and is packed: we write a new one as
-	 *	loose, which by all means renders the packed one useless
-	 */
-
-	return 0;
-}
-
 int git_reference_delete(git_reference *ref)
 {
-	return git_refdb_delete(ref->db, ref);
+	return git_refdb_delete(ref->db, ref->name);
 }
 
 int git_reference_lookup(git_reference **ref_out,
@@ -420,23 +307,24 @@ static int reference__create(
 	if (ref_out)
 		*ref_out = NULL;
 
-	if ((error = git_reference__normalize_name_lax(normalized, sizeof(normalized), name)) < 0 ||
-		(error = reference_can_write(repo, normalized, NULL, force)) < 0 ||
-		(error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
+	error = git_reference__normalize_name_lax(normalized, sizeof(normalized), name);
+	if (error < 0)
+		return error;
+
+	error = git_repository_refdb__weakptr(&refdb, repo);
+	if (error < 0)
 		return error;
 
 	if (oid != NULL) {
 		assert(symbolic == NULL);
-		ref = git_reference__alloc(name, oid, NULL);
+		ref = git_reference__alloc(normalized, oid, NULL);
 	} else {
-		ref = git_reference__alloc_symbolic(name, symbolic);
+		ref = git_reference__alloc_symbolic(normalized, symbolic);
 	}
 
-	/* TODO: this needs to be written more explicitly */
 	GITERR_CHECK_ALLOC(ref);
-	ref->db = refdb;
 
-	if ((error = git_refdb_write(refdb, ref)) < 0) {
+	if ((error = git_refdb_write(refdb, ref, force)) < 0) {
 		git_reference_free(ref);
 		return error;
 	}
@@ -533,77 +421,41 @@ int git_reference_rename(
 	unsigned int normalization_flags;
 	char normalized[GIT_REFNAME_MAX];
 	bool should_head_be_updated = false;
-	git_reference *result = NULL;
 	int error = 0;
 	int reference_has_log;
 
-	*out = NULL;
-
 	normalization_flags = ref->type == GIT_REF_SYMBOLIC ?
 		GIT_REF_FORMAT_ALLOW_ONELEVEL : GIT_REF_FORMAT_NORMAL;
 
 	if ((error = git_reference_normalize_name(
-			normalized, sizeof(normalized), new_name, normalization_flags)) < 0 ||
-		(error = reference_can_write(ref->db->repo, normalized, ref->name, force)) < 0)
+			normalized, sizeof(normalized), new_name, normalization_flags)) < 0)
 		return error;
 
-	/*
-	 * Create the new reference.
-	 */
-	if (ref->type == GIT_REF_OID) {
-		result = git_reference__alloc(new_name, &ref->target.oid, &ref->peel);
-	} else if (ref->type == GIT_REF_SYMBOLIC) {
-		result = git_reference__alloc_symbolic(new_name, ref->target.symbolic);
-	} else {
-		assert(0);
-	}
-
-	if (result == NULL)
-		return -1;
-
-	/* TODO: this is bad */
-	result->db = ref->db;
-
 	/* Check if we have to update HEAD. */
 	if ((error = git_branch_is_head(ref)) < 0)
-		goto on_error;
+		return error;
 
 	should_head_be_updated = (error > 0);
 
-	/* Now delete the old ref and save the new one. */
-	if ((error = git_refdb_delete(ref->db, ref)) < 0)
-		goto on_error;
-
-	/* Save the new reference. */
-	if ((error = git_refdb_write(ref->db, result)) < 0)
-		goto rollback;
+	if ((error = git_refdb_rename(out, ref->db, ref->name, new_name, force)) < 0)
+		return error;
 
 	/* Update HEAD it was poiting to the reference being renamed. */
-	if (should_head_be_updated && (error = git_repository_set_head(ref->db->repo, new_name)) < 0) {
+	if (should_head_be_updated &&
+		(error = git_repository_set_head(ref->db->repo, new_name)) < 0) {
 		giterr_set(GITERR_REFERENCE, "Failed to update HEAD after renaming reference");
-		goto on_error;
+		return error;
 	}
 
 	/* Rename the reflog file, if it exists. */
 	reference_has_log = git_reference_has_log(ref);
-	if (reference_has_log < 0) {
-		error = reference_has_log;
-		goto on_error;
-	}
-	if (reference_has_log && (error = git_reflog_rename(ref, new_name)) < 0)
-		goto on_error;
-
-	*out = result;
-
-	return error;
+	if (reference_has_log < 0)
+		return reference_has_log;
 
-rollback:
-	git_refdb_write(ref->db, ref);
-
-on_error:
-	git_reference_free(result);
+	if (reference_has_log && (error = git_reflog_rename(ref, new_name)) < 0)
+		return error;
 
-	return error;
+	return 0;
 }
 
 int git_reference_resolve(git_reference **ref_out, const git_reference *ref)
diff --git a/src/remote.c b/src/remote.c
index 266a3d9..674c277 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -1245,7 +1245,6 @@ static int rename_one_remote_reference(
 {
 	int error = -1;
 	git_buf new_name = GIT_BUF_INIT;
-	git_reference *newref = NULL;
 
 	if (git_buf_printf(
 		&new_name,
@@ -1254,11 +1253,9 @@ static int rename_one_remote_reference(
 		reference->name + strlen(GIT_REFS_REMOTES_DIR) + strlen(old_remote_name)) < 0)
 			return -1;
 
-	/* TODO: can we make this NULL? */
-	error = git_reference_rename(&newref, reference, git_buf_cstr(&new_name), 0);
+	error = git_reference_rename(NULL, reference, git_buf_cstr(&new_name), 0);
 	git_reference_free(reference);
 
-	git_reference_free(newref);
 	git_buf_free(&new_name);
 	return error;
 }
diff --git a/tests-clar/refdb/testdb.c b/tests-clar/refdb/testdb.c
deleted file mode 100644
index 6509ae9..0000000
--- a/tests-clar/refdb/testdb.c
+++ /dev/null
@@ -1,182 +0,0 @@
-#include "vector.h"
-#include "util.h"
-#include "testdb.h"
-
-typedef struct refdb_test_backend {
-	git_refdb_backend parent;
-
-	git_repository *repo;
-	git_vector refs;
-} refdb_test_backend;
-
-typedef struct refdb_test_entry {
-	char *name;
-	git_ref_t type;
-
-	union {
-		git_oid oid;
-		char *symbolic;
-	} target;
-} refdb_test_entry;
-
-static int ref_name_cmp(const void *a, const void *b)
-{
-	return strcmp(git_reference_name((git_reference *)a),
-		git_reference_name((git_reference *)b));
-}
-
-static int refdb_test_backend__exists(
-	int *exists,
-	git_refdb_backend *_backend,
-	const char *ref_name)
-{
-	refdb_test_backend *backend;
-	refdb_test_entry *entry;
-	size_t i;
-
-	assert(_backend);
-	backend = (refdb_test_backend *)_backend;
-
-	*exists = 0;
-
-	git_vector_foreach(&backend->refs, i, entry) {
-		if (strcmp(entry->name, ref_name) == 0) {
-			*exists = 1;
-			break;
-		}
-	}
-
-	return 0;
-}
-
-static int refdb_test_backend__write(
-	git_refdb_backend *_backend,
-	const git_reference *ref)
-{
-	refdb_test_backend *backend;
-	refdb_test_entry *entry;
-
-	assert(_backend);
-	backend = (refdb_test_backend *)_backend;
-
-	entry = git__calloc(1, sizeof(refdb_test_entry));
-	GITERR_CHECK_ALLOC(entry);
-
-	entry->name = git__strdup(git_reference_name(ref));
-	GITERR_CHECK_ALLOC(entry->name);
-
-	entry->type = git_reference_type(ref);
-
-	if (entry->type == GIT_REF_OID)
-		git_oid_cpy(&entry->target.oid, git_reference_target(ref));
-	else {
-		entry->target.symbolic = git__strdup(git_reference_symbolic_target(ref));
-		GITERR_CHECK_ALLOC(entry->target.symbolic);
-	}
-
-	git_vector_insert(&backend->refs, entry);
-
-	return 0;
-}
-
-static int refdb_test_backend__lookup(
-	git_reference **out,
-	git_refdb_backend *_backend,
-	const char *ref_name)
-{
-	refdb_test_backend *backend;
-	refdb_test_entry *entry;
-	size_t i;
-
-	assert(_backend);
-	backend = (refdb_test_backend *)_backend;
-
-	git_vector_foreach(&backend->refs, i, entry) {
-		if (strcmp(entry->name, ref_name) == 0) {
-
-			if (entry->type == GIT_REF_OID) {
-				*out = git_reference__alloc(ref_name,
-					&entry->target.oid, NULL);
-			} else if (entry->type == GIT_REF_SYMBOLIC) {
-				*out = git_reference__alloc_symbolic(ref_name,
-					entry->target.symbolic);
-			}
-
-			if (*out == NULL)
-				return -1;
-
-			return 0;
-		}
-	}
-
-	return GIT_ENOTFOUND;
-}
-
-static void refdb_test_entry_free(refdb_test_entry *entry)
-{
-	if (entry->type == GIT_REF_SYMBOLIC)
-		git__free(entry->target.symbolic);
-
-	git__free(entry->name);
-	git__free(entry);
-}
-
-static int refdb_test_backend__delete(
-	git_refdb_backend *_backend,
-	const git_reference *ref)
-{
-	refdb_test_backend *backend;
-	refdb_test_entry *entry;
-	size_t i;
-
-	assert(_backend);
-	backend = (refdb_test_backend *)_backend;
-
-	git_vector_foreach(&backend->refs, i, entry) {
-		if (strcmp(entry->name, git_reference_name(ref)) == 0) {
-			git_vector_remove(&backend->refs, i);
-			refdb_test_entry_free(entry);
-		}
-	}
-
-	return GIT_ENOTFOUND;
-}
-
-static void refdb_test_backend__free(git_refdb_backend *_backend)
-{
-	refdb_test_backend *backend;
-	refdb_test_entry *entry;
-	size_t i;
-
-	assert(_backend);
-	backend = (refdb_test_backend *)_backend;
-
-	git_vector_foreach(&backend->refs, i, entry)
-		refdb_test_entry_free(entry);
-
-	git_vector_free(&backend->refs);
-	git__free(backend);
-}
-
-int refdb_backend_test(
-	git_refdb_backend **backend_out,
-	git_repository *repo)
-{
-	refdb_test_backend *backend;
-
-	backend = git__calloc(1, sizeof(refdb_test_backend));
-	GITERR_CHECK_ALLOC(backend);
-
-	git_vector_init(&backend->refs, 0, ref_name_cmp);
-
-	backend->repo = repo;
-
-	backend->parent.exists = &refdb_test_backend__exists;
-	backend->parent.lookup = &refdb_test_backend__lookup;
-	backend->parent.write = &refdb_test_backend__write;
-	backend->parent.delete = &refdb_test_backend__delete;
-	backend->parent.free = &refdb_test_backend__free;
-
-	*backend_out = (git_refdb_backend *)backend;
-	return 0;
-}
diff --git a/tests-clar/refdb/testdb.h b/tests-clar/refdb/testdb.h
deleted file mode 100644
index a0d1bbc..0000000
--- a/tests-clar/refdb/testdb.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <git2/errors.h>
-#include <git2/repository.h>
-#include <git2/refdb.h>
-#include <git2/sys/refs.h>
-#include <git2/sys/refdb_backend.h>
-
-int refdb_backend_test(
-	git_refdb_backend **backend_out,
-	git_repository *repo);