Commit ea8bac37b0c3a302b8f740cbeb4af50d0626815b

Vicent Martí 2013-04-11T06:34:59

Merge pull request #1450 from carlosmn/branch-upstream Branch upstream configuration

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
diff --git a/include/git2/branch.h b/include/git2/branch.h
index 89a1396..b151713 100644
--- a/include/git2/branch.h
+++ b/include/git2/branch.h
@@ -171,11 +171,23 @@ GIT_EXTERN(int) git_branch_name(const char **out,
  * @return 0 on success; GIT_ENOTFOUND when no remote tracking
  * reference exists, otherwise an error code.
  */
-GIT_EXTERN(int) git_branch_tracking(
+GIT_EXTERN(int) git_branch_upstream(
 	git_reference **out,
 	git_reference *branch);
 
 /**
+ * Set the upstream configuration for a given local branch
+ *
+ * @param branch the branch to configure
+ *
+ * @param upstream_name remote-tracking or local branch to set as
+ * upstream. Pass NULL to unset.
+ *
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_branch_set_upstream(git_reference *branch, const char *upstream_name);
+
+/**
  * Return the name of the reference supporting the remote tracking branch,
  * given the name of a local branch reference.
  *
@@ -193,7 +205,7 @@ GIT_EXTERN(int) git_branch_tracking(
  *     including the trailing NUL byte; GIT_ENOTFOUND when no remote tracking
  *     reference exists, otherwise an error code.
  */
-GIT_EXTERN(int) git_branch_tracking_name(
+GIT_EXTERN(int) git_branch_upstream_name(
 	char *tracking_branch_name_out,
 	size_t buffer_size,
 	git_repository *repo,
diff --git a/src/branch.c b/src/branch.c
index 45ecca7..e708879 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -228,7 +228,7 @@ int git_branch_name(const char **out, git_reference *ref)
 	return 0;
 }
 
-static int retrieve_tracking_configuration(
+static int retrieve_upstream_configuration(
 	const char **out,
 	git_repository *repo,
 	const char *canonical_branch_name,
@@ -250,7 +250,7 @@ static int retrieve_tracking_configuration(
 	return error;
 }
 
-int git_branch_tracking__name(
+int git_branch_upstream__name(
 	git_buf *tracking_name,
 	git_repository *repo,
 	const char *canonical_branch_name)
@@ -266,11 +266,11 @@ int git_branch_tracking__name(
 	if (!git_reference__is_branch(canonical_branch_name))
 		return not_a_local_branch(canonical_branch_name);
 
-	if ((error = retrieve_tracking_configuration(
+	if ((error = retrieve_upstream_configuration(
 		&remote_name, repo, canonical_branch_name, "branch.%s.remote")) < 0)
 			goto cleanup;
 
-	if ((error = retrieve_tracking_configuration(
+	if ((error = retrieve_upstream_configuration(
 		&merge_name, repo, canonical_branch_name, "branch.%s.merge")) < 0)
 			goto cleanup;
 
@@ -305,23 +305,16 @@ cleanup:
 	return error;
 }
 
-int git_branch_remote_name(
-	char *remote_name_out,
-	size_t buffer_size,
-	git_repository *repo,
-	const char *canonical_branch_name)
+static int remote_name(git_buf *buf, git_repository *repo, const char *canonical_branch_name)
 {
 	git_strarray remote_list = {0};
-	size_t i, remote_name_size;
+	size_t i;
 	git_remote *remote;
 	const git_refspec *fetchspec;
 	int error = 0;
 	char *remote_name = NULL;
 
-	assert(repo && canonical_branch_name);
-
-	if (remote_name_out && buffer_size)
-		*remote_name_out = '\0';
+	assert(buf && repo && canonical_branch_name);
 
 	/* Verify that this is a remote branch */
 	if (!git_reference__is_remote(canonical_branch_name)) {
@@ -338,7 +331,7 @@ int git_branch_remote_name(
 	/* Find matching remotes */
 	for (i = 0; i < remote_list.count; i++) {
 		if ((error = git_remote_load(&remote, repo, remote_list.strings[i])) < 0)
-			goto cleanup;
+			continue;
 
 		fetchspec = git_remote_fetchspec(remote);
 
@@ -362,23 +355,10 @@ int git_branch_remote_name(
 	}
 
 	if (remote_name) {
-		remote_name_size = strlen(remote_name) + 1;
-		error = (int) remote_name_size;
-
-		if (remote_name_out) {
-			if(remote_name_size > buffer_size) {
-				giterr_set(
-					GITERR_INVALID,
-					"Buffer too short to hold the remote name.");
-				error = GIT_ERROR;
-				goto cleanup;
-			}
-
-			memcpy(remote_name_out, remote_name, remote_name_size);
-		}
+		git_buf_clear(buf);
+		error = git_buf_puts(buf, remote_name);
 	} else {
 		error = GIT_ENOTFOUND;
-		goto cleanup;
 	}
 
 cleanup:
@@ -386,7 +366,24 @@ cleanup:
 	return error;
 }
 
-int git_branch_tracking_name(
+int git_branch_remote_name(char *buffer, size_t buffer_len, git_repository *repo, const char *refname)
+{
+	int ret;
+	git_buf buf = GIT_BUF_INIT;
+
+	if ((ret = remote_name(&buf, repo, refname)) < 0)
+		return ret;
+
+	if (buffer)
+		git_buf_copy_cstr(buffer, buffer_len, &buf);
+
+	ret = git_buf_len(&buf) + 1;
+	git_buf_free(&buf);
+
+	return ret;
+}
+
+int git_branch_upstream_name(
 	char *tracking_branch_name_out,
 	size_t buffer_size,
 	git_repository *repo,
@@ -400,7 +397,7 @@ int git_branch_tracking_name(
 	if (tracking_branch_name_out && buffer_size)
 		*tracking_branch_name_out = '\0';
 
-	if ((error = git_branch_tracking__name(
+	if ((error = git_branch_upstream__name(
 		&buf, repo, canonical_branch_name)) < 0)
 			goto cleanup;
 
@@ -422,14 +419,14 @@ cleanup:
 	return (int)error;
 }
 
-int git_branch_tracking(
+int git_branch_upstream(
 		git_reference **tracking_out,
 		git_reference *branch)
 {
 	int error;
 	git_buf tracking_name = GIT_BUF_INIT;
 
-	if ((error = git_branch_tracking__name(&tracking_name,
+	if ((error = git_branch_upstream__name(&tracking_name,
 		git_reference_owner(branch), git_reference_name(branch))) < 0)
 			return error;
 
@@ -442,6 +439,120 @@ int git_branch_tracking(
 	return error;
 }
 
+static int unset_upstream(git_config *config, const char *shortname)
+{
+	git_buf buf = GIT_BUF_INIT;
+
+	if (git_buf_printf(&buf, "branch.%s.remote", shortname) < 0)
+		return -1;
+
+	if (git_config_delete_entry(config, git_buf_cstr(&buf)) < 0)
+		goto on_error;
+
+	git_buf_clear(&buf);
+	if (git_buf_printf(&buf, "branch.%s.merge", shortname) < 0)
+		goto on_error;
+
+	if (git_config_delete_entry(config, git_buf_cstr(&buf)) < 0)
+		goto on_error;
+
+	git_buf_free(&buf);
+	return 0;
+
+on_error:
+	git_buf_free(&buf);
+	return -1;
+}
+
+int git_branch_set_upstream(git_reference *branch, const char *upstream_name)
+{
+	git_buf key = GIT_BUF_INIT, value = GIT_BUF_INIT;
+	git_reference *upstream;
+	git_repository *repo;
+	git_remote *remote = NULL;
+	git_config *config;
+	const char *name, *shortname;
+	int local;
+	const git_refspec *fetchspec;
+
+	name = git_reference_name(branch);
+	if (!git_reference__is_branch(name))
+		return not_a_local_branch(name);
+
+	if (git_repository_config__weakptr(&config, git_reference_owner(branch)) < 0)
+		return -1;
+
+	shortname = name + strlen(GIT_REFS_HEADS_DIR);
+
+	if (upstream_name == NULL)
+		return unset_upstream(config, shortname);
+
+	repo = git_reference_owner(branch);
+
+	/* First we need to figure out whether it's a branch or remote-tracking */
+	if (git_branch_lookup(&upstream, repo, upstream_name, GIT_BRANCH_LOCAL) == 0)
+		local = 1;
+	else if (git_branch_lookup(&upstream, repo, upstream_name, GIT_BRANCH_REMOTE) == 0)
+		local = 0;
+	else
+		return GIT_ENOTFOUND;
+
+	/*
+	 * If it's local, the remote is "." and the branch name is
+	 * simply the refname. Otherwise we need to figure out what
+	 * the remote-tracking branch's name on the remote is and use
+	 * that.
+	 */
+	if (local)
+		git_buf_puts(&value, ".");
+	else
+		remote_name(&value, repo, git_reference_name(upstream));
+
+	if (git_buf_printf(&key, "branch.%s.remote", shortname) < 0)
+		goto on_error;
+
+	if (git_config_set_string(config, git_buf_cstr(&key), git_buf_cstr(&value)) < 0)
+		goto on_error;
+
+	if (local) {
+		if (git_buf_puts(&value, git_reference_name(branch)) < 0)
+			goto on_error;
+	} else {
+		/* Get the remoe-tracking branch's refname in its repo */
+		if (git_remote_load(&remote, repo, git_buf_cstr(&value)) < 0)
+			goto on_error;
+
+		fetchspec = git_remote_fetchspec(remote);
+		git_buf_clear(&value);
+		if (git_refspec_transform_l(&value, fetchspec, git_reference_name(upstream)) < 0)
+			goto on_error;
+
+		git_remote_free(remote);
+		remote = NULL;
+	}
+
+	git_buf_clear(&key);
+	if (git_buf_printf(&key, "branch.%s.merge", shortname) < 0)
+		goto on_error;
+
+	if (git_config_set_string(config, git_buf_cstr(&key), git_buf_cstr(&value)) < 0)
+		goto on_error;
+
+	git_reference_free(upstream);
+	git_buf_free(&key);
+	git_buf_free(&value);
+
+	return 0;
+
+on_error:
+	git_reference_free(upstream);
+	git_buf_free(&key);
+	git_buf_free(&value);
+	git_remote_free(remote);
+
+	return -1;
+}
+
 int git_branch_is_head(
 		git_reference *branch)
 {
diff --git a/src/branch.h b/src/branch.h
index 8a26c4f..d02f2af 100644
--- a/src/branch.h
+++ b/src/branch.h
@@ -9,7 +9,7 @@
 
 #include "buffer.h"
 
-int git_branch_tracking__name(
+int git_branch_upstream__name(
 	git_buf *tracking_name,
 	git_repository *repo,
 	const char *canonical_branch_name);
diff --git a/src/remote.c b/src/remote.c
index a6f62d6..896361e 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -705,7 +705,7 @@ static int remote_head_for_ref(git_remote_head **out, git_remote *remote, git_ve
 
 	if ((error = git_reference_resolve(&resolved_ref, ref)) < 0 ||
 		(!git_reference_is_branch(resolved_ref)) ||
-		(error = git_branch_tracking(&tracking_ref, resolved_ref)) < 0 ||
+		(error = git_branch_upstream(&tracking_ref, resolved_ref)) < 0 ||
 		(error = git_refspec_transform_l(&remote_name, &remote->fetch, git_reference_name(tracking_ref))) < 0) {
 		/* Not an error if HEAD is orphaned or no tracking branch */
 		if (error == GIT_ENOTFOUND)
diff --git a/src/revparse.c b/src/revparse.c
index b1eb51b..fd62a2f 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -356,7 +356,7 @@ static int retrieve_remote_tracking_reference(git_reference **base_ref, const ch
 		goto cleanup;
 	}
 
-	if ((error = git_branch_tracking(&tracking, ref)) < 0)
+	if ((error = git_branch_upstream(&tracking, ref)) < 0)
 		goto cleanup;
 	
 	*base_ref = tracking;
diff --git a/src/submodule.c b/src/submodule.c
index 066a881..2fdaf2f 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -1327,7 +1327,7 @@ static int lookup_head_remote(git_buf *url, git_repository *repo)
 		goto cleanup;
 	}
 
-	if ((error = git_branch_tracking(&remote, head)) < 0)
+	if ((error = git_branch_upstream(&remote, head)) < 0)
 		goto cleanup;
 
 	/* remote should refer to something like refs/remotes/ORIGIN/BRANCH */
diff --git a/tests-clar/clone/empty.c b/tests-clar/clone/empty.c
index 0f86725..f190523 100644
--- a/tests-clar/clone/empty.c
+++ b/tests-clar/clone/empty.c
@@ -49,7 +49,7 @@ void test_clone_empty__can_clone_an_empty_local_repo_barely(void)
 
 	/* ...one can still retrieve the name of the remote tracking reference */
 	cl_assert_equal_i((int)strlen(expected_tracked_branch_name) + 1, 
-		git_branch_tracking_name(buffer, 1024, g_repo_cloned, local_name));
+		git_branch_upstream_name(buffer, 1024, g_repo_cloned, local_name));
 
 	cl_assert_equal_s(expected_tracked_branch_name, buffer);
 
diff --git a/tests-clar/refs/branches/remote.c b/tests-clar/refs/branches/remote.c
index 5272d12..2beef37 100644
--- a/tests-clar/refs/branches/remote.c
+++ b/tests-clar/refs/branches/remote.c
@@ -42,7 +42,7 @@ void test_refs_branches_remote__insufficient_buffer_returns_error(void)
 
 	cl_git_fail_with(git_branch_remote_name(remotename,
 		expected_remote_name_length - 1, g_repo, remote_tracking_branch_name),
-			GIT_ERROR);
+			expected_remote_name_length);
 }
 
 void test_refs_branches_remote__no_matching_remote_returns_error(void)
diff --git a/tests-clar/refs/branches/tracking.c b/tests-clar/refs/branches/tracking.c
deleted file mode 100644
index 30599d9..0000000
--- a/tests-clar/refs/branches/tracking.c
+++ /dev/null
@@ -1,95 +0,0 @@
-#include "clar_libgit2.h"
-#include "refs.h"
-
-static git_repository *repo;
-static git_reference *branch, *tracking;
-
-void test_refs_branches_tracking__initialize(void)
-{
-	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
-
-	branch = NULL;
-	tracking = NULL;
-}
-
-void test_refs_branches_tracking__cleanup(void)
-{
-	git_reference_free(tracking);
-	git_reference_free(branch);
-	branch = NULL;
-
-	git_repository_free(repo);
-	repo = NULL;
-}
-
-void test_refs_branches_tracking__can_retrieve_the_remote_tracking_reference_of_a_local_branch(void)
-{
-	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));
-}
-
-void test_refs_branches_tracking__can_retrieve_the_local_tracking_reference_of_a_local_branch(void)
-{
-	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));
-}
-
-void test_refs_branches_tracking__cannot_retrieve_a_remote_tracking_reference_from_a_non_branch(void)
-{
-	cl_git_pass(git_reference_lookup(&branch, repo, "refs/tags/e90810b"));
-
-	cl_git_fail(git_branch_tracking(&tracking, branch));
-}
-
-void test_refs_branches_tracking__trying_to_retrieve_a_remote_tracking_reference_from_a_plain_local_branch_returns_GIT_ENOTFOUND(void)
-{
-	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/subtrees"));
-
-	cl_assert_equal_i(GIT_ENOTFOUND, git_branch_tracking(&tracking, branch));
-}
-
-void test_refs_branches_tracking__trying_to_retrieve_a_remote_tracking_reference_from_a_branch_with_no_fetchspec_returns_GIT_ENOTFOUND(void)
-{
-	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/cannot-fetch"));
-
-	cl_assert_equal_i(GIT_ENOTFOUND, git_branch_tracking(&tracking, branch));
-}
-
-static void assert_merge_and_or_remote_key_missing(git_repository *repository, const git_commit *target, const char *entry_name)
-{
-	git_reference *branch;
-
-	cl_assert_equal_i(GIT_OBJ_COMMIT, git_object_type((git_object*)target));
-	cl_git_pass(git_branch_create(&branch, repository, entry_name, (git_commit*)target, 0));
-
-	cl_assert_equal_i(GIT_ENOTFOUND, git_branch_tracking(&tracking, branch));
-
-	git_reference_free(branch);
-}
-
-void test_refs_branches_tracking__retrieve_a_remote_tracking_reference_from_a_branch_with_no_remote_returns_GIT_ENOTFOUND(void)
-{
-	git_reference *head;
-	git_repository *repository;
-	git_commit *target;
-
-	repository = cl_git_sandbox_init("testrepo.git");
-
-	cl_git_pass(git_repository_head(&head, repository));
-	cl_git_pass(git_reference_peel(((git_object **)&target), head, GIT_OBJ_COMMIT));
-	git_reference_free(head);
-
-	assert_merge_and_or_remote_key_missing(repository, target, "remoteless");
-	assert_merge_and_or_remote_key_missing(repository, target, "mergeless");
-	assert_merge_and_or_remote_key_missing(repository, target, "mergeandremoteless");
-
-	git_commit_free(target);
-
-	cl_git_sandbox_cleanup();
-}
diff --git a/tests-clar/refs/branches/trackingname.c b/tests-clar/refs/branches/trackingname.c
deleted file mode 100644
index 5aee333..0000000
--- a/tests-clar/refs/branches/trackingname.c
+++ /dev/null
@@ -1,42 +0,0 @@
-#include "clar_libgit2.h"
-#include "branch.h"
-
-static git_repository *repo;
-static git_buf tracking_name;
-
-void test_refs_branches_trackingname__initialize(void)
-{
-	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
-
-	git_buf_init(&tracking_name, 0);
-}
-
-void test_refs_branches_trackingname__cleanup(void)
-{
-	git_buf_free(&tracking_name);
-
-	git_repository_free(repo);
-	repo = NULL;
-}
-
-void test_refs_branches_trackingname__can_retrieve_the_remote_tracking_reference_name_of_a_local_branch(void)
-{
-	cl_git_pass(git_branch_tracking__name(
-		&tracking_name, repo, "refs/heads/master"));
-
-	cl_assert_equal_s("refs/remotes/test/master", git_buf_cstr(&tracking_name));
-}
-
-void test_refs_branches_trackingname__can_retrieve_the_local_tracking_reference_name_of_a_local_branch(void)
-{
-	cl_git_pass(git_branch_tracking__name(
-		&tracking_name, repo, "refs/heads/track-local"));
-
-	cl_assert_equal_s("refs/heads/master", git_buf_cstr(&tracking_name));
-}
-
-void test_refs_branches_trackingname__can_return_the_size_of_thelocal_tracking_reference_name_of_a_local_branch(void)
-{
-	cl_assert_equal_i((int)strlen("refs/heads/master") + 1,
-		git_branch_tracking_name(NULL, 0, repo, "refs/heads/track-local"));
-}
diff --git a/tests-clar/refs/branches/upstream.c b/tests-clar/refs/branches/upstream.c
new file mode 100644
index 0000000..2d0ebd2
--- /dev/null
+++ b/tests-clar/refs/branches/upstream.c
@@ -0,0 +1,130 @@
+#include "clar_libgit2.h"
+#include "refs.h"
+
+static git_repository *repo;
+static git_reference *branch, *upstream;
+
+void test_refs_branches_upstream__initialize(void)
+{
+	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
+
+	branch = NULL;
+	upstream = NULL;
+}
+
+void test_refs_branches_upstream__cleanup(void)
+{
+	git_reference_free(upstream);
+	git_reference_free(branch);
+	branch = NULL;
+
+	git_repository_free(repo);
+	repo = NULL;
+}
+
+void test_refs_branches_upstream__can_retrieve_the_remote_tracking_reference_of_a_local_branch(void)
+{
+	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
+
+	cl_git_pass(git_branch_upstream(&upstream, branch));
+
+	cl_assert_equal_s("refs/remotes/test/master", git_reference_name(upstream));
+}
+
+void test_refs_branches_upstream__can_retrieve_the_local_upstream_reference_of_a_local_branch(void)
+{
+	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/track-local"));
+
+	cl_git_pass(git_branch_upstream(&upstream, branch));
+
+	cl_assert_equal_s("refs/heads/master", git_reference_name(upstream));
+}
+
+void test_refs_branches_upstream__cannot_retrieve_a_remote_upstream_reference_from_a_non_branch(void)
+{
+	cl_git_pass(git_reference_lookup(&branch, repo, "refs/tags/e90810b"));
+
+	cl_git_fail(git_branch_upstream(&upstream, branch));
+}
+
+void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_plain_local_branch_returns_GIT_ENOTFOUND(void)
+{
+	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/subtrees"));
+
+	cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
+}
+
+void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_branch_with_no_fetchspec_returns_GIT_ENOTFOUND(void)
+{
+	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/cannot-fetch"));
+
+	cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
+}
+
+static void assert_merge_and_or_remote_key_missing(git_repository *repository, const git_commit *target, const char *entry_name)
+{
+	git_reference *branch;
+
+	cl_assert_equal_i(GIT_OBJ_COMMIT, git_object_type((git_object*)target));
+	cl_git_pass(git_branch_create(&branch, repository, entry_name, (git_commit*)target, 0));
+
+	cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
+
+	git_reference_free(branch);
+}
+
+void test_refs_branches_upstream__retrieve_a_remote_tracking_reference_from_a_branch_with_no_remote_returns_GIT_ENOTFOUND(void)
+{
+	git_reference *head;
+	git_repository *repository;
+	git_commit *target;
+
+	repository = cl_git_sandbox_init("testrepo.git");
+
+	cl_git_pass(git_repository_head(&head, repository));
+	cl_git_pass(git_reference_peel(((git_object **)&target), head, GIT_OBJ_COMMIT));
+	git_reference_free(head);
+
+	assert_merge_and_or_remote_key_missing(repository, target, "remoteless");
+	assert_merge_and_or_remote_key_missing(repository, target, "mergeless");
+	assert_merge_and_or_remote_key_missing(repository, target, "mergeandremoteless");
+
+	git_commit_free(target);
+
+	cl_git_sandbox_cleanup();
+}
+
+void test_refs_branches_upstream__set_unset_upstream(void)
+{
+	git_reference *branch;
+	git_repository *repository;
+	const char *value;
+	git_config *config;
+
+	repository = cl_git_sandbox_init("testrepo.git");
+
+	cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
+	cl_git_pass(git_branch_set_upstream(branch, "test/master"));
+
+	cl_git_pass(git_repository_config(&config, repository));
+	cl_git_pass(git_config_get_string(&value, config, "branch.test.remote"));
+	cl_assert_equal_s(value, "test");
+	cl_git_pass(git_config_get_string(&value, config, "branch.test.merge"));
+	cl_assert_equal_s(value, "refs/heads/master");
+
+	cl_git_pass(git_branch_set_upstream(branch, NULL));
+	cl_git_fail_with(git_config_get_string(&value, config, "branch.test.merge"), GIT_ENOTFOUND);
+	cl_git_fail_with(git_config_get_string(&value, config, "branch.test.remote"), GIT_ENOTFOUND);
+
+	git_reference_free(branch);
+
+	cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/master"));
+	cl_git_pass(git_branch_set_upstream(branch, NULL));
+	cl_git_fail_with(git_config_get_string(&value, config, "branch.master.merge"), GIT_ENOTFOUND);
+	cl_git_fail_with(git_config_get_string(&value, config, "branch.master.remote"), GIT_ENOTFOUND);
+
+	git_reference_free(branch);
+
+	git_config_free(config);
+	cl_git_sandbox_cleanup();
+}
diff --git a/tests-clar/refs/branches/upstreamname.c b/tests-clar/refs/branches/upstreamname.c
new file mode 100644
index 0000000..f05607d
--- /dev/null
+++ b/tests-clar/refs/branches/upstreamname.c
@@ -0,0 +1,42 @@
+#include "clar_libgit2.h"
+#include "branch.h"
+
+static git_repository *repo;
+static git_buf upstream_name;
+
+void test_refs_branches_upstreamname__initialize(void)
+{
+	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
+
+	git_buf_init(&upstream_name, 0);
+}
+
+void test_refs_branches_upstreamname__cleanup(void)
+{
+	git_buf_free(&upstream_name);
+
+	git_repository_free(repo);
+	repo = NULL;
+}
+
+void test_refs_branches_upstreamname__can_retrieve_the_remote_tracking_reference_name_of_a_local_branch(void)
+{
+	cl_git_pass(git_branch_upstream__name(
+		&upstream_name, repo, "refs/heads/master"));
+
+	cl_assert_equal_s("refs/remotes/test/master", git_buf_cstr(&upstream_name));
+}
+
+void test_refs_branches_upstreamname__can_retrieve_the_local_upstream_reference_name_of_a_local_branch(void)
+{
+	cl_git_pass(git_branch_upstream__name(
+		&upstream_name, repo, "refs/heads/track-local"));
+
+	cl_assert_equal_s("refs/heads/master", git_buf_cstr(&upstream_name));
+}
+
+void test_refs_branches_upstreamname__can_return_the_size_of_thelocal_upstream_reference_name_of_a_local_branch(void)
+{
+	cl_assert_equal_i((int)strlen("refs/heads/master") + 1,
+		git_branch_upstream_name(NULL, 0, repo, "refs/heads/track-local"));
+}