Example #1
0
/*
 * At the beginning of the test:
 *  - config9 has: core.dummy2=42
 *  - config15 has: core.dummy2=7
 *  - config16 has: core.dummy2=28
 */
void test_config_read__local_config_overrides_global_config_overrides_system_config(void)
{
	git_config *cfg;
	int32_t i;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
		GIT_CONFIG_LEVEL_GLOBAL, NULL, 0));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"),
		GIT_CONFIG_LEVEL_LOCAL, NULL, 0));

	cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2"));
	cl_assert_equal_i(28, i);

	git_config_free(cfg);

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
		GIT_CONFIG_LEVEL_GLOBAL, NULL, 0));

	cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2"));
	cl_assert_equal_i(7, i);

	git_config_free(cfg);
}
Example #2
0
int
Config_init(Config *self, PyObject *args, PyObject *kwds)
{
    char *path = NULL;
    int err;

    if (kwds && PyDict_Size(kwds) > 0) {
        PyErr_SetString(PyExc_TypeError,
                        "Config takes no keyword arguments");
        return -1;
    }

    if (!PyArg_ParseTuple(args, "|s", &path))
        return -1;

    if (path == NULL)
        err = git_config_new(&self->config);
    else
        err = git_config_open_ondisk(&self->config, path);

    if (err < 0) {
        git_config_free(self->config);

        if (err == GIT_ENOTFOUND)
            Error_set_exc(PyExc_IOError);
        else
            Error_set(err);

        return -1;
    }

    return 0;
}
Example #3
0
/*
 * At the beginning of the test, config18 has:
 *	int32global = 28
 *	int64global = 9223372036854775803
 *	boolglobal = true
 *	stringglobal = I'm a global config value!
 *
 * And config19 has:
 *	int32global = -1
 *	int64global = -2
 *	boolglobal = false
 *	stringglobal = don't find me!
 *
 */
void test_config_read__simple_read_from_specific_level(void)
{
	git_config *cfg, *cfg_specific;
	int i;
	int64_t l, expected = +9223372036854775803;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"),
		GIT_CONFIG_LEVEL_GLOBAL, NULL, 0));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"),
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));

	cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL));

	cl_git_pass(git_config_get_int32(&i, cfg_specific, "core.int32global"));
	cl_assert_equal_i(28, i);
	cl_git_pass(git_config_get_int64(&l, cfg_specific, "core.int64global"));
	cl_assert(l == expected);
	cl_git_pass(git_config_get_bool(&i, cfg_specific, "core.boolglobal"));
	cl_assert_equal_b(true, i);
	cl_git_pass(git_config_get_string_buf(&buf, cfg_specific, "core.stringglobal"));
	cl_assert_equal_s("I'm a global config value!", git_buf_cstr(&buf));

	git_config_free(cfg_specific);
	git_config_free(cfg);
}
Example #4
0
int
Config_init(Config *self, PyObject *args, PyObject *kwds)
{
    char *path;
    int err;

    if (kwds) {
        PyErr_SetString(PyExc_TypeError,
                        "Repository takes no keyword arguments");
        return -1;
    }

    if (PySequence_Length(args) > 0) {
        if (!PyArg_ParseTuple(args, "s", &path)) {
            return -1;
        }

        err = git_config_open_ondisk(&self->config, path);

    } else {
        err = git_config_new(&self->config);
    }

    if (err < 0) {
        if (err == GIT_ENOTFOUND) {
            Error_set_exc(PyExc_IOError);
        } else {
            Error_set(err);
        }

        return -1;
    }

    return 0;
}
Example #5
0
/*
 * At the beginning of the test:
 *  - config9 has: core.dummy2=42
 *  - config15 has: core.dummy2=7
 */
void test_config_write__delete_value_at_specific_level(void)
{
	git_config *cfg, *cfg_specific;
	int32_t i;

	cl_git_pass(git_config_open_ondisk(&cfg, "config15"));
	cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2"));
	cl_assert(i == 7);
	git_config_free(cfg);

	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_delete_entry(cfg_specific, "core.dummy2"));
	git_config_free(cfg);

	cl_git_pass(git_config_open_ondisk(&cfg, "config15"));
	cl_assert(git_config_get_int32(&i, cfg, "core.dummy2") == GIT_ENOTFOUND);
	cl_git_pass(git_config_set_int32(cfg, "core.dummy2", 7));

	git_config_free(cfg_specific);
	git_config_free(cfg);
}
Example #6
0
void test_config_read__iterator(void)
{
	git_config *cfg;
	git_config_iterator *iter;
	git_config_entry *entry;
	int count, ret;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
		GIT_CONFIG_LEVEL_GLOBAL, NULL, 0));

	count = 0;
	cl_git_pass(git_config_iterator_new(&iter, cfg));

	while ((ret = git_config_next(&entry, iter)) == 0) {
		count++;
	}

	git_config_iterator_free(iter);
	cl_assert_equal_i(GIT_ITEROVER, ret);
	cl_assert_equal_i(7, count);

	count = 3;
	cl_git_pass(git_config_iterator_new(&iter, cfg));

	git_config_iterator_free(iter);
	git_config_free(cfg);
}
Example #7
0
static int load_config(
	git_config **out,
	git_repository *repo,
	const char *global_config_path,
	const char *xdg_config_path,
	const char *system_config_path)
{
	int error;
	git_buf config_path = GIT_BUF_INIT;
	git_config *cfg = NULL;

	assert(repo && out);

	if ((error = git_config_new(&cfg)) < 0)
		return error;

	error = git_buf_joinpath(
		&config_path, repo->path_repository, GIT_CONFIG_FILENAME_INREPO);
	if (error < 0)
		goto on_error;

	if ((error = git_config_add_file_ondisk(
			cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, 0)) < 0 &&
		error != GIT_ENOTFOUND)
		goto on_error;

	git_buf_free(&config_path);

	if (global_config_path != NULL &&
		(error = git_config_add_file_ondisk(
			cfg, global_config_path, GIT_CONFIG_LEVEL_GLOBAL, 0)) < 0 &&
		error != GIT_ENOTFOUND)
		goto on_error;

	if (xdg_config_path != NULL &&
		(error = git_config_add_file_ondisk(
			cfg, xdg_config_path, GIT_CONFIG_LEVEL_XDG, 0)) < 0 &&
		error != GIT_ENOTFOUND)
		goto on_error;

	if (system_config_path != NULL &&
		(error = git_config_add_file_ondisk(
			cfg, system_config_path, GIT_CONFIG_LEVEL_SYSTEM, 0)) < 0 &&
		error != GIT_ENOTFOUND)
		goto on_error;

	giterr_clear(); /* clear any lingering ENOTFOUND errors */

	*out = cfg;
	return 0;

on_error:
	git_buf_free(&config_path);
	git_config_free(cfg);
	*out = NULL;
	return error;
}
/**
 * ggit_config_new:
 *
 * Create a new config. See also ggit_config_get_default() to get
 * a #GgitConfig representing the global, XDG and system configuration files.
 * To get a #GgitConfig for a repository use #ggit_repository_get_config
 * instead.
 *
 * Returns: (transfer full): a #GgitConfig.
 *
 **/
GgitConfig *
ggit_config_new (void)
{
	git_config *config;

	git_config_new (&config);

	return _ggit_config_wrap (config);
}
Example #9
0
MAP_STRING_STRING GetBranchDescriptions()
{
	MAP_STRING_STRING descriptions;
	git_config * config;
	git_config_new(&config);
	git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitLocalConfig()), GIT_CONFIG_LEVEL_LOCAL, FALSE);
	git_config_foreach_match(config, "branch\\..*\\.description", GetBranchDescriptionsCallback, &descriptions);
	git_config_free(config);
	return descriptions;
}
Example #10
0
void test_config_configlevel__fetching_a_level_from_an_empty_compound_config_returns_ENOTFOUND(void)
{
	git_config *cfg;
	git_config *local_cfg;

	cl_git_pass(git_config_new(&cfg));

	cl_assert_equal_i(GIT_ENOTFOUND, git_config_open_level(&local_cfg, cfg, GIT_CONFIG_LEVEL_LOCAL));

	git_config_free(cfg);
}
Example #11
0
MAP_STRING_STRING GetBranchDescriptions()
{
	MAP_STRING_STRING descriptions;
	git_config * config;
	git_config_new(&config);
	CStringA projectConfigA = CUnicodeUtils::GetMulti(g_Git.GetGitLocalConfig(), CP_UTF8);
	git_config_add_file_ondisk(config, projectConfigA.GetBuffer(), 3);
	projectConfigA.ReleaseBuffer();
	git_config_foreach_match(config, "branch\\..*\\.description", GetBranchDescriptionsCallback, &descriptions);
	git_config_free(config);
	return descriptions;
}
Example #12
0
int git_config_open_ondisk(git_config **cfg, const char *path)
{
	int error;

	error = git_config_new(cfg);
	if (error < GIT_SUCCESS)
		return error;

	error = git_config_add_file_ondisk(*cfg, path, 1);
	if (error < GIT_SUCCESS)
		git_config_free(*cfg);

	return error;
}
Example #13
0
void test_config_configlevel__can_replace_a_config_file_at_an_existing_level(void)
{
	git_config *cfg;
	const char *s;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"),
		GIT_CONFIG_LEVEL_LOCAL, 1));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"),
		GIT_CONFIG_LEVEL_LOCAL, 1));

	cl_git_pass(git_config_get_string(&s, cfg, "core.stringglobal"));
	cl_assert_equal_s("don't find me!", s);

	git_config_free(cfg);
}
Example #14
0
void test_config_read__read_git_config_entry(void)
{
	git_config *cfg;
	git_config_entry *entry;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));

	cl_git_pass(git_config_get_entry(&entry, cfg, "core.dummy2"));
	cl_assert_equal_s("core.dummy2", entry->name);
	cl_assert_equal_s("42", entry->value);
	cl_assert_equal_i(GIT_CONFIG_LEVEL_SYSTEM, entry->level);

	git_config_entry_free(entry);
	git_config_free(cfg);
}
Example #15
0
void test_config_configlevel__adding_the_same_level_twice_returns_EEXISTS(void)
{
	int error;
	git_config *cfg;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
		GIT_CONFIG_LEVEL_LOCAL, 0));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
		GIT_CONFIG_LEVEL_GLOBAL, 0));
	error = git_config_add_file_ondisk(cfg, cl_fixture("config/config16"),
		GIT_CONFIG_LEVEL_GLOBAL, 0);

	cl_git_fail(error);
	cl_assert_equal_i(GIT_EEXISTS, error);

	git_config_free(cfg);
}
CString CSettingGitCredential::Load(CString key)
{
	CString cmd;

	if (m_strUrl.IsEmpty())
		cmd.Format(_T("credential.%s"), key);
	else
		cmd.Format(_T("credential.%s.%s"), m_strUrl, key);

	git_config * config;
	git_config_new(&config);
	int sel = m_ctrlConfigType.GetCurSel();
	if (sel == ConfigType::Local)
	{
		CStringA projectConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitLocalConfig());
		git_config_add_file_ondisk(config, projectConfigA.GetBuffer(), GIT_CONFIG_LEVEL_LOCAL, FALSE);
		projectConfigA.ReleaseBuffer();
	}
	else if (sel == ConfigType::Global)
	{
		CStringA globalConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalConfig());
		git_config_add_file_ondisk(config, globalConfigA.GetBuffer(), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
		globalConfigA.ReleaseBuffer();
		CStringA globalXDGConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalXDGConfig());
		git_config_add_file_ondisk(config, globalXDGConfigA.GetBuffer(), GIT_CONFIG_LEVEL_XDG, FALSE);
		globalXDGConfigA.ReleaseBuffer();
	}
	else if (sel == ConfigType::System)
	{
		CStringA systemConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitSystemConfig());
		git_config_add_file_ondisk(config, systemConfigA.GetBuffer(), GIT_CONFIG_LEVEL_SYSTEM, FALSE);
		systemConfigA.ReleaseBuffer();
	}
	
	CStringA cmdA = CUnicodeUtils::GetUTF8(cmd);
	CStringA valueA;
	const git_config_entry *entry;
	if (!git_config_get_entry(&entry, config, cmdA))
		valueA = CStringA(entry->value);
	cmdA.ReleaseBuffer();
	git_config_free(config);

	return CUnicodeUtils::GetUnicode(valueA);
}
Example #17
0
void test_config_read__parent_dir_is_file(void)
{
	git_config *cfg;
	int count;

	cl_git_pass(git_config_new(&cfg));
	/*
	 * Verify we can add non-existing files when the parent directory is not
	 * a directory.
	 */
	cl_git_pass(git_config_add_file_ondisk(cfg, "/dev/null/.gitconfig",
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));

	count = 0;
	cl_git_pass(git_config_foreach(cfg, count_cfg_entries_and_compare_levels, &count));
	cl_assert_equal_i(0, count);

	git_config_free(cfg);
}
Example #18
0
static int load_config(
	git_config **out,
	git_repository *repo,
	const char *global_config_path,
	const char *system_config_path)
{
	git_buf config_path = GIT_BUF_INIT;
	git_config *cfg = NULL;

	assert(repo && out);

	if (git_config_new(&cfg) < 0)
		return -1;

	if (git_buf_joinpath(
		&config_path, repo->path_repository, GIT_CONFIG_FILENAME_INREPO) < 0)
		goto on_error;

	if (git_config_add_file_ondisk(cfg, config_path.ptr, 3) < 0)
		goto on_error;

	git_buf_free(&config_path);

	if (global_config_path != NULL) {
		if (git_config_add_file_ondisk(cfg, global_config_path, 2) < 0)
			goto on_error;
	}

	if (system_config_path != NULL) {
		if (git_config_add_file_ondisk(cfg, system_config_path, 1) < 0)
			goto on_error;
	}

	*out = cfg;
	return 0;

on_error:
	git_buf_free(&config_path);
	git_config_free(cfg);
	*out = NULL;
	return -1;
}
Example #19
0
void test_odb_backend_nobackend__initialize(void)
{
	git_config *config;
	git_odb *odb;
	git_refdb *refdb;

	cl_git_pass(git_repository_new(&_repo));
	cl_git_pass(git_config_new(&config));
	cl_git_pass(git_odb_new(&odb));
	cl_git_pass(git_refdb_new(&refdb, _repo));

	git_repository_set_config(_repo, config);
	git_repository_set_odb(_repo, odb);
	git_repository_set_refdb(_repo, refdb);

	/* The set increases the refcount and we don't want them anymore */
	git_config_free(config);
	git_odb_free(odb);
	git_refdb_free(refdb);
}
Example #20
0
/*
 * At the beginning of the test:
 *  - config9 has: core.global does not exist
 *  - config15 has: core.global=17
 *  - config16 has: core.global=29
 *
 * And also:
 *  - config9 has: core.system does not exist
 *  - config15 has: core.system does not exist
 *  - config16 has: core.system=11
 */
void test_config_read__fallback_from_local_to_global_and_from_global_to_system(void)
{
	git_config *cfg;
	int32_t i;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
		GIT_CONFIG_LEVEL_GLOBAL, NULL, 0));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"),
		GIT_CONFIG_LEVEL_LOCAL, NULL, 0));

	cl_git_pass(git_config_get_int32(&i, cfg, "core.global"));
	cl_assert_equal_i(17, i);
	cl_git_pass(git_config_get_int32(&i, cfg, "core.system"));
	cl_assert_equal_i(11, i);

	git_config_free(cfg);
}
Example #21
0
void test_config_configlevel__can_read_from_a_single_level_focused_file_after_parent_config_has_been_freed(void)
{
	git_config *cfg;
	git_config *single_level_cfg;
	const char *s;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"),
		GIT_CONFIG_LEVEL_GLOBAL, 0));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"),
		GIT_CONFIG_LEVEL_LOCAL, 0));

	cl_git_pass(git_config_open_level(&single_level_cfg, cfg, GIT_CONFIG_LEVEL_LOCAL));

	git_config_free(cfg);

	cl_git_pass(git_config_get_string(&s, single_level_cfg, "core.stringglobal"));
	cl_assert_equal_s("don't find me!", s);

	git_config_free(single_level_cfg);
}
Example #22
0
void test_config_read__foreach(void)
{
	git_config *cfg;
	int count, ret;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
		GIT_CONFIG_LEVEL_GLOBAL, NULL, 0));

	count = 0;
	cl_git_pass(git_config_foreach(cfg, count_cfg_entries_and_compare_levels, &count));
	cl_assert_equal_i(7, count);

	count = 3;
	cl_git_fail(ret = git_config_foreach(cfg, cfg_callback_countdown, &count));
	cl_assert_equal_i(-100, ret);

	git_config_free(cfg);
}
Example #23
0
void test_config_read__iterator(void)
{
	const char *keys[] = {
		"core.dummy2",
		"core.verylong",
		"core.dummy",
		"remote.ab.url",
		"remote.abba.url",
		"core.dummy2",
		"core.global"
	};
	git_config *cfg;
	git_config_iterator *iter;
	git_config_entry *entry;
	int count, ret;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
		GIT_CONFIG_LEVEL_GLOBAL, NULL, 0));

	count = 0;
	cl_git_pass(git_config_iterator_new(&iter, cfg));

	while ((ret = git_config_next(&entry, iter)) == 0) {
		cl_assert_equal_s(entry->name, keys[count]);
		count++;
	}

	git_config_iterator_free(iter);
	cl_assert_equal_i(GIT_ITEROVER, ret);
	cl_assert_equal_i(7, count);

	count = 3;
	cl_git_pass(git_config_iterator_new(&iter, cfg));

	git_config_iterator_free(iter);
	git_config_free(cfg);
}
Example #24
0
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);
}
static int DeleteOtherKeys(int type)
{
	CString match;
	if (type == SimpleCredentialType::LocalWincred)
		match = _T("L\ncredential.helper\nwincred");
	else if (type == SimpleCredentialType::LocalWinstore)
		match = _T("L\ncredential.helper\n") + GetWinstorePath();
	else if (type == SimpleCredentialType::GlobalWincred)
		match = _T("G\ncredential.helper\nwincred");
	else if (type == SimpleCredentialType::GlobalWinstore)
		match = _T("G\ncredential.helper\n") + GetWinstorePath();
	else if (type == SimpleCredentialType::SystemWincred)
		match = _T("S\ncredential.helper\nwincred");

	git_config * config;
	git_config_new(&config);
	CStringA projectConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitLocalConfig());
	git_config_add_file_ondisk(config, projectConfigA.GetBuffer(), GIT_CONFIG_LEVEL_LOCAL, FALSE);
	projectConfigA.ReleaseBuffer();
	CStringA globalConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalConfig());
	git_config_add_file_ondisk(config, globalConfigA.GetBuffer(), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
	globalConfigA.ReleaseBuffer();
	CStringA globalXDGConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalXDGConfig());
	git_config_add_file_ondisk(config, globalXDGConfigA.GetBuffer(), GIT_CONFIG_LEVEL_XDG, FALSE);
	globalXDGConfigA.ReleaseBuffer();
	CStringA systemConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitSystemConfig());
	git_config_add_file_ondisk(config, systemConfigA.GetBuffer(), GIT_CONFIG_LEVEL_SYSTEM, FALSE);
	systemConfigA.ReleaseBuffer();

	STRING_VECTOR list;
	git_config_foreach_match(config, "credential\\..*", GetCredentialAnyEntryCallback, &list);
	for (size_t i = 0; i < list.size(); ++i)
	{
		int pos = 0;
		CString prefix = list[i].Tokenize(_T("\n"), pos);
		if (prefix == _T("S") && !CAppUtils::IsAdminLogin())
		{
			RunUAC();
			return -1;
		}
	}

	int result = 0;
	// workaround gitdll bug
	// TODO: switch to libgit2
	bool old = g_Git.m_IsUseGitDLL;
	g_Git.m_IsUseGitDLL = false;
	for (size_t i = 0; i < list.size(); ++i)
	{
		if (list[i] != match)
		{
			int pos = 0;
			CString prefix = list[i].Tokenize(_T("\n"), pos);
			CString key = list[i].Tokenize(_T("\n"), pos);
			CONFIG_TYPE configLevel = prefix == _T("S") ? CONFIG_SYSTEM : prefix == _T("G") || prefix == _T("X") ? CONFIG_GLOBAL : CONFIG_LOCAL;
			if (g_Git.UnsetConfigValue(key, configLevel, CP_UTF8))
			{
				CString msg;
				msg.Format(IDS_PROC_SAVECONFIGFAILED, key, _T(""));
				CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
				result = 1;
				break;
			}
		}
	}
	g_Git.m_IsUseGitDLL = old;

	return result;
}
Example #26
0
int
create_repo(const char *repo_name)
{
	int r;

	git_repository *repo;
	git_oid blob_id;
	git_oid oid;
	git_oid tree_id;
	git_signature *author;
	git_time_t time;
	git_config *config;
	git_index *index;
	git_tree *tree;
	git_treebuilder *tree_builder;
	git_treebuilder *empty_tree_builder;
	char global_config_path[GIT_PATH_MAX];
	
	// create the repository
	r = git_repository_init(&repo, repo_name, 1);
	if (r)
		printf("error in creating repository\n");
	printf("Repo created\n");
	
	// set the repository config
	git_config_new(&config);
	git_config_find_global(global_config_path, GIT_PATH_MAX);
	git_config_add_file_ondisk(config, global_config_path, 1);
	//git_config_set_string(config, "name", "Varun Agrawal");
	//git_config_set_string(config, "email", "*****@*****.**");
	git_repository_set_config(repo, config);
	printf("Repo config set\n");

	// create a treebuilder
	r = git_treebuilder_create(&tree_builder, NULL);
	if (r)
		printf("error in creting treebuilder\n");
	printf("Tree builder created\n");

	// ADDING FIRST FILE
	// create a blob
	r = git_blob_create_fromdisk(&blob_id, repo, "test1");
	if (r)
		printf("error in creating blob from disk\n");
	printf("Blob created\n");

	// insert into tree
	r = git_treebuilder_insert(NULL, tree_builder, "test1", &blob_id, 0100644);
	if (r)
		printf("error in inserting into treebuilder\n");
	printf("Insert into treebuilder successful\n");

	// ADDING SECOND FILE
	// create a blob
	r = git_blob_create_fromdisk(&blob_id, repo, "test2");
	if (r)
		printf("error in creating blob from disk\n");
	printf("Blob created\n");

	// insert into tree
	r = git_treebuilder_insert(NULL, tree_builder, "test2", &blob_id, 0100644);
	if (r)
		printf("error in inserting into treebuilder\n");
	printf("Insert into treebuilder successful\n");

	// ADDING A EMPTY FOLDER
	// create a empty tree
	r = git_treebuilder_create(&empty_tree_builder, NULL);
	if (r)
		printf("error in creting empty treebuilder\n");
	printf("Empty Tree builder created\n");
	
	// write the empty tree to the repo
	r = git_treebuilder_write(&tree_id, repo, empty_tree_builder);
	if (r)
		printf("error in writing the empty tree to the repo\n");
	printf("Writing the empty tree to repo successful\n");


	// insert empty tree into the tree
	r = git_treebuilder_insert(NULL, tree_builder, "test_dir", &tree_id, 0040000);
	if (r)
		printf("error in inserting into treebuilder\n");
	printf("Insert into treebuilder successful\n");

	// write the tree to the repo
	r = git_treebuilder_write(&oid, repo, tree_builder);
	if (r)
		printf("error in writing the tree to the repo\n");
	printf("Writing the tree to repo successful\n");

	// tree lookup
	r = git_tree_lookup(&tree, repo, &oid);
	if (r)
		printf("error in tree lookup\n");
	printf("Tree lookup done\n");
	
	// create a author
	time = get_time();
	r = git_signature_new(&author, "Varun Agrawal", "*****@*****.**", time, -300);
	if (r)
		printf("error in creating signature\n");
	printf("Author signature created\n");

	// create a commit
	r = git_commit_create(  &oid, // object id
				repo, // repository
				"HEAD", // update reference, this will update the HEAD to this commit
				author, // author
				author, // committer
				NULL, // message encoding, by default UTF-8 is used
				"first commit", // message for the commit
				tree, // the git_tree object which will be used as the tree for this commit. don't know if NULL is valid
				0, // number of parents. Don't know what value should be used here
				NULL); // array of pointers to the parents(git_commit *parents[])
	if (r)
		printf("error in creating a commit\n");
	printf("Commit created\n");
	
	git_repository_free(repo);
	return 0;
}
Example #27
0
bool CGitIgnoreList::CheckAndUpdateCoreExcludefile(const CString &adminDir)
{
    CString projectConfig = adminDir + _T("config");
    CString globalConfig = g_Git.GetGitGlobalConfig();
    CString globalXDGConfig = g_Git.GetGitGlobalXDGConfig();

    CAutoWriteLock lock(&m_coreExcludefilesSharedMutex);
    bool hasChanged = CheckAndUpdateMsysGitBinpath();
    CString systemConfig = m_sMsysGitBinPath + _T("\\..\\etc\\gitconfig");

    hasChanged = hasChanged || CheckFileChanged(projectConfig);
    hasChanged = hasChanged || CheckFileChanged(globalConfig);
    hasChanged = hasChanged || CheckFileChanged(globalXDGConfig);
    if (!m_sMsysGitBinPath.IsEmpty())
        hasChanged = hasChanged || CheckFileChanged(systemConfig);

    m_SharedMutex.AcquireShared();
    CString excludesFile = m_CoreExcludesfiles[adminDir];
    m_SharedMutex.ReleaseShared();
    if (!excludesFile.IsEmpty())
        hasChanged = hasChanged || CheckFileChanged(excludesFile);

    if (!hasChanged)
        return false;

    git_config * config;
    git_config_new(&config);
    git_config_add_file_ondisk(config, CGit::GetGitPathStringA(projectConfig), GIT_CONFIG_LEVEL_LOCAL, FALSE);
    git_config_add_file_ondisk(config, CGit::GetGitPathStringA(globalConfig), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
    git_config_add_file_ondisk(config, CGit::GetGitPathStringA(globalXDGConfig), GIT_CONFIG_LEVEL_XDG, FALSE);
    if (!m_sMsysGitBinPath.IsEmpty())
        git_config_add_file_ondisk(config, CGit::GetGitPathStringA(systemConfig), GIT_CONFIG_LEVEL_SYSTEM, FALSE);
    const char * out = NULL;
    CStringA name(_T("core.excludesfile"));
    git_config_get_string(&out, config, name);
    CStringA excludesFileA(out);
    excludesFile = CUnicodeUtils::GetUnicode(excludesFileA);
    if (excludesFile.IsEmpty())
        excludesFile = GetWindowsHome() + _T("\\.config\\git\\ignore");
    else if (excludesFile.Find(_T("~/")) == 0)
        excludesFile = GetWindowsHome() + excludesFile.Mid(1);
    git_config_free(config);

    CAutoWriteLock lockMap(&m_SharedMutex);
    g_Git.GetFileModifyTime(projectConfig, &m_Map[projectConfig].m_LastModifyTime);
    g_Git.GetFileModifyTime(globalXDGConfig, &m_Map[globalXDGConfig].m_LastModifyTime);
    if (m_Map[globalXDGConfig].m_LastModifyTime == 0)
    {
        m_Map[globalXDGConfig].m_SharedMutex.Release();
        m_Map.erase(globalXDGConfig);
    }
    g_Git.GetFileModifyTime(globalConfig, &m_Map[globalConfig].m_LastModifyTime);
    if (m_Map[globalConfig].m_LastModifyTime == 0)
    {
        m_Map[globalConfig].m_SharedMutex.Release();
        m_Map.erase(globalConfig);
    }
    if (!m_sMsysGitBinPath.IsEmpty())
        g_Git.GetFileModifyTime(systemConfig, &m_Map[systemConfig].m_LastModifyTime);
    if (m_Map[systemConfig].m_LastModifyTime == 0 || m_sMsysGitBinPath.IsEmpty())
    {
        m_Map[systemConfig].m_SharedMutex.Release();
        m_Map.erase(systemConfig);
    }
    m_CoreExcludesfiles[adminDir] = excludesFile;

    return true;
}
Example #28
0
QGitConfig::QGitConfig(git_config *cfg)
    : d(cfg)
{
    if (d == 0)
        git_config_new(&d);
}
void CSettingGitCredential::LoadList()
{
	git_config * config;
	git_config_new(&config);
	CStringA projectConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitLocalConfig());
	git_config_add_file_ondisk(config, projectConfigA.GetBuffer(), GIT_CONFIG_LEVEL_LOCAL, FALSE);
	projectConfigA.ReleaseBuffer();
	CStringA globalConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalConfig());
	git_config_add_file_ondisk(config, globalConfigA.GetBuffer(), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
	globalConfigA.ReleaseBuffer();
	CStringA globalXDGConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalXDGConfig());
	git_config_add_file_ondisk(config, globalXDGConfigA.GetBuffer(), GIT_CONFIG_LEVEL_XDG, FALSE);
	globalXDGConfigA.ReleaseBuffer();
	CStringA systemConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitSystemConfig());
	git_config_add_file_ondisk(config, systemConfigA.GetBuffer(), GIT_CONFIG_LEVEL_SYSTEM, FALSE);
	systemConfigA.ReleaseBuffer();

	STRING_VECTOR defaultList, urlList;
	git_config_foreach_match(config, "credential\\.helper", GetCredentialDefaultUrlCallback, &defaultList);
	git_config_foreach_match(config, "credential\\..*\\.helper", GetCredentialUrlCallback, &urlList);
	STRING_VECTOR anyList;
	git_config_foreach_match(config, "credential\\..*", GetCredentialAnyEntryCallback, &anyList);
	git_config_free(config);

	for (int i = 0; i < defaultList.size(); ++i)
		m_ctrlUrlList.AddString(defaultList[i]);
	for (int i = 0; i < urlList.size(); ++i)
		m_ctrlUrlList.AddString(urlList[i]);

	if (anyList.empty())
	{
		m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::None);
		return;
	}
	if (anyList.size() > 1)
	{
		m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::Advanced);
		return;
	}

	int pos = 0;
	CString prefix = anyList[0].Tokenize(_T("\n"), pos);
	CString key = anyList[0].Tokenize(_T("\n"), pos);
	CString value = anyList[0].Tokenize(_T("\n"), pos);
	if (key != _T("credential.helper"))
	{
		m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::Advanced);
		return;
	}

	CString winstore = GetWinstorePath();
	if (prefix == _T("L"))
	{
		if (value == _T("wincred"))
		{
			m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::LocalWincred);
			return;
		}
		else if (value == winstore)
		{
			m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::LocalWinstore);
			return;
		}
	}

	if (prefix == _T("G") || prefix == _T("X"))
	{
		if (value == _T("wincred"))
		{
			m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::GlobalWincred);
			return;
		}
		else if (value == winstore)
		{
			m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::GlobalWinstore);
			return;
		}
	}

	if (prefix == _T("S"))
	{
		if (value == _T("wincred"))
		{
			m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::SystemWincred);
			return;
		}
	}

	m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::Advanced);
}
void CSettingGitCredential::OnBnClickedButtonRemove()
{
	int index = m_ctrlUrlList.GetCurSel();
	if (index >= 0)
	{
		CString str;
		m_ctrlUrlList.GetText(index, str);
		CString msg;
		msg.Format(IDS_GITCREDENTIAL_DELETEHELPER, str);
		if (CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_YESNO | MB_ICONQUESTION) == IDYES)
		{
			git_config * config;
			git_config_new(&config);
			int pos = str.Find(_T(':'));
			CString prefix = pos >= 0 ? str.Left(pos) : str;
			CString url = pos >= 0 ? str.Mid(pos + 1) : _T("");
			CONFIG_TYPE configLevel = CONFIG_LOCAL;
			switch (prefix[0])
			{
			case _T('L'):
				{
				configLevel = CONFIG_LOCAL;
				CStringA projectConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitLocalConfig());
				git_config_add_file_ondisk(config, projectConfigA.GetBuffer(), GIT_CONFIG_LEVEL_LOCAL, FALSE);
				projectConfigA.ReleaseBuffer();
				break;
				}
			case _T('G'):
			case _T('X'):
				{
				configLevel = CONFIG_GLOBAL;
				CStringA globalConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalConfig());
				git_config_add_file_ondisk(config, globalConfigA.GetBuffer(), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
				globalConfigA.ReleaseBuffer();
				CStringA globalXDGConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalXDGConfig());
				git_config_add_file_ondisk(config, globalXDGConfigA.GetBuffer(), GIT_CONFIG_LEVEL_XDG, FALSE);
				globalXDGConfigA.ReleaseBuffer();
				break;
				}
			case _T('S'):
				{
				if (!CAppUtils::IsAdminLogin())
				{
					RunUAC();
					git_config_free(config);
					EndDialog(0);
					return;
				}
				configLevel = CONFIG_SYSTEM;
				CStringA systemConfigA = CUnicodeUtils::GetUTF8(g_Git.GetGitSystemConfig());
				git_config_add_file_ondisk(config, systemConfigA.GetBuffer(), GIT_CONFIG_LEVEL_SYSTEM, FALSE);
				systemConfigA.ReleaseBuffer();
				break;
				}
			}

			STRING_VECTOR list;
			CStringA urlA = CUnicodeUtils::GetUTF8(url);
			CStringA pattern = urlA.IsEmpty() ? "^credential\\.[^.]+$" : ("credential\\." + RegexEscape(urlA) + "\\..*");
			git_config_foreach_match(config, pattern, GetCredentialEntryCallback, &list);
			git_config_free(config);
			for (size_t i = 0; i < list.size(); ++i)
				g_Git.UnsetConfigValue(list[i], configLevel, CP_UTF8);
			m_ctrlUrlList.DeleteString(index);
			OnLbnSelchangeListUrl();
		}
	}
}