Skip to content

Commit

Permalink
Merge branch 'pb/config'
Browse files Browse the repository at this point in the history
* pb/config:
  git_config: access() returns 0 on success, not > 0
  repo-config: Fix late-night bug
  Read configuration also from $HOME/.gitconfig
  Fix setting config variables with an alternative GIT_CONFIG
  Support for extracting configuration from different files
  • Loading branch information
Junio C Hamano committed Jun 22, 2006
2 parents f97ccde + e33d061 commit c0a2e1c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 11 deletions.
12 changes: 12 additions & 0 deletions Documentation/git-repo-config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ OPTIONS
List all variables set in .git/config.


ENVIRONMENT
-----------

GIT_CONFIG::
Take the configuration from the given file instead of .git/config.

GIT_CONFIG_LOCAL::
Currently the same as $GIT_CONFIG; when Git will support global
configuration files, this will cause it to take the configuration
from the global configuration file in addition to the given file.


EXAMPLE
-------

Expand Down
43 changes: 39 additions & 4 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,33 @@ int git_config_from_file(config_fn_t fn, const char *filename)

int git_config(config_fn_t fn)
{
return git_config_from_file(fn, git_path("config"));
int ret = 0;
char *repo_config = NULL;
const char *home = NULL, *filename;

/* $GIT_CONFIG makes git read _only_ the given config file,
* $GIT_CONFIG_LOCAL will make it process it in addition to the
* global config file, the same way it would the per-repository
* config file otherwise. */
filename = getenv("GIT_CONFIG");
if (!filename) {
home = getenv("HOME");
filename = getenv("GIT_CONFIG_LOCAL");
if (!filename)
filename = repo_config = strdup(git_path("config"));
}

if (home) {
char *user_config = strdup(mkpath("%s/.gitconfig", home));
if (!access(user_config, R_OK))
ret = git_config_from_file(fn, user_config);
free(user_config);
}

ret += git_config_from_file(fn, filename);
if (repo_config)
free(repo_config);
return ret;
}

/*
Expand Down Expand Up @@ -490,10 +516,19 @@ int git_config_set_multivar(const char* key, const char* value,
int i, dot;
int fd = -1, in_fd;
int ret;
char* config_filename = strdup(git_path("config"));
char* lock_file = strdup(git_path("config.lock"));
char* config_filename;
char* lock_file;
const char* last_dot = strrchr(key, '.');

config_filename = getenv("GIT_CONFIG");
if (!config_filename) {
config_filename = getenv("GIT_CONFIG_LOCAL");
if (!config_filename)
config_filename = git_path("config");
}
config_filename = strdup(config_filename);
lock_file = strdup(mkpath("%s.lock", config_filename));

/*
* Since "key" actually contains the section name and the real
* key name separated by a dot, we have to know where the dot is.
Expand Down Expand Up @@ -600,7 +635,7 @@ int git_config_set_multivar(const char* key, const char* value,
* As a side effect, we make sure to transform only a valid
* existing config file.
*/
if (git_config(store_aux)) {
if (git_config_from_file(store_aux, config_filename)) {
fprintf(stderr, "invalid config file\n");
free(store.key);
if (store.value_regex != NULL) {
Expand Down
37 changes: 31 additions & 6 deletions repo-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@ static int show_config(const char* key_, const char* value_)

static int get_value(const char* key_, const char* regex_)
{
int ret = -1;
char *tl;
char *global = NULL, *repo_config = NULL;
const char *local;

local = getenv("GIT_CONFIG");
if (!local) {
const char *home = getenv("HOME");
local = getenv("GIT_CONFIG_LOCAL");
if (!local)
local = repo_config = strdup(git_path("config"));
if (home)
global = strdup(mkpath("%s/.gitconfig", home));
}

key = strdup(key_);
for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
Expand All @@ -76,7 +89,7 @@ static int get_value(const char* key_, const char* regex_)
key_regexp = (regex_t*)malloc(sizeof(regex_t));
if (regcomp(key_regexp, key, REG_EXTENDED)) {
fprintf(stderr, "Invalid key pattern: %s\n", key_);
return -1;
goto free_strings;
}
}

Expand All @@ -89,21 +102,33 @@ static int get_value(const char* key_, const char* regex_)
regexp = (regex_t*)malloc(sizeof(regex_t));
if (regcomp(regexp, regex_, REG_EXTENDED)) {
fprintf(stderr, "Invalid pattern: %s\n", regex_);
return -1;
goto free_strings;
}
}

git_config(show_config);
if (do_all && global)
git_config_from_file(show_config, global);
git_config_from_file(show_config, local);
if (!do_all && !seen && global)
git_config_from_file(show_config, global);

free(key);
if (regexp) {
regfree(regexp);
free(regexp);
}

if (do_all)
return !seen;

return (seen == 1) ? 0 : 1;
ret = !seen;
else
ret = (seen == 1) ? 0 : 1;

free_strings:
if (repo_config)
free(repo_config);
if (global)
free(global);
return ret;
}

int main(int argc, const char **argv)
Expand Down
2 changes: 1 addition & 1 deletion t/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ endif
all: $(T) clean

$(T):
@echo "*** $@ ***"; '$(SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS)
@echo "*** $@ ***"; GIT_CONFIG=.git/config '$(SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS)

clean:
rm -fr trash
Expand Down
24 changes: 24 additions & 0 deletions t/t1300-repo-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,29 @@ EOF

test_expect_success 'new variable inserts into proper section' 'cmp .git/config expect'

cat > other-config << EOF
[ein]
bahn = strasse
EOF

cat > expect << EOF
ein.bahn=strasse
EOF

GIT_CONFIG=other-config git-repo-config -l > output

test_expect_success 'alternative GIT_CONFIG' 'cmp output expect'

GIT_CONFIG=other-config git-repo-config anwohner.park ausweis

cat > expect << EOF
[ein]
bahn = strasse
[anwohner]
park = ausweis
EOF

test_expect_success '--set in alternative GIT_CONFIG' 'cmp other-config expect'

test_done

0 comments on commit c0a2e1c

Please sign in to comment.