parse-options-cb.c 5.39 KB
Newer Older
1 2 3 4 5 6
#include "git-compat-util.h"
#include "parse-options.h"
#include "cache.h"
#include "commit.h"
#include "color.h"
#include "string-list.h"
7
#include "argv-array.h"
8
#include "sha1-array.h"
9 10 11 12 13 14 15 16 17 18 19 20

/*----- some often used options -----*/

int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
{
	int v;

	if (!arg) {
		v = unset ? 0 : DEFAULT_ABBREV;
	} else {
		v = strtol(arg, (char **)&arg, 10);
		if (*arg)
21 22
			return error(_("option `%s' expects a numerical value"),
				     opt->long_name);
23 24
		if (v && v < MINIMUM_ABBREV)
			v = MINIMUM_ABBREV;
25 26
		else if (v > the_hash_algo->hexsz)
			v = the_hash_algo->hexsz;
27 28 29 30 31
	}
	*(int *)(opt->value) = v;
	return 0;
}

32 33 34
int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
			     int unset)
{
35 36 37 38 39
	if (unset)
		arg = "never";
	if (parse_expiry_date(arg, (timestamp_t *)opt->value))
		die(_("malformed expiration date '%s'"), arg);
	return 0;
40 41
}

42 43 44 45 46 47 48
int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
			    int unset)
{
	int value;

	if (!arg)
		arg = unset ? "never" : (const char *)opt->defval;
49
	value = git_config_colorbool(NULL, arg);
50
	if (value < 0)
51 52
		return error(_("option `%s' expects \"always\", \"auto\", or \"never\""),
			     opt->long_name);
53 54 55 56 57 58 59 60 61
	*(int *)opt->value = value;
	return 0;
}

int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
			   int unset)
{
	int *target = opt->value;

62 63
	BUG_ON_OPT_ARG(arg);

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	if (unset)
		/* --no-quiet, --no-verbose */
		*target = 0;
	else if (opt->short_name == 'v') {
		if (*target >= 0)
			(*target)++;
		else
			*target = 1;
	} else {
		if (*target <= 0)
			(*target)--;
		else
			*target = -1;
	}
	return 0;
}

81
int parse_opt_commits(const struct option *opt, const char *arg, int unset)
82
{
83
	struct object_id oid;
84 85
	struct commit *commit;

86 87
	BUG_ON_OPT_NEG(unset);

88 89
	if (!arg)
		return -1;
90
	if (get_oid(arg, &oid))
91
		return error("malformed object name %s", arg);
92
	commit = lookup_commit_reference(the_repository, &oid);
93 94 95 96 97 98
	if (!commit)
		return error("no such commit %s", arg);
	commit_list_insert(commit, opt->value);
	return 0;
}

99 100
int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
{
101
	struct object_id oid;
102 103

	if (unset) {
104
		oid_array_clear(opt->value);
105 106 107 108
		return 0;
	}
	if (!arg)
		return -1;
109
	if (get_oid(arg, &oid))
110
		return error(_("malformed object name '%s'"), arg);
111
	oid_array_append(opt->value, &oid);
112 113 114
	return 0;
}

115 116 117
int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
{
	int *target = opt->value;
118 119 120

	BUG_ON_OPT_ARG(arg);

121 122 123 124
	*target = unset ? 2 : 1;
	return 0;
}

125
struct option *parse_options_concat(struct option *a, struct option *b)
126
{
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	struct option *ret;
	size_t i, a_len = 0, b_len = 0;

	for (i = 0; a[i].type != OPTION_END; i++)
		a_len++;
	for (i = 0; b[i].type != OPTION_END; i++)
		b_len++;

	ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
	for (i = 0; i < a_len; i++)
		ret[i] = a[i];
	for (i = 0; i < b_len; i++)
		ret[a_len + i] = b[i];
	ret[a_len + b_len] = b[b_len]; /* final OPTION_END */

	return ret;
143 144 145 146 147 148 149 150 151 152 153 154 155 156
}

int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
{
	struct string_list *v = opt->value;

	if (unset) {
		string_list_clear(v, 0);
		return 0;
	}

	if (!arg)
		return -1;

157
	string_list_append(v, arg);
158 159
	return 0;
}
160 161 162 163 164

int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
{
	return 0;
}
165

166 167 168 169 170 171 172
/**
 * Report that the option is unknown, so that other code can handle
 * it. This can be used as a callback together with
 * OPTION_LOWLEVEL_CALLBACK to allow an option to be documented in the
 * "-h" output even if it's not being handled directly by
 * parse_options().
 */
173
enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
174 175
					   const struct option *opt,
					   const char *arg, int unset)
176
{
177
	BUG_ON_OPT_ARG(arg);
178
	return PARSE_OPT_UNKNOWN;
179 180
}

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/**
 * Recreates the command-line option in the strbuf.
 */
static int recreate_opt(struct strbuf *sb, const struct option *opt,
		const char *arg, int unset)
{
	strbuf_reset(sb);

	if (opt->long_name) {
		strbuf_addstr(sb, unset ? "--no-" : "--");
		strbuf_addstr(sb, opt->long_name);
		if (arg) {
			strbuf_addch(sb, '=');
			strbuf_addstr(sb, arg);
		}
	} else if (opt->short_name && !unset) {
		strbuf_addch(sb, '-');
		strbuf_addch(sb, opt->short_name);
		if (arg)
			strbuf_addstr(sb, arg);
	} else
		return -1;

	return 0;
}

/**
 * For an option opt, recreates the command-line option in opt->value which
 * must be an char* initialized to NULL. This is useful when we need to pass
 * the command-line option to another command. Since any previous value will be
 * overwritten, this callback should only be used for options where the last
 * one wins.
 */
int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
{
	static struct strbuf sb = STRBUF_INIT;
	char **opt_value = opt->value;

	if (recreate_opt(&sb, opt, arg, unset) < 0)
		return -1;

222
	free(*opt_value);
223 224 225 226 227

	*opt_value = strbuf_detach(&sb, NULL);

	return 0;
}
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

/**
 * For an option opt, recreate the command-line option, appending it to
 * opt->value which must be a argv_array. This is useful when we need to pass
 * the command-line option, which can be specified multiple times, to another
 * command.
 */
int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset)
{
	static struct strbuf sb = STRBUF_INIT;
	struct argv_array *opt_value = opt->value;

	if (recreate_opt(&sb, opt, arg, unset) < 0)
		return -1;

	argv_array_push(opt_value, sb.buf);

	return 0;
}