Ejemplo n.º 1
0
int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
{
	int i;
	const char *format = "%(objectname) %(objecttype)\t%(refname)";
	struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
	int maxcount = 0, quote_style = 0;
	struct ref_array array;
	struct ref_filter filter;

	struct option opts[] = {
		OPT_BIT('s', "shell", &quote_style,
			N_("quote placeholders suitably for shells"), QUOTE_SHELL),
		OPT_BIT('p', "perl",  &quote_style,
			N_("quote placeholders suitably for perl"), QUOTE_PERL),
		OPT_BIT(0 , "python", &quote_style,
			N_("quote placeholders suitably for python"), QUOTE_PYTHON),
		OPT_BIT(0 , "tcl",  &quote_style,
			N_("quote placeholders suitably for Tcl"), QUOTE_TCL),

		OPT_GROUP(""),
		OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
		OPT_STRING(  0 , "format", &format, N_("format"), N_("format to use for the output")),
		OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
			    N_("field name to sort on"), &parse_opt_ref_sorting),
		OPT_CALLBACK(0, "points-at", &filter.points_at,
			     N_("object"), N_("print only refs which points at the given object"),
			     parse_opt_object_name),
		OPT_MERGED(&filter, N_("print only refs that are merged")),
		OPT_NO_MERGED(&filter, N_("print only refs that are not merged")),
		OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")),
		OPT_END(),
	};

	memset(&array, 0, sizeof(array));
	memset(&filter, 0, sizeof(filter));

	parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
	if (maxcount < 0) {
		error("invalid --count argument: `%d'", maxcount);
		usage_with_options(for_each_ref_usage, opts);
	}
	if (HAS_MULTI_BITS(quote_style)) {
		error("more than one quoting style?");
		usage_with_options(for_each_ref_usage, opts);
	}
	if (verify_ref_format(format))
		usage_with_options(for_each_ref_usage, opts);

	if (!sorting)
		sorting = ref_default_sorting();

	/* for warn_ambiguous_refs */
	git_config(git_default_config, NULL);

	filter.name_patterns = argv;
	filter.match_as_path = 1;
	filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
	ref_array_sort(sorting, &array);

	if (!maxcount || array.nr < maxcount)
		maxcount = array.nr;
	for (i = 0; i < maxcount; i++)
		show_ref_array_item(array.items[i], format, quote_style);
	ref_array_clear(&array);
	return 0;
}
Ejemplo n.º 2
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;
}