Ejemplo n.º 1
0
/*
 * Try to remove the "submodule.<name>" section from .gitmodules where the given
 * path is configured. Return 0 only if a .gitmodules file was found, a section
 * with the correct path=<path> setting was found and we could remove it.
 */
int remove_path_from_gitmodules(const char *path)
{
	struct strbuf sect = STRBUF_INIT;
	struct string_list_item *path_option;

	if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
		return -1;

	if (gitmodules_is_unmerged)
		die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));

	path_option = unsorted_string_list_lookup(&config_name_for_path, path);
	if (!path_option) {
		warning(_("Could not find section in .gitmodules where path=%s"), path);
		return -1;
	}
	strbuf_addstr(&sect, "submodule.");
	strbuf_addstr(&sect, path_option->util);
	if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
		/* Maybe the user already did that, don't error out here */
		warning(_("Could not remove .gitmodules entry for %s"), path);
		strbuf_release(&sect);
		return -1;
	}
	strbuf_release(&sect);
	return 0;
}
Ejemplo n.º 2
0
/*
 * Try to remove the "submodule.<name>" section from .gitmodules where the given
 * path is configured. Return 0 only if a .gitmodules file was found, a section
 * with the correct path=<path> setting was found and we could remove it.
 */
int remove_path_from_gitmodules(const char *path)
{
	struct strbuf sect = STRBUF_INIT;
	const struct submodule *submodule;

	if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
		return -1;

	if (gitmodules_is_unmerged)
		die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));

	submodule = submodule_from_path(null_sha1, path);
	if (!submodule || !submodule->name) {
		warning(_("Could not find section in .gitmodules where path=%s"), path);
		return -1;
	}
	strbuf_addstr(&sect, "submodule.");
	strbuf_addstr(&sect, submodule->name);
	if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
		/* Maybe the user already did that, don't error out here */
		warning(_("Could not remove .gitmodules entry for %s"), path);
		strbuf_release(&sect);
		return -1;
	}
	strbuf_release(&sect);
	return 0;
}
Ejemplo n.º 3
0
int cmd_config(int argc, const char **argv, const char *prefix)
{
    int nongit = !startup_info->have_repository;
    char *value;

    given_config_file = getenv(CONFIG_ENVIRONMENT);

    argc = parse_options(argc, argv, prefix, builtin_config_options,
                         builtin_config_usage,
                         PARSE_OPT_STOP_AT_NON_OPTION);

    if (use_global_config + use_system_config + use_local_config + !!given_config_file > 1) {
        error("only one config file at a time.");
        usage_with_options(builtin_config_usage, builtin_config_options);
    }

    if (use_global_config) {
        char *user_config = NULL;
        char *xdg_config = NULL;

        home_config_paths(&user_config, &xdg_config, "config");

        if (access(user_config, R_OK) && !access(xdg_config, R_OK))
            given_config_file = xdg_config;
        else if (user_config)
            given_config_file = user_config;
        else
            die("$HOME not set");
    }
    else if (use_system_config)
        given_config_file = git_etc_gitconfig();
    else if (use_local_config)
        given_config_file = git_pathdup("config");
    else if (given_config_file) {
        if (!is_absolute_path(given_config_file) && prefix)
            given_config_file =
                xstrdup(prefix_filename(prefix,
                                        strlen(prefix),
                                        given_config_file));
    }

    if (respect_includes == -1)
        respect_includes = !given_config_file;

    if (end_null) {
        term = '\0';
        delim = '\n';
        key_delim = '\n';
    }

    if (HAS_MULTI_BITS(types)) {
        error("only one type at a time.");
        usage_with_options(builtin_config_usage, builtin_config_options);
    }

    if (get_color_slot)
        actions |= ACTION_GET_COLOR;
    if (get_colorbool_slot)
        actions |= ACTION_GET_COLORBOOL;

    if ((get_color_slot || get_colorbool_slot) && types) {
        error("--get-color and variable type are incoherent");
        usage_with_options(builtin_config_usage, builtin_config_options);
    }

    if (HAS_MULTI_BITS(actions)) {
        error("only one action at a time.");
        usage_with_options(builtin_config_usage, builtin_config_options);
    }
    if (actions == 0)
        switch (argc) {
        case 1:
            actions = ACTION_GET;
            break;
        case 2:
            actions = ACTION_SET;
            break;
        case 3:
            actions = ACTION_SET_ALL;
            break;
        default:
            usage_with_options(builtin_config_usage, builtin_config_options);
        }

    if (actions == ACTION_LIST) {
        check_argc(argc, 0, 0);
        if (git_config_with_options(show_all_config, NULL,
                                    given_config_file,
                                    respect_includes) < 0) {
            if (given_config_file)
                die_errno("unable to read config file '%s'",
                          given_config_file);
            else
                die("error processing config file(s)");
        }
    }
    else if (actions == ACTION_EDIT) {
        check_argc(argc, 0, 0);
        if (!given_config_file && nongit)
            die("not in a git directory");
        git_config(git_default_config, NULL);
        launch_editor(given_config_file ?
                      given_config_file : git_path("config"),
                      NULL, NULL);
    }
    else if (actions == ACTION_SET) {
        int ret;
        check_argc(argc, 2, 2);
        value = normalize_value(argv[0], argv[1]);
        ret = git_config_set_in_file(given_config_file, argv[0], value);
        if (ret == CONFIG_NOTHING_SET)
            error("cannot overwrite multiple values with a single value\n"
                  "       Use a regexp, --add or --replace-all to change %s.", argv[0]);
        return ret;
    }
    else if (actions == ACTION_SET_ALL) {
        check_argc(argc, 2, 3);
        value = normalize_value(argv[0], argv[1]);
        return git_config_set_multivar_in_file(given_config_file,
                                               argv[0], value, argv[2], 0);
    }
    else if (actions == ACTION_ADD) {
        check_argc(argc, 2, 2);
        value = normalize_value(argv[0], argv[1]);
        return git_config_set_multivar_in_file(given_config_file,
                                               argv[0], value, "^$", 0);
    }
    else if (actions == ACTION_REPLACE_ALL) {
        check_argc(argc, 2, 3);
        value = normalize_value(argv[0], argv[1]);
        return git_config_set_multivar_in_file(given_config_file,
                                               argv[0], value, argv[2], 1);
    }
    else if (actions == ACTION_GET) {
        check_argc(argc, 1, 2);
        return get_value(argv[0], argv[1]);
    }
    else if (actions == ACTION_GET_ALL) {
        do_all = 1;
        check_argc(argc, 1, 2);
        return get_value(argv[0], argv[1]);
    }
    else if (actions == ACTION_GET_REGEXP) {
        show_keys = 1;
        use_key_regexp = 1;
        do_all = 1;
        check_argc(argc, 1, 2);
        return get_value(argv[0], argv[1]);
    }
    else if (actions == ACTION_UNSET) {
        check_argc(argc, 1, 2);
        if (argc == 2)
            return git_config_set_multivar_in_file(given_config_file,
                                                   argv[0], NULL, argv[1], 0);
        else
            return git_config_set_in_file(given_config_file,
                                          argv[0], NULL);
    }
    else if (actions == ACTION_UNSET_ALL) {
        check_argc(argc, 1, 2);
        return git_config_set_multivar_in_file(given_config_file,
                                               argv[0], NULL, argv[1], 1);
    }
    else if (actions == ACTION_RENAME_SECTION) {
        int ret;
        check_argc(argc, 2, 2);
        ret = git_config_rename_section_in_file(given_config_file,
                                                argv[0], argv[1]);
        if (ret < 0)
            return ret;
        if (ret == 0)
            die("No such section!");
    }
    else if (actions == ACTION_REMOVE_SECTION) {
        int ret;
        check_argc(argc, 1, 1);
        ret = git_config_rename_section_in_file(given_config_file,
                                                argv[0], NULL);
        if (ret < 0)
            return ret;
        if (ret == 0)
            die("No such section!");
    }
    else if (actions == ACTION_GET_COLOR) {
        get_color(argv[0]);
    }
    else if (actions == ACTION_GET_COLORBOOL) {
        if (argc == 1)
            color_stdout_is_tty = git_config_bool("command line", argv[0]);
        return get_colorbool(argc != 0);
    }

    return 0;
}