Allow passing of configuration parameters in the command line
The values passed this way will override whatever is defined in the config files. Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alex Riesen committed
Mar 26, 2010 at 23:53 UTC
8b1fa778676ae94f7a6d4113fa90947b548154dd
6 files changed
+102
-1
Documentation/git.txt
+7
@@ -12,6 +12,7 @@ SYNOPSIS
12
'git' [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]
13
[-p|--paginate|--no-pager] [--no-replace-objects]
14
[--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
15
+ [-c name=value]
16
[--help] COMMAND [ARGS]
17
18
DESCRIPTION
@@ -219,6 +220,12 @@ displayed. See linkgit:git-help[1] for more information,
220
because `git --help ...` is converted internally into `git
221
help ...`.
222
223
+-c <name>=<value>::
224
+ Pass a configuration parameter to the command. The value
225
+ given will override values from configuration files.
226
+ The <name> is expected in the same format as listed by
227
+ 'git config' (subkeys separated by dots).
228
+
229
--exec-path::
230
Path to wherever your core git programs are installed.
231
This can also be controlled by setting the GIT_EXEC_PATH
builtin/config.c
+5
-1
@@ -197,7 +197,11 @@ static int get_value(const char *key_, const char *regex_)
197
git_config_from_file(show_config, system_wide, NULL);
198
if (do_all && global)
199
git_config_from_file(show_config, global, NULL);
200
- git_config_from_file(show_config, local, NULL);
200
+ if (do_all)
201
+ git_config_from_file(show_config, local, NULL);
202
+ git_config_from_parameters(show_config, NULL);
203
+ if (!do_all && !seen)
204
+ git_config_from_file(show_config, local, NULL);
205
if (!do_all && !seen && global)
206
git_config_from_file(show_config, global, NULL);
207
if (!do_all && !seen && system_wide)
cache.h
+2
@@ -932,6 +932,8 @@ extern int update_server_info(int);
932
typedef int (*config_fn_t)(const char *, const char *, void *);
933
extern int git_default_config(const char *, const char *, void *);
934
extern int git_config_from_file(config_fn_t fn, const char *, void *);
935
+extern int git_config_parse_parameter(const char *text);
936
+extern int git_config_from_parameters();
937
extern int git_config(config_fn_t fn, void *);
938
extern int git_parse_ulong(const char *, unsigned long *);
939
extern int git_config_int(const char *, const char *);
config.c
+71
@@ -18,6 +18,62 @@ static int zlib_compression_seen;
18
19
const char *config_exclusive_filename = NULL;
20
21
+struct config_item
22
+{
23
+ struct config_item *next;
24
+ char *value;
25
+ char name[1 /* NUL */];
26
+};
27
+static struct config_item *config_parameters;
28
+static struct config_item **config_parameters_tail = &config_parameters;
29
+
30
+static void lowercase(char *p)
31
+{
32
+ for (; *p; p++)
33
+ *p = tolower(*p);
34
+}
35
+static char *skip_space(const char *p)
36
+{
37
+ for (; *p; p++)
38
+ if (!isspace(*p))
39
+ break;
40
+ return (char *)p;
41
+}
42
+static char *trailing_space(const char *begin, const char *p)
43
+{
44
+ while (p-- > begin)
45
+ if (!isspace(*p))
46
+ break;
47
+ return (char *)p + 1;
48
+}
49
+
50
+int git_config_parse_parameter(const char *text)
51
+{
52
+ struct config_item *ct;
53
+ const char *name;
54
+ const char *val;
55
+ name = skip_space(text);
56
+ text = val = strchr(name, '=');
57
+ if (!text)
58
+ text = name + strlen(name);
59
+ text = trailing_space(name, text);
60
+ if (text <= name)
61
+ return -1;
62
+ ct = xcalloc(1, sizeof(struct config_item) + (text - name));
63
+ memcpy(ct->name, name, text - name);
64
+ lowercase(ct->name);
65
+ if (!val)
66
+ ct->value = NULL;
67
+ else {
68
+ val = skip_space(++val /* skip "=" */);
69
+ text = trailing_space(val, val + strlen(val));
70
+ ct->value = xstrndup(val, text - val);
71
+ }
72
+ *config_parameters_tail = ct;
73
+ config_parameters_tail = &ct->next;
74
+ return 0;
75
+}
76
+
77
static int get_next_char(void)
78
{
79
int c;
@@ -699,6 +755,15 @@ int git_config_global(void)
755
return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0);
756
}
757
758
+int git_config_from_parameters(config_fn_t fn, void *data)
759
+{
760
+ const struct config_item *ct;
761
+ for (ct = config_parameters; ct; ct = ct->next)
762
+ if (fn(ct->name, ct->value, data) < 0)
763
+ return -1;
764
+ return 0;
765
+}
766
+
767
int git_config(config_fn_t fn, void *data)
768
{
769
int ret = 0, found = 0;
@@ -730,6 +795,12 @@ int git_config(config_fn_t fn, void *data)
795
found += 1;
796
}
797
free(repo_config);
798
+
799
+ if (config_parameters) {
800
+ ret += git_config_from_parameters(fn, data);
801
+ found += 1;
802
+ }
803
+
804
if (found == 0)
805
return -1;
806
return ret;
git.c
+9
@@ -8,6 +8,7 @@ const char git_usage_string[] =
8
"git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]\n"
9
" [-p|--paginate|--no-pager] [--no-replace-objects]\n"
10
" [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]\n"
11
+ " [-c name=value\n"
12
" [--help] COMMAND [ARGS]";
13
14
const char git_more_info_string[] =
@@ -130,6 +131,14 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
131
setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0);
132
if (envchanged)
133
*envchanged = 1;
134
+ } else if (!strcmp(cmd, "-c")) {
135
+ if (*argc < 2) {
136
+ fprintf(stderr, "-c expects a configuration string\n" );
137
+ usage(git_usage_string);
138
+ }
139
+ git_config_parse_parameter((*argv)[1]);
140
+ (*argv)++;
141
+ (*argc)--;
142
} else {
143
fprintf(stderr, "Unknown option: %s\n", cmd);
144
usage(git_usage_string);
t/t1300-repo-config.sh
+8
@@ -824,4 +824,12 @@ test_expect_success 'check split_cmdline return' "
824
test_must_fail git merge master
825
"
826
827
+test_expect_success 'git -c "key=value" support' '
828
+ test "z$(git -c name=value config name)" = zvalue &&
829
+ test "z$(git -c core.name=value config core.name)" = zvalue &&
830
+ test "z$(git -c CamelCase=value config camelcase)" = zvalue &&
831
+ test "z$(git -c flag config --bool flag)" = ztrue &&
832
+ test_must_fail git -c core.name=value config name
833
+'
834
+
835
test_done