Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 02bc523

Browse files
committed
Merge pull request libgit2#2698 from libgit2/cmn/fetchhead-refactor
Refactor fetchhead
2 parents bc8c4a8 + 2c9b9c8 commit 02bc523

File tree

5 files changed

+90
-25
lines changed

5 files changed

+90
-25
lines changed

include/git2/branch.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,17 @@ GIT_EXTERN(int) git_branch_remote_name(
260260
git_repository *repo,
261261
const char *canonical_branch_name);
262262

263+
264+
/**
265+
* Retrieve the name fo the upstream remote of a local branch
266+
*
267+
* @param buf the buffer into which to write the name
268+
* @param repo the repository in which to look
269+
* @param refname the full name of the branch
270+
* @return 0 or an error code
271+
*/
272+
GIT_EXTERN(int) git_branch_upstream_remote(git_buf *buf, git_repository *repo, const char *refname);
273+
263274
/** @} */
264275
GIT_END_DECL
265276
#endif

src/branch.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,29 @@ int git_branch_upstream_name(
384384
return error;
385385
}
386386

387+
int git_branch_upstream_remote(git_buf *buf, git_repository *repo, const char *refname)
388+
{
389+
int error;
390+
const char *str;
391+
git_config *cfg;
392+
393+
if (!git_reference__is_branch(refname))
394+
return not_a_local_branch(refname);
395+
396+
git_buf_sanitize(buf);
397+
if ((error = git_repository_config_snapshot(&cfg, repo)) < 0)
398+
return error;
399+
400+
if ((error = retrieve_upstream_configuration(&str, cfg, refname, "branch.%s.remote")) < 0)
401+
goto cleanup;
402+
403+
error = git_buf_puts(buf, str);
404+
405+
cleanup:
406+
git_config_free(cfg);
407+
return error;
408+
}
409+
387410
int git_branch_remote_name(git_buf *buf, git_repository *repo, const char *refname)
388411
{
389412
git_strarray remote_list = {0};

src/remote.c

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -930,23 +930,50 @@ static int remote_head_for_fetchspec_src(git_remote_head **out, git_vector *upda
930930
return 0;
931931
}
932932

933+
static int ref_to_update(int *update, git_buf *remote_name, git_remote *remote, git_refspec *spec, const char *ref_name)
934+
{
935+
int error = 0;
936+
git_repository *repo;
937+
git_buf upstream_remote = GIT_BUF_INIT;
938+
git_buf upstream_name = GIT_BUF_INIT;
939+
940+
repo = git_remote_owner(remote);
941+
942+
if ((!git_reference__is_branch(ref_name)) ||
943+
!git_remote_name(remote) ||
944+
(error = git_branch_upstream_remote(&upstream_remote, repo, ref_name) < 0) ||
945+
git__strcmp(git_remote_name(remote), git_buf_cstr(&upstream_remote)) ||
946+
(error = git_branch_upstream_name(&upstream_name, repo, ref_name)) < 0 ||
947+
!git_refspec_dst_matches(spec, git_buf_cstr(&upstream_name)) ||
948+
(error = git_refspec_rtransform(remote_name, spec, upstream_name.ptr)) < 0) {
949+
/* Not an error if there is no upstream */
950+
if (error == GIT_ENOTFOUND) {
951+
giterr_clear();
952+
error = 0;
953+
}
954+
955+
*update = 0;
956+
} else {
957+
*update = 1;
958+
}
959+
960+
git_buf_free(&upstream_remote);
961+
git_buf_free(&upstream_name);
962+
return error;
963+
}
964+
933965
static int remote_head_for_ref(git_remote_head **out, git_remote *remote, git_refspec *spec, git_vector *update_heads, git_reference *ref)
934966
{
935967
git_reference *resolved_ref = NULL;
936968
git_buf remote_name = GIT_BUF_INIT;
937-
git_buf upstream_name = GIT_BUF_INIT;
938-
git_buf config_key = GIT_BUF_INIT;
939-
git_repository *repo;
940969
git_config *config = NULL;
941-
const char *ref_name, *branch_remote;
942-
int error = 0;
970+
const char *ref_name;
971+
int error = 0, update;
943972

944973
assert(out && spec && ref);
945974

946975
*out = NULL;
947976

948-
repo = git_reference_owner(ref);
949-
950977
error = git_reference_resolve(&resolved_ref, ref);
951978

952979
/* If we're in an unborn branch, let's pretend nothing happened */
@@ -957,29 +984,14 @@ static int remote_head_for_ref(git_remote_head **out, git_remote *remote, git_re
957984
ref_name = git_reference_name(resolved_ref);
958985
}
959986

960-
if ((!git_reference__is_branch(ref_name)) ||
961-
(error = git_repository_config_snapshot(&config, repo)) < 0 ||
962-
(error = git_buf_printf(&config_key, "branch.%s.remote",
963-
ref_name + strlen(GIT_REFS_HEADS_DIR))) < 0 ||
964-
(error = git_config_get_string(&branch_remote, config, git_buf_cstr(&config_key))) < 0 ||
965-
git__strcmp(git_remote_name(remote), branch_remote) ||
966-
(error = git_branch_upstream_name(&upstream_name, repo, ref_name)) < 0 ||
967-
!git_refspec_dst_matches(spec, git_buf_cstr(&upstream_name)) ||
968-
(error = git_refspec_rtransform(&remote_name, spec, upstream_name.ptr)) < 0) {
969-
/* Not an error if there is no upstream */
970-
if (error == GIT_ENOTFOUND)
971-
error = 0;
972-
987+
if ((error = ref_to_update(&update, &remote_name, remote, spec, ref_name)) < 0)
973988
goto cleanup;
974-
}
975989

976-
error = remote_head_for_fetchspec_src(out, update_heads, git_buf_cstr(&remote_name));
990+
if (update)
991+
error = remote_head_for_fetchspec_src(out, update_heads, git_buf_cstr(&remote_name));
977992

978993
cleanup:
979994
git_reference_free(resolved_ref);
980-
git_buf_free(&remote_name);
981-
git_buf_free(&upstream_name);
982-
git_buf_free(&config_key);
983995
git_config_free(config);
984996
return error;
985997
}

tests/network/remote/remotes.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,16 @@ static int remote_single_branch(git_remote **out, git_repository *repo, const ch
535535
return 0;
536536
}
537537

538+
void test_network_remote_remotes__fetch_from_anonymous(void)
539+
{
540+
git_remote *remote;
541+
542+
cl_git_pass(git_remote_create_anonymous(&remote, _repo, cl_fixture("testrepo.git"),
543+
"refs/heads/*:refs/other/*"));
544+
cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
545+
git_remote_free(remote);
546+
}
547+
538548
void test_network_remote_remotes__single_branch(void)
539549
{
540550
git_clone_options opts = GIT_CLONE_OPTIONS_INIT;

tests/refs/branches/upstream.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference
6161
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
6262
}
6363

64+
void test_refs_branches_upstream__upstream_remote(void)
65+
{
66+
git_buf buf = GIT_BUF_INIT;
67+
68+
cl_git_pass(git_branch_upstream_remote(&buf, repo, "refs/heads/master"));
69+
cl_assert_equal_s("test", buf.ptr);
70+
git_buf_free(&buf);
71+
}
72+
6473
static void assert_merge_and_or_remote_key_missing(git_repository *repository, const git_commit *target, const char *entry_name)
6574
{
6675
git_reference *branch;

0 commit comments

Comments
 (0)