PyObject * Branch_remote_name__get__(Branch *self) { int err; const char *branch_name; char *c_name = NULL; PyObject *py_name; CHECK_REFERENCE(self); branch_name = git_reference_name(self->reference); /* Get the length of the remote name */ err = git_branch_remote_name(NULL, 0, self->repo->repo, branch_name); if (err < GIT_OK) return Error_set(err); /* Get the actual remote name */ c_name = calloc(err, sizeof(char)); if (c_name == NULL) return PyErr_NoMemory(); err = git_branch_remote_name(c_name, err * sizeof(char), self->repo->repo, branch_name); if (err < GIT_OK) { free(c_name); return Error_set(err); } py_name = to_unicode(c_name, NULL, NULL); free(c_name); return py_name; }
void test_refs_branches_remote__insufficient_buffer_returns_error(void) { char remotename[1024] = {0}; cl_assert_equal_i(expected_remote_name_length, git_branch_remote_name(NULL, 0, g_repo, remote_tracking_branch_name)); cl_git_fail_with(git_branch_remote_name(remotename, expected_remote_name_length - 1, g_repo, remote_tracking_branch_name), expected_remote_name_length); }
void test_refs_branches_remote__can_get_remote_for_branch(void) { char remotename[1024] = {0}; cl_assert_equal_i(expected_remote_name_length, git_branch_remote_name(NULL, 0, g_repo, remote_tracking_branch_name)); cl_assert_equal_i(expected_remote_name_length, git_branch_remote_name(remotename, expected_remote_name_length, g_repo, remote_tracking_branch_name)); cl_assert_equal_s("test", remotename); }
void test_clone_empty__can_clone_an_empty_local_repo_barely(void) { char *local_name = "refs/heads/master"; const char *expected_tracked_branch_name = "refs/remotes/origin/master"; const char *expected_remote_name = "origin"; char buffer[1024]; git_reference *ref; cl_set_cleanup(&cleanup_repository, "./empty"); g_options.bare = true; cl_git_pass(git_clone(&g_repo_cloned, "./empty_bare.git", "./empty", &g_options)); /* Although the HEAD is orphaned... */ cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo_cloned, local_name)); /* ...one can still retrieve the name of the remote tracking reference */ cl_assert_equal_i((int)strlen(expected_tracked_branch_name) + 1, git_branch_upstream_name(buffer, 1024, g_repo_cloned, local_name)); cl_assert_equal_s(expected_tracked_branch_name, buffer); /* ...and the name of the remote... */ cl_assert_equal_i((int)strlen(expected_remote_name) + 1, git_branch_remote_name(buffer, 1024, g_repo_cloned, expected_tracked_branch_name)); cl_assert_equal_s(expected_remote_name, buffer); /* ...even when the remote HEAD is orphaned as well */ cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo_cloned, expected_tracked_branch_name)); }
void test_refs_branches_remote__local_remote_returns_error(void) { const char *local = "refs/heads/master"; cl_git_fail_with(git_branch_remote_name( NULL, 0, g_repo, local), GIT_ERROR); }
void test_refs_branches_remote__no_matching_remote_returns_error(void) { const char *unknown = "refs/remotes/nonexistent/master"; cl_git_fail_with(git_branch_remote_name( NULL, 0, g_repo, unknown), GIT_ENOTFOUND); }
void test_refs_branches_remote__ambiguous_remote_returns_error(void) { git_remote *remote; /* Create the remote */ cl_git_pass(git_remote_create(&remote, g_repo, "addtest", "http://github.com/libgit2/libgit2")); /* Update the remote fetch spec */ cl_git_pass(git_remote_set_fetchspec(remote, "refs/heads/*:refs/remotes/test/*")); cl_git_pass(git_remote_save(remote)); git_remote_free(remote); cl_git_fail_with(git_branch_remote_name(NULL, 0, g_repo, remote_tracking_branch_name), GIT_EAMBIGUOUS); }
static VALUE rb_git_branch__remote_name(VALUE rb_repo, const char *canonical_name) { git_repository *repo; git_buf remote_name = { NULL }; int error; VALUE result = Qnil; Data_Get_Struct(rb_repo, git_repository, repo); if ((error = git_branch_remote_name(&remote_name, repo, canonical_name)) == GIT_OK) result = rb_enc_str_new(remote_name.ptr, remote_name.size, rb_utf8_encoding()); git_buf_dispose(&remote_name); rugged_exception_check(error); return result; }
PyObject * Branch_remote_name__get__(Branch *self) { int err; git_buf name = {NULL}; const char *branch_name; PyObject *py_name; CHECK_REFERENCE(self); branch_name = git_reference_name(self->reference); err = git_branch_remote_name(&name, self->repo->repo, branch_name); if (err < GIT_OK) return Error_set(err); py_name = to_unicode(name.ptr, NULL, NULL); git_buf_free(&name); return py_name; }
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, error; 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 { giterr_set(GITERR_REFERENCE, "Cannot set upstream for branch '%s'", shortname); 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) error = git_buf_puts(&value, "."); else error = git_branch_remote_name(&value, repo, git_reference_name(upstream)); if (error < 0) goto on_error; 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) { git_buf_clear(&value); if (git_buf_puts(&value, git_reference_name(upstream)) < 0) goto on_error; } else { /* Get the remoe-tracking branch's refname in its repo */ if (git_remote_lookup(&remote, repo, git_buf_cstr(&value)) < 0) goto on_error; fetchspec = git_remote__matching_dst_refspec(remote, git_reference_name(upstream)); git_buf_clear(&value); if (!fetchspec || git_refspec_rtransform(&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; }