Merge branch 'jk/setup-sequence-update'
authorJunio C Hamano <gitster@pobox.com>
Wed, 21 Sep 2016 22:15:23 +0000 (15:15 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 21 Sep 2016 22:15:24 +0000 (15:15 -0700)
There were numerous corner cases in which the configuration files
are read and used or not read at all depending on the directory a
Git command was run, leading to inconsistent behaviour. The code
to set-up repository access at the beginning of a Git process has
been updated to fix them.

* jk/setup-sequence-update:
t1007: factor out repeated setup
init: reset cached config when entering new repo
init: expand comments explaining config trickery
config: only read .git/config from configured repos
test-config: setup git directory
t1302: use "git -C"
pager: handle early config
pager: use callbacks instead of configset
pager: make pager_program a file-local static
pager: stop loading git_default_config()
pager: remove obsolete comment
diff: always try to set up the repository
diff: handle --no-index prefixes consistently
diff: skip implicit no-index check when given --no-index
patch-id: use RUN_SETUP_GENTLY
hash-object: always try to set up the git repository

1  2 
builtin/diff.c
builtin/init-db.c
cache.h
config.c
diff-no-index.c
environment.c
git.c
pager.c
t/helper/test-config.c
t/t4204-patch-id.sh
diff --cc builtin/diff.c
Simple merge
Simple merge
diff --cc cache.h
Simple merge
diff --cc config.c
index 0dfed682b86829fc06667d303c676e8abe6b3150,5e50fb1a31a5b4e0bb3c4cf517023c447c2f925f..1e4b6178f79587c9519ecbc9dd90c1e0b72af952
+++ b/config.c
@@@ -1286,32 -1191,43 +1283,32 @@@ int git_config_system(void
  
  static int do_git_config_sequence(config_fn_t fn, void *data)
  {
 -      int ret = 0, found = 0;
 +      int ret = 0;
        char *xdg_config = xdg_config_home("config");
        char *user_config = expand_user_path("~/.gitconfig");
-       char *repo_config = git_pathdup("config");
+       char *repo_config = have_git_dir() ? git_pathdup("config") : NULL;
  
 -      if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
 +      current_parsing_scope = CONFIG_SCOPE_SYSTEM;
 +      if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0))
                ret += git_config_from_file(fn, git_etc_gitconfig(),
                                            data);
 -              found += 1;
 -      }
  
 -      if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) {
 +      current_parsing_scope = CONFIG_SCOPE_GLOBAL;
 +      if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
                ret += git_config_from_file(fn, xdg_config, data);
 -              found += 1;
 -      }
  
 -      if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) {
 +      if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
                ret += git_config_from_file(fn, user_config, data);
 -              found += 1;
 -      }
  
 -      if (repo_config && !access_or_die(repo_config, R_OK, 0)) {
 +      current_parsing_scope = CONFIG_SCOPE_REPO;
 +      if (repo_config && !access_or_die(repo_config, R_OK, 0))
                ret += git_config_from_file(fn, repo_config, data);
 -              found += 1;
 -      }
  
 -      switch (git_config_from_parameters(fn, data)) {
 -      case -1: /* error */
 +      current_parsing_scope = CONFIG_SCOPE_CMDLINE;
 +      if (git_config_from_parameters(fn, data) < 0)
                die(_("unable to parse command-line config"));
 -              break;
 -      case 0: /* found nothing */
 -              break;
 -      default: /* found at least one item */
 -              found++;
 -              break;
 -      }
  
 +      current_parsing_scope = CONFIG_SCOPE_UNKNOWN;
        free(xdg_config);
        free(user_config);
        free(repo_config);
diff --cc diff-no-index.c
Simple merge
diff --cc environment.c
Simple merge
diff --cc git.c
Simple merge
diff --cc pager.c
Simple merge
index 3c6d08cd095152cab9abeb038c1b4f82440c55cd,0000000000000000000000000000000000000000..83a4f2ab86999876ecfb78c7e6dd108eb09b04ae
mode 100644,000000..100644
--- /dev/null
@@@ -1,188 -1,0 +1,191 @@@
 +#include "cache.h"
 +#include "string-list.h"
 +
 +/*
 + * This program exposes the C API of the configuration mechanism
 + * as a set of simple commands in order to facilitate testing.
 + *
 + * Reads stdin and prints result of command to stdout:
 + *
 + * get_value -> prints the value with highest priority for the entered key
 + *
 + * get_value_multi -> prints all values for the entered key in increasing order
 + *                 of priority
 + *
 + * get_int -> print integer value for the entered key or die
 + *
 + * get_bool -> print bool value for the entered key or die
 + *
 + * get_string -> print string value for the entered key or die
 + *
 + * configset_get_value -> returns value with the highest priority for the entered key
 + *                    from a config_set constructed from files entered as arguments.
 + *
 + * configset_get_value_multi -> returns value_list for the entered key sorted in
 + *                            ascending order of priority from a config_set
 + *                            constructed from files entered as arguments.
 + *
 + * iterate -> iterate over all values using git_config(), and print some
 + *            data for each
 + *
 + * Examples:
 + *
 + * To print the value with highest priority for key "foo.bAr Baz.rock":
 + *    test-config get_value "foo.bAr Baz.rock"
 + *
 + */
 +
 +static const char *scope_name(enum config_scope scope)
 +{
 +      switch (scope) {
 +      case CONFIG_SCOPE_SYSTEM:
 +              return "system";
 +      case CONFIG_SCOPE_GLOBAL:
 +              return "global";
 +      case CONFIG_SCOPE_REPO:
 +              return "repo";
 +      case CONFIG_SCOPE_CMDLINE:
 +              return "cmdline";
 +      default:
 +              return "unknown";
 +      }
 +}
 +static int iterate_cb(const char *var, const char *value, void *data)
 +{
 +      static int nr;
 +
 +      if (nr++)
 +              putchar('\n');
 +
 +      printf("key=%s\n", var);
 +      printf("value=%s\n", value ? value : "(null)");
 +      printf("origin=%s\n", current_config_origin_type());
 +      printf("name=%s\n", current_config_name());
 +      printf("scope=%s\n", scope_name(current_config_scope()));
 +
 +      return 0;
 +}
 +
 +int cmd_main(int argc, const char **argv)
 +{
 +      int i, val;
 +      const char *v;
 +      const struct string_list *strptr;
 +      struct config_set cs;
++
++      setup_git_directory();
++
 +      git_configset_init(&cs);
 +
 +      if (argc < 2) {
 +              fprintf(stderr, "Please, provide a command name on the command-line\n");
 +              goto exit1;
 +      } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
 +              if (!git_config_get_value(argv[2], &v)) {
 +                      if (!v)
 +                              printf("(NULL)\n");
 +                      else
 +                              printf("%s\n", v);
 +                      goto exit0;
 +              } else {
 +                      printf("Value not found for \"%s\"\n", argv[2]);
 +                      goto exit1;
 +              }
 +      } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
 +              strptr = git_config_get_value_multi(argv[2]);
 +              if (strptr) {
 +                      for (i = 0; i < strptr->nr; i++) {
 +                              v = strptr->items[i].string;
 +                              if (!v)
 +                                      printf("(NULL)\n");
 +                              else
 +                                      printf("%s\n", v);
 +                      }
 +                      goto exit0;
 +              } else {
 +                      printf("Value not found for \"%s\"\n", argv[2]);
 +                      goto exit1;
 +              }
 +      } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
 +              if (!git_config_get_int(argv[2], &val)) {
 +                      printf("%d\n", val);
 +                      goto exit0;
 +              } else {
 +                      printf("Value not found for \"%s\"\n", argv[2]);
 +                      goto exit1;
 +              }
 +      } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
 +              if (!git_config_get_bool(argv[2], &val)) {
 +                      printf("%d\n", val);
 +                      goto exit0;
 +              } else {
 +                      printf("Value not found for \"%s\"\n", argv[2]);
 +                      goto exit1;
 +              }
 +      } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
 +              if (!git_config_get_string_const(argv[2], &v)) {
 +                      printf("%s\n", v);
 +                      goto exit0;
 +              } else {
 +                      printf("Value not found for \"%s\"\n", argv[2]);
 +                      goto exit1;
 +              }
 +      } else if (!strcmp(argv[1], "configset_get_value")) {
 +              for (i = 3; i < argc; i++) {
 +                      int err;
 +                      if ((err = git_configset_add_file(&cs, argv[i]))) {
 +                              fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
 +                              goto exit2;
 +                      }
 +              }
 +              if (!git_configset_get_value(&cs, argv[2], &v)) {
 +                      if (!v)
 +                              printf("(NULL)\n");
 +                      else
 +                              printf("%s\n", v);
 +                      goto exit0;
 +              } else {
 +                      printf("Value not found for \"%s\"\n", argv[2]);
 +                      goto exit1;
 +              }
 +      } else if (!strcmp(argv[1], "configset_get_value_multi")) {
 +              for (i = 3; i < argc; i++) {
 +                      int err;
 +                      if ((err = git_configset_add_file(&cs, argv[i]))) {
 +                              fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
 +                              goto exit2;
 +                      }
 +              }
 +              strptr = git_configset_get_value_multi(&cs, argv[2]);
 +              if (strptr) {
 +                      for (i = 0; i < strptr->nr; i++) {
 +                              v = strptr->items[i].string;
 +                              if (!v)
 +                                      printf("(NULL)\n");
 +                              else
 +                                      printf("%s\n", v);
 +                      }
 +                      goto exit0;
 +              } else {
 +                      printf("Value not found for \"%s\"\n", argv[2]);
 +                      goto exit1;
 +              }
 +      } else if (!strcmp(argv[1], "iterate")) {
 +              git_config(iterate_cb, NULL);
 +              goto exit0;
 +      }
 +
 +      die("%s: Please check the syntax and the function name", argv[0]);
 +
 +exit0:
 +      git_configset_clear(&cs);
 +      return 0;
 +
 +exit1:
 +      git_configset_clear(&cs);
 +      return 1;
 +
 +exit2:
 +      git_configset_clear(&cs);
 +      return 2;
 +}
Simple merge