Merge branch 'jh/maint-config-file-prefix'
authorJunio C Hamano <gitster@pobox.com>
Wed, 27 Jan 2010 22:56:25 +0000 (14:56 -0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 27 Jan 2010 22:56:25 +0000 (14:56 -0800)
* jh/maint-config-file-prefix:
builtin-config: Fix crash when using "-f <relative path>" from non-root dir

1  2 
builtin-config.c
t/t1300-repo-config.sh
diff --combined builtin-config.c
index 2e3ef911d6d6682bc2f71ea912b05768fb6298be,5a5d214bb84c8eefa7ce9a1ca55c5689ce698af5..4bc46b15fde00d913577a2980778746f8315bb70
@@@ -45,7 -45,6 +45,7 @@@ static int end_null
  #define TYPE_BOOL (1<<0)
  #define TYPE_INT (1<<1)
  #define TYPE_BOOL_OR_INT (1<<2)
 +#define TYPE_PATH (1<<3)
  
  static struct option builtin_config_options[] = {
        OPT_GROUP("Config file location"),
@@@ -70,7 -69,6 +70,7 @@@
        OPT_BIT(0, "bool", &types, "value is \"true\" or \"false\"", TYPE_BOOL),
        OPT_BIT(0, "int", &types, "value is decimal number", TYPE_INT),
        OPT_BIT(0, "bool-or-int", &types, "value is --bool or --int", TYPE_BOOL_OR_INT),
 +      OPT_BIT(0, "path", &types, "value is a path (file or directory name)", TYPE_PATH),
        OPT_GROUP("Other"),
        OPT_BOOLEAN('z', "null", &end_null, "terminate values with NUL byte"),
        OPT_END(),
@@@ -96,7 -94,6 +96,7 @@@ static int show_config(const char *key_
  {
        char value[256];
        const char *vptr = value;
 +      int must_free_vptr = 0;
        int dup_error = 0;
  
        if (!use_key_regexp && strcmp(key_, key))
                        vptr = v ? "true" : "false";
                else
                        sprintf(value, "%d", v);
 +      } else if (types == TYPE_PATH) {
 +              git_config_pathname(&vptr, key_, value_);
 +              must_free_vptr = 1;
        }
        else
                vptr = value_?value_:"";
        }
        else
                printf("%s%c", vptr, term);
 +      if (must_free_vptr)
 +              /* If vptr must be freed, it's a pointer to a
 +               * dynamically allocated buffer, it's safe to cast to
 +               * const.
 +              */
 +              free((char *)vptr);
  
        return 0;
  }
@@@ -227,13 -215,7 +227,13 @@@ static char *normalize_value(const cha
        if (!value)
                return NULL;
  
 -      if (types == 0)
 +      if (types == 0 || types == TYPE_PATH)
 +              /*
 +               * We don't do normalization for TYPE_PATH here: If
 +               * the path is like ~/foobar/, we prefer to store
 +               * "~/foobar/" in the config file, and to expand the ~
 +               * when retrieving the value.
 +               */
                normalized = xstrdup(value);
        else {
                normalized = xmalloc(64);
@@@ -334,8 -316,7 +334,8 @@@ int cmd_config(int argc, const char **a
  
        config_exclusive_filename = getenv(CONFIG_ENVIRONMENT);
  
 -      argc = parse_options(argc, argv, builtin_config_options, builtin_config_usage,
 +      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 + !!given_config_file > 1) {
                if (!is_absolute_path(given_config_file) && prefix)
                        config_exclusive_filename = prefix_filename(prefix,
                                                                    strlen(prefix),
-                                                                   argv[2]);
+                                                                   given_config_file);
                else
                        config_exclusive_filename = given_config_file;
        }
                check_argc(argc, 0, 0);
                if (git_config(show_all_config, NULL) < 0) {
                        if (config_exclusive_filename)
 -                              die("unable to read config file %s: %s",
 -                                  config_exclusive_filename, strerror(errno));
 +                              die_errno("unable to read config file '%s'",
 +                                        config_exclusive_filename);
                        else
                                die("error processing config file(s)");
                }
diff --combined t/t1300-repo-config.sh
index f89d7e9e4959ddccafafd11c5fbf9f4b06541578,c81ccf2e56659b41787b9da69acbb3bf9f44f984..f11f98c3ce7e35f61d06542ce707d61b98079fda
@@@ -398,6 -398,17 +398,17 @@@ test_expect_success 'alternative GIT_CO
  test_expect_success 'alternative GIT_CONFIG (--file)' \
        'git config --file other-config -l > output && cmp output expect'
  
+ test_expect_success 'refer config from subdirectory' '
+       mkdir x &&
+       (
+               cd x &&
+               echo strasse >expect
+               git config --get --file ../other-config ein.bahn >actual &&
+               test_cmp expect actual
+       )
+ '
  GIT_CONFIG=other-config git config anwohner.park ausweis
  
  cat > expect << EOF
@@@ -459,28 -470,6 +470,28 @@@ EO
  
  test_expect_success "rename succeeded" "test_cmp expect .git/config"
  
 +cat >> .git/config << EOF
 +[branch "vier"] z = 1
 +EOF
 +
 +test_expect_success "rename a section with a var on the same line" \
 +      'git config --rename-section branch.vier branch.zwei'
 +
 +cat > expect << EOF
 +# Hallo
 +      #Bello
 +[branch "zwei"]
 +      x = 1
 +[branch "zwei"]
 +      y = 1
 +[branch "drei"]
 +weird
 +[branch "zwei"]
 +      z = 1
 +EOF
 +
 +test_expect_success "rename succeeded" "test_cmp expect .git/config"
 +
  cat >> .git/config << EOF
    [branch "zwei"] a = 1 [branch "vier"]
  EOF
@@@ -683,34 -672,6 +694,34 @@@ test_expect_success 'set --bool-or-int
  
  rm .git/config
  
 +cat >expect <<\EOF
 +[path]
 +      home = ~/
 +      normal = /dev/null
 +      trailingtilde = foo~
 +EOF
 +
 +test_expect_success 'set --path' '
 +      git config --path path.home "~/" &&
 +      git config --path path.normal "/dev/null" &&
 +      git config --path path.trailingtilde "foo~" &&
 +      test_cmp expect .git/config'
 +
 +cat >expect <<EOF
 +$HOME/
 +/dev/null
 +foo~
 +EOF
 +
 +test_expect_success 'get --path' '
 +      git config --get --path path.home > result &&
 +      git config --get --path path.normal >> result &&
 +      git config --get --path path.trailingtilde >> result &&
 +      test_cmp expect result
 +'
 +
 +rm .git/config
 +
  git config quote.leading " test"
  git config quote.ending "test "
  git config quote.semicolon "test;test"
@@@ -783,11 -744,6 +794,11 @@@ echo >>resul
  
  test_expect_success '--null --get-regexp' 'cmp result expect'
  
 +test_expect_success 'inner whitespace kept verbatim' '
 +      git config section.val "foo       bar" &&
 +      test "z$(git config section.val)" = "zfoo         bar"
 +'
 +
  test_expect_success SYMLINKS 'symlinked configuration' '
  
        ln -s notyet myconfig &&