Skip to content

Commit cc68c19

Browse files
committed
Merge branch 'pr/5861'
2 parents 47dd9f4 + f2915ec commit cc68c19

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

include/git2/branch.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,19 @@ GIT_EXTERN(int) git_branch_remote_name(
304304
*/
305305
GIT_EXTERN(int) git_branch_upstream_remote(git_buf *buf, git_repository *repo, const char *refname);
306306

307+
/**
308+
* Retrieve the upstream merge of a local branch
309+
*
310+
* This will return the currently configured "branch.*.merge" for a given
311+
* branch. This branch must be local.
312+
*
313+
* @param buf the buffer into which to write the name
314+
* @param repo the repository in which to look
315+
* @param refname the full name of the branch
316+
* @return 0 or an error code
317+
*/
318+
GIT_EXTERN(int) git_branch_upstream_merge(git_buf *buf, git_repository *repo, const char *refname);
319+
307320
/**
308321
* Determine whether a branch name is valid, meaning that (when prefixed
309322
* with `refs/heads/`) that it is a valid reference name, and that any

src/branch.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ int git_branch_upstream_name(
468468
return error;
469469
}
470470

471-
int git_branch_upstream_remote(git_buf *buf, git_repository *repo, const char *refname)
471+
static int git_branch_upstream_with_format(git_buf *buf, git_repository *repo, const char *refname, const char *format, const char *format_name)
472472
{
473473
int error;
474474
git_config *cfg;
@@ -480,18 +480,28 @@ int git_branch_upstream_remote(git_buf *buf, git_repository *repo, const char *r
480480
return error;
481481

482482
if ((error = git_buf_sanitize(buf)) < 0 ||
483-
(error = retrieve_upstream_configuration(buf, cfg, refname, "branch.%s.remote")) < 0)
483+
(error = retrieve_upstream_configuration(buf, cfg, refname, format)) < 0)
484484
return error;
485485

486486
if (git_buf_len(buf) == 0) {
487-
git_error_set(GIT_ERROR_REFERENCE, "branch '%s' does not have an upstream remote", refname);
487+
git_error_set(GIT_ERROR_REFERENCE, "branch '%s' does not have an upstream %s", refname, format_name);
488488
error = GIT_ENOTFOUND;
489489
git_buf_clear(buf);
490490
}
491491

492492
return error;
493493
}
494494

495+
int git_branch_upstream_remote(git_buf *buf, git_repository *repo, const char *refname)
496+
{
497+
return git_branch_upstream_with_format(buf, repo, refname, "branch.%s.remote", "remote");
498+
}
499+
500+
int git_branch_upstream_merge(git_buf *buf, git_repository *repo, const char *refname)
501+
{
502+
return git_branch_upstream_with_format(buf, repo, refname, "branch.%s.merge", "merge");
503+
}
504+
495505
int git_branch_remote_name(git_buf *buf, git_repository *repo, const char *refname)
496506
{
497507
git_strarray remote_list = {0};

tests/refs/branches/upstream.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,29 @@ void test_refs_branches_upstream__upstream_remote(void)
7171
git_buf_dispose(&buf);
7272
}
7373

74+
void test_refs_branches_upstream__upstream_merge(void)
75+
{
76+
git_reference *branch;
77+
git_repository *repository;
78+
git_buf buf = GIT_BUF_INIT;
79+
80+
repository = cl_git_sandbox_init("testrepo.git");
81+
82+
/* check repository */
83+
cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
84+
cl_git_pass(git_branch_set_upstream(branch, "test/master"));
85+
86+
assert_config_entry_value(repository, "branch.test.remote", "test");
87+
assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
88+
89+
git_reference_free(branch);
90+
91+
/* check merge branch */
92+
cl_git_pass(git_branch_upstream_merge(&buf, repository, "refs/heads/test"));
93+
cl_assert_equal_s("refs/heads/master", buf.ptr);
94+
git_buf_dispose(&buf);
95+
}
96+
7497
void test_refs_branches_upstream__upstream_remote_empty_value(void)
7598
{
7699
git_repository *repository;

0 commit comments

Comments
 (0)