]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Merge branch 'jk/startup-info' into maint
authorJunio C Hamano <gitster@pobox.com>
Fri, 15 Apr 2016 01:57:46 +0000 (18:57 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 15 Apr 2016 01:57:46 +0000 (18:57 -0700)
The startup_info data, which records if we are working inside a
repository (among other things), are now uniformly available to Git
subcommand implementations, and Git avoids attempting to touch
references when we are not in a repository.

* jk/startup-info:
  use setup_git_directory() in test-* programs
  grep: turn off gitlink detection for --no-index
  mailmap: do not resolve blobs in a non-repository
  remote: don't resolve HEAD in non-repository
  setup: set startup_info->have_repository more reliably
  setup: make startup_info available everywhere

13 files changed:
builtin/grep.c
builtin/init-db.c
cache.h
environment.c
git.c
mailmap.c
remote.c
setup.c
sha1_name.c
t/t1506-rev-parse-diagnosis.sh
t/t7810-grep.sh
test-match-trees.c
test-revision-walking.c

index aa7435f380e95d87982cd1ae71e91bc53e77af50..111b6f6cf1a61b0fb481101fcdc5a9f3f48c4650 100644 (file)
@@ -522,12 +522,14 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
 }
 
 static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
-                         int exc_std)
+                         int exc_std, int use_index)
 {
        struct dir_struct dir;
        int i, hit = 0;
 
        memset(&dir, 0, sizeof(dir));
+       if (!use_index)
+               dir.flags |= DIR_NO_GITLINKS;
        if (exc_std)
                setup_standard_excludes(&dir);
 
@@ -902,7 +904,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
                if (list.nr)
                        die(_("--no-index or --untracked cannot be used with revs."));
-               hit = grep_directory(&opt, &pathspec, use_exclude);
+               hit = grep_directory(&opt, &pathspec, use_exclude, use_index);
        } else if (0 <= opt_exclude) {
                die(_("--[no-]exclude-standard cannot be used for tracked contents."));
        } else if (!list.nr) {
index 6223b7d46af346b0d96870ee0c647ab9a5440b33..da531f6b766fb300d8c1fff0d94a0756bbbf1f6f 100644 (file)
@@ -322,6 +322,7 @@ int set_git_dir_init(const char *git_dir, const char *real_git_dir,
                set_git_dir(real_path(git_dir));
                git_link = NULL;
        }
+       startup_info->have_repository = 1;
        return 0;
 }
 
diff --git a/cache.h b/cache.h
index b829410f6da0afc14353b4621d2fdf874181a9f7..9f09540bbca587cb2ba0135a69d65aa966b09622 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -1771,7 +1771,7 @@ int split_cmdline(char *cmdline, const char ***argv);
 /* Takes a negative value returned by split_cmdline */
 const char *split_cmdline_strerror(int cmdline_errno);
 
-/* git.c */
+/* setup.c */
 struct startup_info {
        int have_repository;
        const char *prefix;
index 6dec9d0403f11579a7ab316db87a1dbbc58e71b2..6cc0a7780f790d6cea3774acd989a57e52934f91 100644 (file)
@@ -64,7 +64,6 @@ int grafts_replace_parents = 1;
 int core_apply_sparse_checkout;
 int merge_log_config = -1;
 int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
-struct startup_info *startup_info;
 unsigned long pack_size_limit_cfg;
 
 #ifndef PROTECT_HFS_DEFAULT
diff --git a/git.c b/git.c
index 6cc0c077f9761f9b56e5fb7e666722b437c4fae4..968a8a464588f10c5c1564440e06d5e5afe8d37a 100644 (file)
--- a/git.c
+++ b/git.c
@@ -15,7 +15,6 @@ const char git_more_info_string[] =
           "concept guides. See 'git help <command>' or 'git help <concept>'\n"
           "to read about a specific subcommand or concept.");
 
-static struct startup_info git_startup_info;
 static int use_pager = -1;
 static char *orig_cwd;
 static const char *env_names[] = {
@@ -637,8 +636,6 @@ int main(int argc, char **av)
        const char *cmd;
        int done_help = 0;
 
-       startup_info = &git_startup_info;
-
        cmd = git_extract_argv0_path(argv[0]);
        if (!cmd)
                cmd = "git-help";
index f4a0f1cf27bf0a10626cc5b1fd628e12cbda3a7e..972623709fdac5503328f7e252d5334dfcb461c6 100644 (file)
--- a/mailmap.c
+++ b/mailmap.c
@@ -250,7 +250,8 @@ int read_mailmap(struct string_list *map, char **repo_abbrev)
                git_mailmap_blob = "HEAD:.mailmap";
 
        err |= read_mailmap_file(map, ".mailmap", repo_abbrev);
-       err |= read_mailmap_blob(map, git_mailmap_blob, repo_abbrev);
+       if (startup_info->have_repository)
+               err |= read_mailmap_blob(map, git_mailmap_blob, repo_abbrev);
        err |= read_mailmap_file(map, git_mailmap_file, repo_abbrev);
        return err;
 }
index fc02698587c61d230200272ce4704403e1a6741f..28fd676acbbf4985422d74c9f1987dd0a5765039 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -455,7 +455,6 @@ static void read_config(void)
 {
        static int loaded;
        struct object_id oid;
-       const char *head_ref;
        int flag;
 
        if (loaded)
@@ -463,10 +462,12 @@ static void read_config(void)
        loaded = 1;
 
        current_branch = NULL;
-       head_ref = resolve_ref_unsafe("HEAD", 0, oid.hash, &flag);
-       if (head_ref && (flag & REF_ISSYMREF) &&
-           skip_prefix(head_ref, "refs/heads/", &head_ref)) {
-               current_branch = make_branch(head_ref, 0);
+       if (startup_info->have_repository) {
+               const char *head_ref = resolve_ref_unsafe("HEAD", 0, oid.hash, &flag);
+               if (head_ref && (flag & REF_ISSYMREF) &&
+                   skip_prefix(head_ref, "refs/heads/", &head_ref)) {
+                       current_branch = make_branch(head_ref, 0);
+               }
        }
        git_config(handle_config, NULL);
        alias_all_urls();
diff --git a/setup.c b/setup.c
index de1a2a7ea5973fef256328a26730117466c15172..3439ec6d815f823ed853251c9afa94d14d78556a 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -7,6 +7,9 @@ static int inside_work_tree = -1;
 static int work_tree_config_is_bogus;
 static struct string_list unknown_extensions = STRING_LIST_INIT_DUP;
 
+static struct startup_info the_startup_info;
+struct startup_info *startup_info = &the_startup_info;
+
 /*
  * The input parameter must contain an absolute path, and it must already be
  * normalized.
@@ -905,10 +908,9 @@ const char *setup_git_directory_gently(int *nongit_ok)
        else
                setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
 
-       if (startup_info) {
-               startup_info->have_repository = !nongit_ok || !*nongit_ok;
-               startup_info->prefix = prefix;
-       }
+       startup_info->have_repository = !nongit_ok || !*nongit_ok;
+       startup_info->prefix = prefix;
+
        return prefix;
 }
 
@@ -984,7 +986,9 @@ int check_repository_format_version(const char *var, const char *value, void *cb
 
 int check_repository_format(void)
 {
-       return check_repository_format_gently(get_git_dir(), NULL);
+       check_repository_format_gently(get_git_dir(), NULL);
+       startup_info->have_repository = 1;
+       return 0;
 }
 
 /*
index 3acf221f92f7a0857f17c0210044cacc325c8ce0..776101e8d709b924033a141efa38e3f734837c79 100644 (file)
@@ -1353,9 +1353,6 @@ static char *resolve_relative_path(const char *rel)
        if (!starts_with(rel, "./") && !starts_with(rel, "../"))
                return NULL;
 
-       if (!startup_info)
-               die("BUG: startup_info struct is not initialized.");
-
        if (!is_inside_work_tree())
                die("relative path syntax can't be used outside working tree.");
 
index 613d9bfe1bbb153d1c2e647aee26a60e07a6af86..86c2ff255d43d644caf81e6258d29dcea1db8e53 100755 (executable)
@@ -166,11 +166,6 @@ test_expect_success 'relative path when cwd is outside worktree' '
        grep "relative path syntax can.t be used outside working tree." error
 '
 
-test_expect_success 'relative path when startup_info is NULL' '
-       test_must_fail test-match-trees HEAD:./file.txt HEAD:./file.txt 2>error &&
-       grep "BUG: startup_info struct is not initialized." error
-'
-
 test_expect_success '<commit>:file correctly diagnosed after a pathname' '
        test_must_fail git rev-parse file.txt HEAD:file.txt 1>actual 2>error &&
        test_i18ngrep ! "exists on disk" error &&
index b540944408a6feb35f28ff7e18601db7a29f8635..1e72971a165efc127e6bda24fb0e4a3852d3c543 100755 (executable)
@@ -905,6 +905,33 @@ test_expect_success 'inside git repository but with --no-index' '
        )
 '
 
+test_expect_success 'grep --no-index descends into repos, but not .git' '
+       rm -fr non &&
+       mkdir -p non/git &&
+       (
+               GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
+               export GIT_CEILING_DIRECTORIES &&
+               cd non/git &&
+
+               echo magic >file &&
+               git init repo &&
+               (
+                       cd repo &&
+                       echo magic >file &&
+                       git add file &&
+                       git commit -m foo &&
+                       echo magic >.git/file
+               ) &&
+
+               cat >expect <<-\EOF &&
+               file
+               repo/file
+               EOF
+               git grep -l --no-index magic >actual &&
+               test_cmp expect actual
+       )
+'
+
 test_expect_success 'setup double-dash tests' '
 cat >double-dash <<EOF &&
 --
index 109f03e711803251b5b5d06bc42579784a326ba8..4dad7095f10bb324f043ff8132678488b65d4537 100644 (file)
@@ -6,6 +6,8 @@ int main(int ac, char **av)
        unsigned char hash1[20], hash2[20], shifted[20];
        struct tree *one, *two;
 
+       setup_git_directory();
+
        if (get_sha1(av[1], hash1))
                die("cannot parse %s as an object name", av[1]);
        if (get_sha1(av[2], hash2))
index 285f06b7ff262378150be80f2a09b4162ae3a564..3d0313354b3e100fe3624b58c61af7cc7e0f8e7b 100644 (file)
@@ -50,6 +50,8 @@ int main(int argc, char **argv)
        if (argc < 2)
                return 1;
 
+       setup_git_directory();
+
        if (!strcmp(argv[1], "run-twice")) {
                printf("1st\n");
                if (!run_revision_walk())