Commit b41a30bdbb96dc0eb2d7e30497918ecbb45fd090

Vicent Marti 2012-07-27T18:45:55

Merge remote-tracking branch 'nulltoken/topic/branch-rework' into development

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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
diff --git a/include/git2/branch.h b/include/git2/branch.h
index 8884df1..15894b7 100644
--- a/include/git2/branch.h
+++ b/include/git2/branch.h
@@ -26,9 +26,9 @@ GIT_BEGIN_DECL
  * this target commit. If `force` is true and a reference
  * already exists with the given name, it'll be replaced.
  *
- * @param oid_out Pointer where to store the OID of the target commit.
+ * The returned reference must be freed by the user.
  *
- * @param repo Repository where to store the branch.
+ * @param branch_out Pointer where to store the underlying reference.
  *
  * @param branch_name Name for the branch; this name is
  * validated for consistency. It should also not conflict with
@@ -46,8 +46,7 @@ GIT_BEGIN_DECL
  * pointing to the provided target commit.
  */
 GIT_EXTERN(int) git_branch_create(
-		git_oid *oid_out,
-		git_repository *repo,
+		git_reference **branch_out,
 		const char *branch_name,
 		const git_object *target,
 		int force);
@@ -97,27 +96,62 @@ GIT_EXTERN(int) git_branch_foreach(
 );
 
 /**
- * Move/rename an existing branch reference.
- *
- * @param repo Repository where lives the branch.
+ * Move/rename an existing local branch reference.
  *
- * @param old_branch_name Current name of the branch to be moved;
- * this name is validated for consistency.
+ * @param branch Current underlying reference of the branch.
  *
  * @param new_branch_name Target name of the branch once the move
  * is performed; this name is validated for consistency.
  *
  * @param force Overwrite existing branch.
  *
- * @return 0 on success, GIT_ENOTFOUND if the branch
- * doesn't exist or an error code.
+ * @return 0 on success, or an error code.
  */
 GIT_EXTERN(int) git_branch_move(
-		git_repository *repo,
-		const char *old_branch_name,
+		git_reference *branch,
 		const char *new_branch_name,
 		int force);
 
+/**
+ * Lookup a branch by its name in a repository.
+ *
+ * The generated reference must be freed by the user.
+ *
+ * @param branch_out pointer to the looked-up branch reference
+ *
+ * @param repo the repository to look up the branch
+ *
+ * @param branch_name Name of the branch to be looked-up;
+ * this name is validated for consistency.
+ *
+ * @param branch_type Type of the considered branch. This should
+ * be valued with either GIT_BRANCH_LOCAL or GIT_BRANCH_REMOTE.
+ *
+ * @return 0 on success; GIT_ENOTFOUND when no matching branch
+ * exists, otherwise an error code.
+ */
+GIT_EXTERN(int) git_branch_lookup(
+		git_reference **branch_out,
+		git_repository *repo,
+		const char *branch_name,
+		git_branch_t branch_type);
+
+/**
+ * Return the reference supporting the remote tracking branch,
+ * given a local branch reference.
+ *
+ * @param tracking_out Pointer where to store the retrieved
+ * reference.
+ *
+ * @param branch Current underlying reference of the branch.
+ *
+ * @return 0 on success; GIT_ENOTFOUND when no remote tracking
+ * reference exists, otherwise an error code.
+ */
+GIT_EXTERN(int) git_branch_tracking(
+		git_reference **tracking_out,
+		git_reference *branch);
+
 /** @} */
 GIT_END_DECL
 #endif
diff --git a/include/git2/refs.h b/include/git2/refs.h
index b119e90..8dd8e31 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -363,26 +363,15 @@ GIT_EXTERN(int) git_reference_foreach_glob(
  */
 GIT_EXTERN(int) git_reference_has_log(git_reference *ref);
 
-
 /**
- * Return the reference supporting the remote tracking branch,
- * given a reference branch.
- *
- * The input reference has to be located in the `refs/heads`
- * namespace.
- *
- * @param tracking_ref Pointer where to store the retrieved
- * reference.
+ * Check if a reference is a local branch.
  *
- * @param branch_ref A git local branch reference. 
+ * @param ref A git reference
  *
- * @return 0 on success; GIT_ENOTFOUND when no remote tracking
- * reference exists, otherwise an error code.
+ * @return 1 when the reference lives in the refs/heads
+ * namespace; 0 otherwise.
  */
-GIT_EXTERN(int) git_reference_remote_tracking_from_branch(
-	git_reference **tracking_ref,
-	git_reference *branch_ref
-);
+GIT_EXTERN(int) git_reference_is_branch(git_reference *ref);
 
 /** @} */
 GIT_END_DECL
diff --git a/src/branch.c b/src/branch.c
index 671e420..d11eca8 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -7,8 +7,11 @@
 
 #include "common.h"
 #include "commit.h"
-#include "branch.h"
 #include "tag.h"
+#include "config.h"
+#include "refspec.h"
+
+#include "git2/branch.h"
 
 static int retrieve_branch_reference(
 	git_reference **branch_reference_out,
@@ -48,8 +51,7 @@ static int create_error_invalid(const char *msg)
 }
 
 int git_branch_create(
-		git_oid *oid_out,
-		git_repository *repo,
+		git_reference **ref_out,
 		const char *branch_name,
 		const git_object *target,
 		int force)
@@ -60,10 +62,7 @@ int git_branch_create(
 	git_buf canonical_branch_name = GIT_BUF_INIT;
 	int error = -1;
 
-	assert(repo && branch_name && target && oid_out);
-
-	if (git_object_owner(target) != repo)
-		return create_error_invalid("The given target does not belong to this repository");
+	assert(branch_name && target && ref_out);
 
 	target_type = git_object_type(target);
 
@@ -90,17 +89,17 @@ int git_branch_create(
 	if (git_buf_joinpath(&canonical_branch_name, GIT_REFS_HEADS_DIR, branch_name) < 0)
 		goto cleanup;
 
-	if (git_reference_create_oid(&branch, repo, git_buf_cstr(&canonical_branch_name), git_object_id(commit), force) < 0)
+	if (git_reference_create_oid(&branch, git_object_owner(commit),
+		git_buf_cstr(&canonical_branch_name), git_object_id(commit), force) < 0)
 		goto cleanup;
 
-	git_oid_cpy(oid_out, git_reference_oid(branch));
+	*ref_out = branch;
 	error = 0;
 
 cleanup:
 	if (target_type == GIT_OBJ_TAG)
 		git_object_free(commit);
 
-	git_reference_free(branch);
 	git_buf_free(&canonical_branch_name);
 	return error;
 }
@@ -111,6 +110,7 @@ int git_branch_delete(git_repository *repo, const char *branch_name, git_branch_
 	git_reference *head = NULL;
 	int error;
 
+	assert(repo && branch_name);
 	assert((branch_type == GIT_BRANCH_LOCAL) || (branch_type == GIT_BRANCH_REMOTE));
 
 	if ((error = retrieve_branch_reference(&branch, repo, branch_name, branch_type == GIT_BRANCH_REMOTE)) < 0)
@@ -183,28 +183,109 @@ int git_branch_foreach(
 	return git_reference_foreach(repo, GIT_REF_LISTALL, &branch_foreach_cb, (void *)&filter);
 }
 
-int git_branch_move(git_repository *repo, const char *old_branch_name, const char *new_branch_name, int force)
+static int not_a_local_branch(git_reference *ref)
+{
+	giterr_set(GITERR_INVALID, "Reference '%s' is not a local branch.", git_reference_name(ref));
+	return -1;
+}
+
+int git_branch_move(
+	git_reference *branch,
+	const char *new_branch_name,
+	int force)
 {
-	git_reference *reference = NULL;
-	git_buf old_reference_name = GIT_BUF_INIT, new_reference_name = GIT_BUF_INIT;
-	int error = 0;
+	git_buf new_reference_name = GIT_BUF_INIT;
+	int error;
 
-	if ((error = git_buf_joinpath(&old_reference_name, GIT_REFS_HEADS_DIR, old_branch_name)) < 0)
-		goto cleanup;
+	assert(branch && new_branch_name);
 
-	/* We need to be able to return GIT_ENOTFOUND */
-	if ((error = git_reference_lookup(&reference, repo, git_buf_cstr(&old_reference_name))) < 0)
-		goto cleanup;
+	if (!git_reference_is_branch(branch))
+		return not_a_local_branch(branch);
 
 	if ((error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0)
 		goto cleanup;
 
-	error = git_reference_rename(reference, git_buf_cstr(&new_reference_name), force);
+	error = git_reference_rename(branch, git_buf_cstr(&new_reference_name), force);
 
 cleanup:
-	git_reference_free(reference);
-	git_buf_free(&old_reference_name);
 	git_buf_free(&new_reference_name);
 
 	return error;
 }
+
+int git_branch_lookup(
+		git_reference **ref_out,
+		git_repository *repo,
+		const char *branch_name,
+		git_branch_t branch_type)
+{
+	assert(ref_out && repo && branch_name);
+
+	return retrieve_branch_reference(ref_out, repo, branch_name, branch_type == GIT_BRANCH_REMOTE);
+}
+
+int retrieve_tracking_configuration(const char **out, git_reference *branch, const char *format)
+{
+	git_config *config;
+	git_buf buf = GIT_BUF_INIT;
+	int error;
+
+	if (git_repository_config__weakptr(&config, git_reference_owner(branch)) < 0)
+		return -1;
+
+	if (git_buf_printf(&buf, format,
+		git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0)
+			return -1;
+
+	error = git_config_get_string(out, config, git_buf_cstr(&buf));
+	git_buf_free(&buf);
+	return error;
+}
+
+int git_branch_tracking(
+		git_reference **tracking_out,
+		git_reference *branch)
+{
+	const char *remote_name, *merge_name;
+	git_buf buf = GIT_BUF_INIT;
+	int error = -1;
+	git_remote *remote = NULL;
+	const git_refspec *refspec;
+
+	assert(tracking_out && branch);
+
+	if (!git_reference_is_branch(branch))
+		return not_a_local_branch(branch);
+
+	if ((error = retrieve_tracking_configuration(&remote_name, branch, "branch.%s.remote")) < 0)
+		goto cleanup;
+
+	if ((error = retrieve_tracking_configuration(&merge_name, branch, "branch.%s.merge")) < 0)
+		goto cleanup;
+
+	if (strcmp(".", remote_name) != 0) {
+		if ((error = git_remote_load(&remote, git_reference_owner(branch), remote_name)) < 0)
+			goto cleanup;
+
+		refspec = git_remote_fetchspec(remote);
+		if (refspec == NULL) {
+			error = GIT_ENOTFOUND;
+			goto cleanup;
+		}
+
+		if (git_refspec_transform_r(&buf, refspec, merge_name) < 0)
+			goto cleanup;
+	} else
+		if (git_buf_sets(&buf, merge_name) < 0)
+			goto cleanup;
+
+	error = git_reference_lookup(
+		tracking_out,
+		git_reference_owner(branch),
+		git_buf_cstr(&buf));
+
+cleanup:
+	git_remote_free(remote);
+	git_buf_free(&buf);
+	return error;
+}
diff --git a/src/branch.h b/src/branch.h
deleted file mode 100644
index d0e5abc..0000000
--- a/src/branch.h
+++ /dev/null
@@ -1,17 +0,0 @@
-/*
- * Copyright (C) 2009-2012 the libgit2 contributors
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-#ifndef INCLUDE_branch_h__
-#define INCLUDE_branch_h__
-
-#include "git2/branch.h"
-
-struct git_branch {
-	char *remote; /* TODO: Make this a git_remote */
-	char *merge;
-};
-
-#endif
diff --git a/src/refs.c b/src/refs.c
index b3c140b..32f54fc 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -11,7 +11,6 @@
 #include "fileops.h"
 #include "pack.h"
 #include "reflog.h"
-#include "config.h"
 
 #include <git2/tag.h>
 #include <git2/object.h>
@@ -1816,75 +1815,9 @@ int git_reference_has_log(
 	return result;
 }
 
-//TODO: How about also taking care of local tracking branches?
-//cf. http://alblue.bandlem.com/2011/07/git-tip-of-week-tracking-branches.html
-int git_reference_remote_tracking_from_branch(
-	git_reference **tracking_ref,
-	git_reference *branch_ref)
+int git_reference_is_branch(git_reference *ref)
 {
-	git_config *config = NULL;
-	const char *name, *remote, *merge;
-	git_buf buf = GIT_BUF_INIT;
-	int error = -1;
-
-	assert(tracking_ref && branch_ref);
-
-	name = git_reference_name(branch_ref);
-
-	if (git__prefixcmp(name, GIT_REFS_HEADS_DIR)) {
-		giterr_set(
-			GITERR_INVALID,
-			"Failed to retrieve tracking reference - '%s' is not a branch.",
-			name);
-		return -1;
-	}
-
-	if (git_repository_config(&config, branch_ref->owner) < 0)
-		return -1;
-
-	if (git_buf_printf(
-		&buf,
-		"branch.%s.remote",
-		name + strlen(GIT_REFS_HEADS_DIR)) < 0)
-			goto cleanup;
-
-	if ((error = git_config_get_string(&remote, config, git_buf_cstr(&buf))) < 0)
-		goto cleanup;
-
-	error = -1;
-
-	git_buf_clear(&buf);
-
-	//TODO: Is it ok to fail when no merge target is found?
-	if (git_buf_printf(
-		&buf,
-		"branch.%s.merge",
-		name + strlen(GIT_REFS_HEADS_DIR)) < 0)
-			goto cleanup;
-
-	if (git_config_get_string(&merge, config, git_buf_cstr(&buf)) < 0)
-		goto cleanup;
-
-	//TODO: Should we test this?
-	if (git__prefixcmp(merge, GIT_REFS_HEADS_DIR))
-		goto cleanup;
-
-	git_buf_clear(&buf);
-
-	if (git_buf_printf(
-		&buf,
-		"refs/remotes/%s/%s",
-		remote,
-		merge + strlen(GIT_REFS_HEADS_DIR)) < 0)
-			goto cleanup;
-
-	error = git_reference_lookup(
-		tracking_ref,
-		branch_ref->owner,
-		git_buf_cstr(&buf));
+	assert(ref);
 
-cleanup:
-	git_config_free(config);
-	git_buf_free(&buf);
-	return error;
+	return git__prefixcmp(ref->name, GIT_REFS_HEADS_DIR) == 0;
 }
diff --git a/src/revparse.c b/src/revparse.c
index b046928..e2c0de6 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -328,7 +328,7 @@ static int retrieve_remote_tracking_reference(git_reference **base_ref, const ch
 		*base_ref = NULL;
 	}
 
-	if ((error = git_reference_remote_tracking_from_branch(&tracking, ref)) < 0)
+	if ((error = git_branch_tracking(&tracking, ref)) < 0)
 		goto cleanup;
 	
 	*base_ref = tracking;
diff --git a/tests-clar/network/remotelocal.c b/tests-clar/network/remotelocal.c
index 5e20b42..16e3fe2 100644
--- a/tests-clar/network/remotelocal.c
+++ b/tests-clar/network/remotelocal.c
@@ -107,7 +107,7 @@ void test_network_remotelocal__retrieve_advertised_references(void)
 
 	cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs));
 
-	cl_assert_equal_i(how_many_refs, 22);
+	cl_assert_equal_i(how_many_refs, 23);
 }
 
 void test_network_remotelocal__retrieve_advertised_references_from_spaced_repository(void)
@@ -121,7 +121,7 @@ void test_network_remotelocal__retrieve_advertised_references_from_spaced_reposi
 
 	cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs));
 
-	cl_assert_equal_i(how_many_refs, 22);
+	cl_assert_equal_i(how_many_refs, 23);
 
 	git_remote_free(remote);	/* Disconnect from the "spaced repo" before the cleanup */
 	remote = NULL;
diff --git a/tests-clar/refs/branches/create.c b/tests-clar/refs/branches/create.c
index ad7e1fd..d539043 100644
--- a/tests-clar/refs/branches/create.c
+++ b/tests-clar/refs/branches/create.c
@@ -1,19 +1,22 @@
 #include "clar_libgit2.h"
 #include "refs.h"
-#include "branch.h"
 
 static git_repository *repo;
-static git_oid branch_target_oid;
 static git_object *target;
+static git_reference *branch;
 
 void test_refs_branches_create__initialize(void)
 {
 	cl_fixture_sandbox("testrepo.git");
 	cl_git_pass(git_repository_open(&repo, "testrepo.git"));
+
+	branch = NULL;
 }
 
 void test_refs_branches_create__cleanup(void)
 {
+	git_reference_free(branch);
+
 	git_object_free(target);
 	git_repository_free(repo);
 
@@ -39,54 +42,24 @@ void test_refs_branches_create__can_create_a_local_branch(void)
 {
 	retrieve_known_commit(&target, repo);
 
-	cl_git_pass(git_branch_create(&branch_target_oid, repo, NEW_BRANCH_NAME, target, 0));
-	cl_git_pass(git_oid_cmp(&branch_target_oid, git_object_id(target)));
-}
-
-void test_refs_branches_create__creating_a_local_branch_triggers_the_creation_of_a_new_direct_reference(void)
-{
-	git_reference *branch;
-
-	retrieve_known_commit(&target, repo);
-
-	cl_git_fail(git_reference_lookup(&branch, repo, GIT_REFS_HEADS_DIR NEW_BRANCH_NAME));
-
-	cl_git_pass(git_branch_create(&branch_target_oid, repo, NEW_BRANCH_NAME, target, 0));
-
-	cl_git_pass(git_reference_lookup(&branch, repo, GIT_REFS_HEADS_DIR NEW_BRANCH_NAME));
-	cl_assert(git_reference_type(branch) == GIT_REF_OID);
-
-	git_reference_free(branch);
+	cl_git_pass(git_branch_create(&branch, NEW_BRANCH_NAME, target, 0));
+	cl_git_pass(git_oid_cmp(git_reference_oid(branch), git_object_id(target)));
 }
 
 void test_refs_branches_create__can_not_create_a_branch_if_its_name_collide_with_an_existing_one(void)
 {
 	retrieve_known_commit(&target, repo);
 
-	cl_git_fail(git_branch_create(&branch_target_oid, repo, "br2", target, 0));
+	cl_git_fail(git_branch_create(&branch, "br2", target, 0));
 }
 
 void test_refs_branches_create__can_force_create_over_an_existing_branch(void)
 {
 	retrieve_known_commit(&target, repo);
 
-	cl_git_pass(git_branch_create(&branch_target_oid, repo, "br2", target, 1));
-	cl_git_pass(git_oid_cmp(&branch_target_oid, git_object_id(target)));
-}
-
-void test_refs_branches_create__can_not_create_a_branch_pointing_at_an_object_unknown_from_the_repository(void)
-{
-	git_repository *repo2;
-
-	/* Open another instance of the same repository */
-	cl_git_pass(git_repository_open(&repo2, cl_fixture("testrepo.git")));
-
-	/* Retrieve a commit object from this different repository */
-	retrieve_known_commit(&target, repo2);
-
-	cl_git_fail(git_branch_create(&branch_target_oid, repo, NEW_BRANCH_NAME, target, 0));
-
-	git_repository_free(repo2);
+	cl_git_pass(git_branch_create(&branch, "br2", target, 1));
+	cl_git_pass(git_oid_cmp(git_reference_oid(branch), git_object_id(target)));
+	cl_assert_equal_s("refs/heads/br2", git_reference_name(branch));
 }
 
 void test_refs_branches_create__creating_a_branch_targeting_a_tag_dereferences_it_to_its_commit(void)
@@ -94,8 +67,8 @@ void test_refs_branches_create__creating_a_branch_targeting_a_tag_dereferences_i
 	/* b25fa35 is a tag, pointing to another tag which points to a commit */
 	retrieve_target_from_oid(&target, repo, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
 
-	cl_git_pass(git_branch_create(&branch_target_oid, repo, NEW_BRANCH_NAME, target, 0));
-	cl_git_pass(git_oid_streq(&branch_target_oid, "e90810b8df3e80c413d903f631643c716887138d"));
+	cl_git_pass(git_branch_create(&branch, NEW_BRANCH_NAME, target, 0));
+	cl_git_pass(git_oid_streq(git_reference_oid(branch), "e90810b8df3e80c413d903f631643c716887138d"));
 }
 
 void test_refs_branches_create__can_not_create_a_branch_pointing_to_a_non_commit_object(void)
@@ -103,11 +76,11 @@ void test_refs_branches_create__can_not_create_a_branch_pointing_to_a_non_commit
 	/* 53fc32d is the tree of commit e90810b */
 	retrieve_target_from_oid(&target, repo, "53fc32d17276939fc79ed05badaef2db09990016");
 
-	cl_git_fail(git_branch_create(&branch_target_oid, repo, NEW_BRANCH_NAME, target, 0));
+	cl_git_fail(git_branch_create(&branch, NEW_BRANCH_NAME, target, 0));
 	git_object_free(target);
 
 	/* 521d87c is an annotated tag pointing to a blob */
 	retrieve_target_from_oid(&target, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91");
 
-	cl_git_fail(git_branch_create(&branch_target_oid, repo, NEW_BRANCH_NAME, target, 0));
+	cl_git_fail(git_branch_create(&branch, NEW_BRANCH_NAME, target, 0));
 }
diff --git a/tests-clar/refs/branches/delete.c b/tests-clar/refs/branches/delete.c
index 03d3c56..699655f 100644
--- a/tests-clar/refs/branches/delete.c
+++ b/tests-clar/refs/branches/delete.c
@@ -1,6 +1,5 @@
 #include "clar_libgit2.h"
 #include "refs.h"
-#include "branch.h"
 
 static git_repository *repo;
 static git_reference *fake_remote;
diff --git a/tests-clar/refs/branches/foreach.c b/tests-clar/refs/branches/foreach.c
index b6e9737..794233c 100644
--- a/tests-clar/refs/branches/foreach.c
+++ b/tests-clar/refs/branches/foreach.c
@@ -1,6 +1,5 @@
 #include "clar_libgit2.h"
 #include "refs.h"
-#include "branch.h"
 
 static git_repository *repo;
 static git_reference *fake_remote;
@@ -48,7 +47,7 @@ static void assert_retrieval(unsigned int flags, unsigned int expected_count)
 
 void test_refs_branches_foreach__retrieve_all_branches(void)
 {
-	assert_retrieval(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, 10);
+	assert_retrieval(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, 11);
 }
 
 void test_refs_branches_foreach__retrieve_remote_branches(void)
@@ -58,7 +57,7 @@ void test_refs_branches_foreach__retrieve_remote_branches(void)
 
 void test_refs_branches_foreach__retrieve_local_branches(void)
 {
-	assert_retrieval(GIT_BRANCH_LOCAL, 8);
+	assert_retrieval(GIT_BRANCH_LOCAL, 9);
 }
 
 struct expectations {
diff --git a/tests-clar/refs/branches/lookup.c b/tests-clar/refs/branches/lookup.c
new file mode 100644
index 0000000..2aabf98
--- /dev/null
+++ b/tests-clar/refs/branches/lookup.c
@@ -0,0 +1,35 @@
+#include "clar_libgit2.h"
+#include "refs.h"
+
+static git_repository *repo;
+static git_reference *branch;
+
+void test_refs_branches_lookup__initialize(void)
+{
+	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
+
+	branch = NULL;
+}
+
+void test_refs_branches_lookup__cleanup(void)
+{
+	git_reference_free(branch);
+
+	git_repository_free(repo);
+}
+
+void test_refs_branches_lookup__can_retrieve_a_local_branch(void)
+{
+	cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
+}
+
+void test_refs_branches_lookup__can_retrieve_a_remote_tracking_branch(void)
+{
+	cl_git_pass(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_REMOTE));
+}
+
+void test_refs_branches_lookup__trying_to_retrieve_an_unknown_branch_returns_ENOTFOUND(void)
+{
+	cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "where/are/you", GIT_BRANCH_LOCAL));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "over/here", GIT_BRANCH_REMOTE));
+}
diff --git a/tests-clar/refs/branches/move.c b/tests-clar/refs/branches/move.c
index 242e5cd..6750473 100644
--- a/tests-clar/refs/branches/move.c
+++ b/tests-clar/refs/branches/move.c
@@ -1,72 +1,64 @@
 #include "clar_libgit2.h"
-#include "branch.h"
+#include "refs.h"
 
 static git_repository *repo;
+static git_reference *ref;
 
 void test_refs_branches_move__initialize(void)
 {
-	cl_fixture_sandbox("testrepo.git");
-	cl_git_pass(git_repository_open(&repo, "testrepo.git"));
+	repo = cl_git_sandbox_init("testrepo.git");
+
+	cl_git_pass(git_reference_lookup(&ref, repo, "refs/heads/br2"));
 }
 
 void test_refs_branches_move__cleanup(void)
 {
-	git_repository_free(repo);
-
-	cl_fixture_cleanup("testrepo.git");
+	git_reference_free(ref);
+	cl_git_sandbox_cleanup();
 }
 
 #define NEW_BRANCH_NAME "new-branch-on-the-block"
 
 void test_refs_branches_move__can_move_a_local_branch(void)
 {
-	cl_git_pass(git_branch_move(repo, "br2", NEW_BRANCH_NAME, 0));
+	cl_git_pass(git_branch_move(ref, NEW_BRANCH_NAME, 0));
+	cl_assert_equal_s(GIT_REFS_HEADS_DIR NEW_BRANCH_NAME, git_reference_name(ref));
 }
 
 void test_refs_branches_move__can_move_a_local_branch_to_a_different_namespace(void)
 {
 	/* Downward */
-	cl_git_pass(git_branch_move(repo, "br2", "somewhere/" NEW_BRANCH_NAME, 0));
+	cl_git_pass(git_branch_move(ref, "somewhere/" NEW_BRANCH_NAME, 0));
 
 	/* Upward */
-	cl_git_pass(git_branch_move(repo, "somewhere/" NEW_BRANCH_NAME, "br2", 0));
+	cl_git_pass(git_branch_move(ref, "br2", 0));
 }
 
 void test_refs_branches_move__can_move_a_local_branch_to_a_partially_colliding_namespace(void)
 {
 	/* Downward */
-	cl_git_pass(git_branch_move(repo, "br2", "br2/" NEW_BRANCH_NAME, 0));
+	cl_git_pass(git_branch_move(ref, "br2/" NEW_BRANCH_NAME, 0));
 
 	/* Upward */
-	cl_git_pass(git_branch_move(repo, "br2/" NEW_BRANCH_NAME, "br2", 0));
+	cl_git_pass(git_branch_move(ref, "br2", 0));
 }
 
 void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_collide_with_an_existing_one(void)
 {
-	cl_git_fail(git_branch_move(repo, "br2", "master", 0));
+	cl_git_fail(git_branch_move(ref, "master", 0));
 }
 
-void test_refs_branches_move__can_not_move_a_non_existing_branch(void)
+void test_refs_branches_move__can_not_move_a_non_branch(void)
 {
-	cl_git_fail(git_branch_move(repo, "i-am-no-branch", NEW_BRANCH_NAME, 0));
-}
+	git_reference *tag;
 
-void test_refs_branches_move__can_force_move_over_an_existing_branch(void)
-{
-	cl_git_pass(git_branch_move(repo, "br2", "master", 1));
-}
+	cl_git_pass(git_reference_lookup(&tag, repo, "refs/tags/e90810b"));
+	cl_git_fail(git_branch_move(tag, NEW_BRANCH_NAME, 0));
 
-void test_refs_branches_move__can_not_move_a_branch_through_its_canonical_name(void)
-{
-	cl_git_fail(git_branch_move(repo, "refs/heads/br2", NEW_BRANCH_NAME, 1));
+	git_reference_free(tag);
 }
 
-void test_refs_branches_move__moving_a_non_exisiting_branch_returns_ENOTFOUND(void)
+void test_refs_branches_move__can_force_move_over_an_existing_branch(void)
 {
-	int error;
-
-	error = git_branch_move(repo, "where/am/I", NEW_BRANCH_NAME, 0);
-	cl_git_fail(error);
-
-	cl_assert_equal_i(GIT_ENOTFOUND, error);
+	cl_git_pass(git_branch_move(ref, "master", 1));
 }
diff --git a/tests-clar/refs/branches/tracking.c b/tests-clar/refs/branches/tracking.c
new file mode 100644
index 0000000..8f70194
--- /dev/null
+++ b/tests-clar/refs/branches/tracking.c
@@ -0,0 +1,69 @@
+#include "clar_libgit2.h"
+#include "refs.h"
+
+static git_repository *repo;
+static git_reference *branch;
+
+void test_refs_branches_tracking__initialize(void)
+{
+	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
+
+	branch = NULL;
+}
+
+void test_refs_branches_tracking__cleanup(void)
+{
+	git_reference_free(branch);
+
+	git_repository_free(repo);
+}
+
+void test_refs_branches_tracking__can_retrieve_the_remote_tracking_reference_of_a_local_branch(void)
+{
+	git_reference *branch, *tracking;
+
+	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
+
+	cl_git_pass(git_branch_tracking(&tracking, branch));
+
+	cl_assert_equal_s("refs/remotes/test/master", git_reference_name(tracking));
+
+	git_reference_free(branch);
+	git_reference_free(tracking);
+}
+
+void test_refs_branches_tracking__can_retrieve_the_local_tracking_reference_of_a_local_branch(void)
+{
+	git_reference *branch, *tracking;
+
+	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/track-local"));
+
+	cl_git_pass(git_branch_tracking(&tracking, branch));
+
+	cl_assert_equal_s("refs/heads/master", git_reference_name(tracking));
+
+	git_reference_free(branch);
+	git_reference_free(tracking);
+}
+
+void test_refs_branches_tracking__cannot_retrieve_a_remote_tracking_reference_from_a_non_branch(void)
+{
+	git_reference *branch, *tracking;
+
+	cl_git_pass(git_reference_lookup(&branch, repo, "refs/tags/e90810b"));
+
+	cl_git_fail(git_branch_tracking(&tracking, branch));
+
+	git_reference_free(branch);
+}
+
+void test_refs_branches_tracking__trying_to_retrieve_a_remote_tracking_reference_from_a_plain_local_branch_returns_GIT_ENOTFOUND(void)
+{
+	git_reference *branch, *tracking;
+
+	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/subtrees"));
+
+	cl_assert_equal_i(GIT_ENOTFOUND, git_branch_tracking(&tracking, branch));
+
+	git_reference_free(branch);
+}
diff --git a/tests-clar/refs/foreachglob.c b/tests-clar/refs/foreachglob.c
index d1412a9..b024d36 100644
--- a/tests-clar/refs/foreachglob.c
+++ b/tests-clar/refs/foreachglob.c
@@ -46,7 +46,7 @@ static void assert_retrieval(const char *glob, unsigned int flags, int expected_
 void test_refs_foreachglob__retrieve_all_refs(void)
 {
 	/* 7 heads (including one packed head) + 1 note + 2 remotes + 6 tags */
-	assert_retrieval("*", GIT_REF_LISTALL, 17);
+	assert_retrieval("*", GIT_REF_LISTALL, 18);
 }
 
 void test_refs_foreachglob__retrieve_remote_branches(void)
@@ -56,7 +56,7 @@ void test_refs_foreachglob__retrieve_remote_branches(void)
 
 void test_refs_foreachglob__retrieve_local_branches(void)
 {
-	assert_retrieval("refs/heads/*", GIT_REF_LISTALL, 8);
+	assert_retrieval("refs/heads/*", GIT_REF_LISTALL, 9);
 }
 
 void test_refs_foreachglob__retrieve_partially_named_references(void)
diff --git a/tests-clar/refs/read.c b/tests-clar/refs/read.c
index ce4eefe..1948e0a 100644
--- a/tests-clar/refs/read.c
+++ b/tests-clar/refs/read.c
@@ -202,3 +202,19 @@ void test_refs_read__unfound_return_ENOTFOUND(void)
 	cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&reference, g_repo, "refs/tags/test/master"));
 	cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&reference, g_repo, "refs/tags/test/farther/master"));
 }
+
+static void assert_is_branch(const char *name, bool expected_branchness)
+{
+	git_reference *reference;
+	cl_git_pass(git_reference_lookup(&reference, g_repo, name));
+	cl_assert_equal_i(expected_branchness, git_reference_is_branch(reference));
+	git_reference_free(reference);
+}
+
+void test_refs_read__can_determine_if_a_reference_is_a_local_branch(void)
+{
+	assert_is_branch("refs/heads/master", true);
+	assert_is_branch("refs/heads/packed", true);
+	assert_is_branch("refs/remotes/test/master", false);
+	assert_is_branch("refs/tags/e90810b", false);
+}
diff --git a/tests-clar/refs/remotetracking.c b/tests-clar/refs/remotetracking.c
deleted file mode 100644
index c4ec588..0000000
--- a/tests-clar/refs/remotetracking.c
+++ /dev/null
@@ -1,49 +0,0 @@
-#include "clar_libgit2.h"
-
-static git_repository *g_repo;
-
-void test_refs_remotetracking__initialize(void)
-{
-	cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
-}
-
-void test_refs_remotetracking__cleanup(void)
-{
-	git_repository_free(g_repo);
-}
-
-void test_refs_remotetracking__unfound_returns_GIT_ENOTFOUND(void)
-{
-	git_reference *branch, *tracking;
-
-	cl_git_pass(git_reference_lookup(&branch, g_repo, "refs/heads/subtrees"));
-
-	cl_assert_equal_i(GIT_ENOTFOUND, git_reference_remote_tracking_from_branch(&tracking, branch));
-
-	git_reference_free(branch);
-}
-
-void test_refs_remotetracking__retrieving_from_a_non_head_fails(void)
-{
-	git_reference *branch, *tracking;
-
-	cl_git_pass(git_reference_lookup(&branch, g_repo, "refs/tags/e90810b"));
-
-	cl_git_fail(git_reference_remote_tracking_from_branch(&tracking, branch));
-
-	git_reference_free(branch);
-}
-
-void test_refs_remotetracking__can_retrieve_a_remote_tracking_branch_reference(void)
-{
-	git_reference *branch, *tracking;
-
-	cl_git_pass(git_reference_lookup(&branch, g_repo, "refs/heads/master"));
-
-	cl_git_pass(git_reference_remote_tracking_from_branch(&tracking, branch));
-
-	cl_assert_equal_s("refs/remotes/test/master", git_reference_name(tracking));
-
-	git_reference_free(branch);
-	git_reference_free(tracking);
-}
diff --git a/tests-clar/resources/testrepo.git/config b/tests-clar/resources/testrepo.git/config
index c99d971..04ab387 100644
--- a/tests-clar/resources/testrepo.git/config
+++ b/tests-clar/resources/testrepo.git/config
@@ -15,3 +15,6 @@
 [branch "master"]
    remote = test
    merge = refs/heads/master
+[branch "track-local"]
+   remote = .
+   merge = refs/heads/master
diff --git a/tests-clar/resources/testrepo.git/refs/heads/track-local b/tests-clar/resources/testrepo.git/refs/heads/track-local
new file mode 100644
index 0000000..f37febb
--- /dev/null
+++ b/tests-clar/resources/testrepo.git/refs/heads/track-local
@@ -0,0 +1 @@
+9fd738e8f7967c078dceed8190330fc8648ee56a