void test_merge_workdir_analysis__no_fastforward_with_config_ffonly(void) { git_config *config; git_merge_analysis_t merge_analysis; git_merge_preference_t merge_pref; git_repository_config(&config, repo); git_config_set_string(config, "merge.ff", "only"); analysis_from_branch(&merge_analysis, &merge_pref, NOFASTFORWARD_BRANCH); cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, (merge_analysis & GIT_MERGE_ANALYSIS_NORMAL)); cl_assert_equal_i(GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY, (merge_pref & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY)); }
/* this should correctly initialize both the local and remote * repository for the Subsurface cloud storage */ static git_repository *create_and_push_remote(const char *localdir, const char *remote, const char *branch) { git_repository *repo; git_config *conf; int len; char *variable_name, *merge_head; if (verbose) fprintf(stderr, "git storage: create and push remote\n"); /* first make sure the directory for the local cache exists */ subsurface_mkdir(localdir); /* set up the origin to point to our remote */ git_repository_init_options init_opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; init_opts.origin_url = remote; /* now initialize the repository with */ git_repository_init_ext(&repo, localdir, &init_opts); /* create a config so we can set the remote tracking branch */ git_repository_config(&conf, repo); len = sizeof("branch..remote") + strlen(branch); variable_name = malloc(len); snprintf(variable_name, len, "branch.%s.remote", branch); git_config_set_string(conf, variable_name, "origin"); /* we know this is shorter than the previous one, so we reuse the variable*/ snprintf(variable_name, len, "branch.%s.merge", branch); len = sizeof("refs/heads/") + strlen(branch); merge_head = malloc(len); snprintf(merge_head, len, "refs/heads/%s", branch); git_config_set_string(conf, variable_name, merge_head); /* finally create an empty commit and push it to the remote */ if (do_git_save(repo, branch, remote, false, true)) return NULL; return(repo); }
static int repository_create_cb(git_repository **out, const char *path, int bare, void *payload) { char *proxy_string; git_config *conf; int ret = git_repository_init(out, path, bare); if (getProxyString(&proxy_string)) { git_repository_config(&conf, *out); git_config_set_string(conf, "http.proxy", proxy_string); free(proxy_string); } return ret; }
void test_config_write__write_subsection(void) { git_config *cfg; const char *str; cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_set_string(cfg, "my.own.var", "works")); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_get_string(&str, cfg, "my.own.var")); cl_git_pass(strcmp(str, "works")); git_config_free(cfg); }
void test_network_remote_remotes__list(void) { git_strarray list; git_config *cfg; cl_git_pass(git_remote_list(&list, _repo)); cl_assert(list.count == 5); git_strarray_free(&list); cl_git_pass(git_repository_config(&cfg, _repo)); /* Create a new remote */ cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com")); /* Update a remote (previously without any url/pushurl entry) */ cl_git_pass(git_config_set_string(cfg, "remote.no-remote-url.pushurl", "http://example.com")); cl_git_pass(git_remote_list(&list, _repo)); cl_assert(list.count == 7); git_strarray_free(&list); git_config_free(cfg); }
void test_merge_workdir_analysis__fastforward_with_config_noff(void) { git_config *config; git_merge_analysis_t merge_analysis; git_merge_preference_t merge_pref; git_repository_config(&config, repo); git_config_set_string(config, "merge.ff", "false"); analysis_from_branch(&merge_analysis, &merge_pref, NULL, FASTFORWARD_BRANCH); cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL|GIT_MERGE_ANALYSIS_FASTFORWARD, merge_analysis); cl_assert_equal_i(GIT_MERGE_PREFERENCE_NO_FASTFORWARD, (merge_pref & GIT_MERGE_PREFERENCE_NO_FASTFORWARD)); }
TEST(libgit2, TGitPatches) { CAutoTempDir tempdir; git_repository_init_options options = GIT_REPOSITORY_INIT_OPTIONS_INIT; options.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE; CAutoRepository repo; ASSERT_EQ(0, git_repository_init_ext(repo.GetPointer(), CUnicodeUtils::GetUTF8(tempdir.GetTempDir()), &options)); CAutoConfig config; ASSERT_EQ(0, git_repository_config(config.GetPointer(), repo)); EXPECT_EQ(0, git_config_set_string(config, "core.autocrlf", "true")); EXPECT_EQ(0, git_config_set_string(config, "core.safecrlf", "true")); CAutoRepository repo2(tempdir.GetTempDir()); ASSERT_TRUE(repo2.IsValid()); CAutoIndex index; ASSERT_EQ(0, git_repository_index(index.GetPointer(), repo2)); CString testFile = tempdir.GetTempDir() + L"\\safecrlf-failure.txt"; EXPECT_TRUE(CStringUtils::WriteStringToTextFile(testFile, L"crlf\r\ncrlf\r\n")); EXPECT_EQ(0, git_index_add_bypath(index, "safecrlf-failure.txt")); EXPECT_TRUE(CStringUtils::WriteStringToTextFile(testFile, L"lf\nlf\n")); EXPECT_EQ(-1, git_index_add_bypath(index, "safecrlf-failure.txt")); EXPECT_TRUE(CStringUtils::WriteStringToTextFile(testFile, L"crlf\r\ncr\rcrlf\r\n")); EXPECT_EQ(0, git_index_add_bypath(index, "safecrlf-failure.txt")); EXPECT_EQ(0, git_config_set_string(config, "core.autocrlf", "input")); CAutoRepository repo3(tempdir.GetTempDir()); ASSERT_TRUE(repo3.IsValid()); ASSERT_EQ(0, git_repository_index(index.GetPointer(), repo3)); EXPECT_TRUE(CStringUtils::WriteStringToTextFile(testFile, L"crlf\r\ncrlf\r\n")); EXPECT_EQ(-1, git_index_add_bypath(index, "safecrlf-failure.txt")); }
void test_config_write__to_file_with_only_comment(void) { git_config *cfg; const char *filename = "config-file"; git_buf result = GIT_BUF_INIT; cl_git_mkfile(filename, "\n\n"); cl_git_pass(git_config_open_ondisk(&cfg, filename)); cl_git_pass(git_config_set_string(cfg, "section.name", "value")); git_config_free(cfg); cl_git_pass(git_futils_readbuffer(&result, "config-file")); cl_assert_equal_s("\n\n[section]\n\tname = value\n", result.ptr); git_buf_free(&result); }
void test_config_write__write_subsection(void) { git_config *cfg; git_buf buf = GIT_BUF_INIT; cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_set_string(cfg, "my.own.var", "works")); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "my.own.var")); cl_assert_equal_s("works", git_buf_cstr(&buf)); git_buf_free(&buf); git_config_free(cfg); }
void test_config_write__escape_value(void) { git_config *cfg; const char* str; cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes and \t")); cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); cl_assert_equal_s(str, "this \"has\" quotes and \t"); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); cl_assert_equal_s(str, "this \"has\" quotes and \t"); git_config_free(cfg); }
void test_checkout_conflict__initialize(void) { git_config *cfg; g_repo = cl_git_sandbox_init(TEST_REPO_PATH); git_repository_index(&g_index, g_repo); cl_git_rewritefile( TEST_REPO_PATH "/.gitattributes", "* text eol=lf\n"); /* Ensure that the user's merge.conflictstyle doesn't interfere */ cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge")); git_config_free(cfg); }
void test_network_remotes__list(void) { git_strarray list; git_config *cfg; cl_git_pass(git_remote_list(&list, _repo)); cl_assert(list.count == 1); git_strarray_free(&list); cl_git_pass(git_repository_config(&cfg, _repo)); cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com")); cl_git_pass(git_remote_list(&list, _repo)); cl_assert(list.count == 2); git_strarray_free(&list); git_config_free(cfg); }
void test_config_write__can_set_an_empty_value(void) { git_repository *repository; git_config *config; git_buf buf = {0}; repository = cl_git_sandbox_init("testrepo.git"); cl_git_pass(git_repository_config(&config, repository)); cl_git_pass(git_config_set_string(config, "core.somevar", "")); cl_git_pass(git_config_get_string_buf(&buf, config, "core.somevar")); cl_assert_equal_s("", buf.ptr); git_buf_free(&buf); git_config_free(config); cl_git_sandbox_cleanup(); }
void test_config_readonly__writing_to_cfg_with_ro_precedence_succeeds(void) { git_config_backend *backend; cl_git_pass(git_config_backend_from_file(&backend, "local")); backend->readonly = 1; cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_LOCAL, NULL, 0)); cl_git_pass(git_config_backend_from_file(&backend, "global")); cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); cl_git_pass(git_config_set_string(cfg, "foo.bar", "baz")); cl_assert(!git_path_exists("local")); cl_assert(git_path_exists("global")); cl_git_pass(p_unlink("global")); }
void test_config_write__escape_value(void) { git_config *cfg; git_buf buf = GIT_BUF_INIT; cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes and \t")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar")); cl_assert_equal_s("this \"has\" quotes and \t", git_buf_cstr(&buf)); git_buf_clear(&buf); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar")); cl_assert_equal_s("this \"has\" quotes and \t", git_buf_cstr(&buf)); git_buf_free(&buf); git_config_free(cfg); }
void test_network_remoterename__renaming_a_remote_notifies_of_non_default_fetchrefspec(void) { git_config *config; char *expected_refspecs[] = { "+refs/*:refs/*", NULL }; git_remote_free(_remote); cl_git_pass(git_repository_config__weakptr(&config, _repo)); cl_git_pass(git_config_set_string(config, "remote.test.fetch", "+refs/*:refs/*")); cl_git_pass(git_remote_load(&_remote, _repo, "test")); cl_git_pass(git_remote_rename(_remote, "just/renamed", ensure_refspecs, &expected_refspecs)); assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/*:refs/*"); }
/* * This test exposes a bug where duplicate section headers could cause * config_write to add a new entry when one already exists. */ void test_config_write__add_value_with_duplicate_header(void) { const char *file_name = "config-duplicate-insert"; const char *entry_name = "foo.c"; const char *old_val = "old"; const char *new_val = "new"; const char *str; git_config *cfg, *snapshot; /* c = old should be replaced by c = new. * The bug causes c = new to be inserted under the first 'foo' header. */ const char *file_content = "[foo]\n" \ " a = b\n" \ "[other]\n" \ " a = b\n" \ "[foo]\n" \ " c = old\n"; /* Write the test config */ cl_git_mkfile(file_name, file_content); cl_git_pass(git_config_open_ondisk(&cfg, file_name)); /* make sure the expected entry (foo.c) exists */ cl_git_pass(git_config_snapshot(&snapshot, cfg)); cl_git_pass(git_config_get_string(&str, snapshot, entry_name)); cl_assert_equal_s(old_val, str); git_config_free(snapshot); /* Try setting foo.c to something else */ cl_git_pass(git_config_set_string(cfg, entry_name, new_val)); git_config_free(cfg); /* Reopen the file and make sure the new value was set */ cl_git_pass(git_config_open_ondisk(&cfg, file_name)); cl_git_pass(git_config_snapshot(&snapshot, cfg)); cl_git_pass(git_config_get_string(&str, snapshot, entry_name)); cl_assert_equal_s(new_val, str); /* Cleanup */ git_config_free(snapshot); git_config_free(cfg); }
void test_merge_driver__initialize(void) { git_config *cfg; repo = cl_git_sandbox_init(TEST_REPO_PATH); git_repository_index(&repo_index, repo); git_oid_fromstr(&automergeable_id, AUTOMERGEABLE_IDSTR); /* Ensure that the user's merge.conflictstyle doesn't interfere */ cl_git_pass(git_repository_config(&cfg, repo)); cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge")); cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", false)); test_drivers_register(); git_config_free(cfg); }
BOOL CFirstStartWizardAuthentication::OnWizardFinish() { UpdateData(); CString sshclient = CRegString(L"Software\\TortoiseGit\\SSH"); if (sshclient.IsEmpty()) sshclient = CRegString(L"Software\\TortoiseGit\\SSH", L"", FALSE, HKEY_LOCAL_MACHINE); if (m_ctrlSSHClient.GetCurSel() == 0 && !(sshclient.IsEmpty() || IsTool(L"tortoisegitplink", sshclient) || IsTool(L"tortoiseplink", sshclient))) CRegString(L"Software\\TortoiseGit\\SSH") = CPathUtils::GetAppDirectory() + L"TortoiseGitPlink.exe"; else if (m_ctrlSSHClient.GetCurSel() == 1 && !IsTool(L"ssh", sshclient)) CRegString(L"Software\\TortoiseGit\\SSH") = L"ssh.exe"; if (m_ctrlSimpleCredential.IsWindowEnabled() && !m_bNoSave && m_ctrlSimpleCredential.GetCurSel() != -1) { CAutoConfig config(true); int err = git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitGlobalXDGConfig()), GIT_CONFIG_LEVEL_XDG, nullptr, FALSE); if (!err && (PathFileExists(g_Git.GetGitGlobalConfig()) || !PathFileExists(g_Git.GetGitGlobalXDGConfig()))) err = git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitGlobalConfig()), GIT_CONFIG_LEVEL_GLOBAL, nullptr, FALSE); if (err) { MessageBox(g_Git.GetLibGit2LastErr(), L"TortoiseGit", MB_ICONEXCLAMATION); return FALSE; } if (m_ctrlSimpleCredential.GetCurSel() == 0 && m_availableHelpers.at(0) == CString(MAKEINTRESOURCE(IDS_NONE))) { int ret = git_config_delete_entry(config, "credential.helper"); if (ret != 0 && ret != GIT_ENOTFOUND) { MessageBox(g_Git.GetLibGit2LastErr(), L"TortoiseGit", MB_ICONEXCLAMATION); return FALSE; } } else if (git_config_set_string(config, "credential.helper", CUnicodeUtils::GetUTF8(m_availableHelpers.at(m_ctrlSimpleCredential.GetCurSel())))) { MessageBox(g_Git.GetLibGit2LastErr(), L"TortoiseGit", MB_ICONEXCLAMATION); return FALSE; } } return __super::OnWizardFinish(); }
void test_network_remote_rename__renaming_a_remote_notifies_of_non_default_fetchrefspec(void) { git_config *config; git_remote *remote; git_strarray problems = {0}; cl_git_pass(git_repository_config__weakptr(&config, _repo)); cl_git_pass(git_config_set_string(config, "remote.test.fetch", "+refs/*:refs/*")); cl_git_pass(git_remote_lookup(&remote, _repo, "test")); git_remote_free(remote); cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed")); cl_assert_equal_i(1, problems.count); cl_assert_equal_s("+refs/*:refs/*", problems.strings[0]); git_strarray_free(&problems); assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/*:refs/*"); git_strarray_free(&problems); }
static int repository_create_cb(git_repository **out, const char *path, int bare, void *payload) { char *proxy_string; git_config *conf; int ret = git_repository_init(out, path, bare); git_repository_config(&conf, *out); if (getProxyString(&proxy_string)) { if (verbose) fprintf(stderr, "set proxy to \"%s\"\n", proxy_string); git_config_set_string(conf, "http.proxy", proxy_string); free(proxy_string); } else { if (verbose) fprintf(stderr, "delete proxy setting\n"); git_config_delete_entry(conf, "http.proxy"); } return ret; }
void test_merge_workdir_simple__merge_overrides_config(void) { git_config *config; git_buf conflicting_buf = GIT_BUF_INIT; struct merge_index_entry merge_index_entries[] = { ADDED_IN_MASTER_INDEX_ENTRY, AUTOMERGEABLE_INDEX_ENTRY, CHANGED_IN_BRANCH_INDEX_ENTRY, CHANGED_IN_MASTER_INDEX_ENTRY, { 0100644, "d427e0b2e138501a3d15cc376077a3631e15bd46", 1, "conflicting.txt" }, { 0100644, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 2, "conflicting.txt" }, { 0100644, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", 3, "conflicting.txt" }, UNCHANGED_INDEX_ENTRY, }; struct merge_reuc_entry merge_reuc_entries[] = { AUTOMERGEABLE_REUC_ENTRY, REMOVED_IN_BRANCH_REUC_ENTRY, REMOVED_IN_MASTER_REUC_ENTRY }; cl_git_pass(git_repository_config(&config, repo)); cl_git_pass(git_config_set_string(config, "merge.conflictstyle", "diff3")); set_core_autocrlf_to(repo, false); merge_simple_branch(0, GIT_CHECKOUT_CONFLICT_STYLE_MERGE); cl_git_pass(git_futils_readbuffer(&conflicting_buf, TEST_REPO_PATH "/conflicting.txt")); cl_assert(strcmp(conflicting_buf.ptr, CONFLICTING_MERGE_FILE) == 0); git_buf_dispose(&conflicting_buf); cl_assert(merge_test_index(repo_index, merge_index_entries, 8)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 3)); git_config_free(config); }
void test_config_global__open_programdata(void) { git_config *cfg; git_repository *repo; git_buf config_path = GIT_BUF_INIT; git_buf var_contents = GIT_BUF_INIT; if (cl_is_env_set("GITTEST_INVASIVE_FS_STRUCTURE")) cl_skip(); cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_PROGRAMDATA, &config_path)); if (!git_path_isdir(config_path.ptr)) cl_git_pass(p_mkdir(config_path.ptr, 0777)); cl_git_pass(git_buf_puts(&config_path, "/config")); cl_git_pass(git_config_open_ondisk(&cfg, config_path.ptr)); cl_git_pass(git_config_set_string(cfg, "programdata.var", "even higher level")); git_buf_free(&config_path); git_config_free(cfg); git_config_open_default(&cfg); cl_git_pass(git_config_get_string_buf(&var_contents, cfg, "programdata.var")); cl_assert_equal_s("even higher level", var_contents.ptr); git_config_free(cfg); git_buf_free(&var_contents); cl_git_pass(git_repository_init(&repo, "./foo.git", true)); cl_git_pass(git_repository_config(&cfg, repo)); cl_git_pass(git_config_get_string_buf(&var_contents, cfg, "programdata.var")); cl_assert_equal_s("even higher level", var_contents.ptr); git_config_free(cfg); git_buf_free(&var_contents); git_repository_free(repo); cl_fixture_cleanup("./foo.git"); }
static void StorePuttyKey(const CString& repoRoot, const CString& remote, const CString& keyFile) { CAutoRepository repo(repoRoot); CAutoConfig config; CString configName; if (!repo) goto error; if (git_repository_config(config.GetPointer(), repo)) goto error; configName.Format(_T("remote.%s.puttykeyfile"), (LPCTSTR)remote); if (git_config_set_string(config, CUnicodeUtils::GetUTF8(configName), CUnicodeUtils::GetUTF8(keyFile))) goto error; return; error: MessageBox(hWndExplorer, CGit::GetLibGit2LastErr(L"Could not open repository"), _T("TortoiseGit"), MB_ICONERROR); }
void test_config_global__open_xdg(void) { git_config *cfg, *xdg, *selected; const char *str = "teststring"; const char *key = "this.variable"; git_buf buf = {0}; cl_git_mkfile("xdg/git/config", "# XDG config\n[core]\n test = 1\n"); cl_git_pass(git_config_open_default(&cfg)); cl_git_pass(git_config_open_level(&xdg, cfg, GIT_CONFIG_LEVEL_XDG)); cl_git_pass(git_config_open_global(&selected, cfg)); cl_git_pass(git_config_set_string(xdg, key, str)); cl_git_pass(git_config_get_string_buf(&buf, selected, key)); cl_assert_equal_s(str, buf.ptr); git_buf_free(&buf); git_config_free(selected); git_config_free(xdg); git_config_free(cfg); }
void test_config_write__add_value_at_specific_level(void) { git_config *cfg, *cfg_specific; int i; int64_t l, expected = +9223372036854775803; git_buf buf = GIT_BUF_INIT; // open config15 as global level config file cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_add_file_ondisk(cfg, "config9", GIT_CONFIG_LEVEL_LOCAL, 0)); cl_git_pass(git_config_add_file_ondisk(cfg, "config15", GIT_CONFIG_LEVEL_GLOBAL, 0)); cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL)); cl_git_pass(git_config_set_int32(cfg_specific, "core.int32global", 28)); cl_git_pass(git_config_set_int64(cfg_specific, "core.int64global", expected)); cl_git_pass(git_config_set_bool(cfg_specific, "core.boolglobal", true)); cl_git_pass(git_config_set_string(cfg_specific, "core.stringglobal", "I'm a global config value!")); git_config_free(cfg_specific); git_config_free(cfg); // open config15 as local level config file cl_git_pass(git_config_open_ondisk(&cfg, "config15")); cl_git_pass(git_config_get_int32(&i, cfg, "core.int32global")); cl_assert_equal_i(28, i); cl_git_pass(git_config_get_int64(&l, cfg, "core.int64global")); cl_assert(l == expected); cl_git_pass(git_config_get_bool(&i, cfg, "core.boolglobal")); cl_assert_equal_b(true, i); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.stringglobal")); cl_assert_equal_s("I'm a global config value!", git_buf_cstr(&buf)); git_buf_free(&buf); git_config_free(cfg); }
void test_repo_config__can_open_global_when_there_is_no_file(void) { git_repository *repo; git_config *config, *global; cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr)); cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); cl_git_pass(git_repository_config(&config, repo)); cl_git_pass(git_config_open_level( &global, config, GIT_CONFIG_LEVEL_GLOBAL)); cl_git_pass(git_config_set_string(global, "test.set", "42")); git_config_free(global); git_config_free(config); git_repository_free(repo); }
void test_repo_open__no_config(void) { git_buf path = GIT_BUF_INIT; git_repository *repo; git_config *config; cl_fixture_sandbox("empty_standard_repo"); cl_git_pass(cl_rename( "empty_standard_repo/.gitted", "empty_standard_repo/.git")); /* remove local config */ cl_git_pass(git_futils_rmdir_r( "empty_standard_repo/.git/config", NULL, GIT_RMDIR_REMOVE_FILES)); /* isolate from system level configs */ cl_must_pass(p_mkdir("alternate", 0777)); cl_git_pass(git_path_prettify(&path, "alternate", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr)); git_buf_free(&path); cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); cl_git_pass(git_repository_config(&config, repo)); cl_git_pass(git_config_set_string(config, "test.set", "42")); git_config_free(config); git_repository_free(repo); cl_fixture_cleanup("empty_standard_repo"); cl_sandbox_set_search_path_defaults(); }
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; }
void test_repo_init__init_with_initial_commit(void) { git_index *index; cl_set_cleanup(&cleanup_repository, "committed"); /* Initialize the repository */ cl_git_pass(git_repository_init(&_repo, "committed", 0)); /* Index will be automatically created when requested for a new repo */ cl_git_pass(git_repository_index(&index, _repo)); /* Create a file so we can commit it * * If you are writing code outside the test suite, you can create this * file any way that you like, such as: * FILE *fp = fopen("committed/file.txt", "w"); * fputs("some stuff\n", fp); * fclose(fp); * We like to use the help functions because they do error detection * in a way that's easily compatible with our test suite. */ cl_git_mkfile("committed/file.txt", "some stuff\n"); /* Add file to the index */ cl_git_pass(git_index_add_bypath(index, "file.txt")); cl_git_pass(git_index_write(index)); /* Intentionally not using cl_repo_commit_from_index here so this code * can be used as an example of how an initial commit is typically * made to a repository... */ /* Make sure we're ready to use git_signature_default :-) */ { git_config *cfg, *local; cl_git_pass(git_repository_config(&cfg, _repo)); cl_git_pass(git_config_open_level(&local, cfg, GIT_CONFIG_LEVEL_LOCAL)); cl_git_pass(git_config_set_string(local, "user.name", "Test User")); cl_git_pass(git_config_set_string(local, "user.email", "*****@*****.**")); git_config_free(local); git_config_free(cfg); } /* Create a commit with the new contents of the index */ { git_signature *sig; git_oid tree_id, commit_id; git_tree *tree; cl_git_pass(git_signature_default(&sig, _repo)); cl_git_pass(git_index_write_tree(&tree_id, index)); cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id)); cl_git_pass(git_commit_create_v( &commit_id, _repo, "HEAD", sig, sig, NULL, "First", tree, 0)); git_tree_free(tree); git_signature_free(sig); } git_index_free(index); }