Merge branch 'rs/git-dir-cleanup'

* rs/git-dir-cleanup:
  Resurrect "git grep --no-index"
  setenv(GIT_DIR) clean-up

Conflicts:
	builtin-grep.c
	t/t7002-grep.sh
This commit is contained in:
Junio C Hamano 2010-02-13 15:09:33 -08:00
commit 263830c47b
5 changed files with 96 additions and 4 deletions

View File

@ -14,6 +14,7 @@
#include "userdiff.h"
#include "grep.h"
#include "quote.h"
#include "dir.h"
#ifndef NO_PTHREADS
#include "thread-utils.h"
@ -645,6 +646,24 @@ static int grep_object(struct grep_opt *opt, const char **paths,
die("unable to grep from object of type %s", typename(obj->type));
}
static int grep_directory(struct grep_opt *opt, const char **paths)
{
struct dir_struct dir;
int i, hit = 0;
memset(&dir, 0, sizeof(dir));
setup_standard_excludes(&dir);
fill_directory(&dir, paths);
for (i = 0; i < dir.nr; i++) {
hit |= grep_file(opt, dir.entries[i]->name);
if (hit && opt->status_only)
break;
}
free_grep_patterns(opt);
return hit;
}
static int context_callback(const struct option *opt, const char *arg,
int unset)
{
@ -739,9 +758,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
const char **paths = NULL;
int i;
int dummy;
int nongit = 0, use_index = 1;
struct option options[] = {
OPT_BOOLEAN(0, "cached", &cached,
"search in index instead of in the work tree"),
OPT_BOOLEAN(0, "index", &use_index,
"--no-index finds in contents not managed by git"),
OPT_GROUP(""),
OPT_BOOLEAN('v', "invert-match", &opt.invert,
"show non-matching lines"),
@ -824,6 +846,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
OPT_END()
};
prefix = setup_git_directory_gently(&nongit);
/*
* 'git grep -h', unlike 'git grep -h <pattern>', is a request
* to show usage information and exit.
@ -861,6 +885,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
PARSE_OPT_STOP_AT_NON_OPTION |
PARSE_OPT_NO_INTERNAL_HELP);
if (use_index && nongit)
/* die the same way as if we did it at the beginning */
setup_git_directory();
/*
* skip a -- separator; we know it cannot be
* separating revisions from pathnames if
@ -932,6 +960,18 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
paths[1] = NULL;
}
if (!use_index) {
int hit;
if (cached)
die("--cached cannot be used with --no-index.");
if (list.nr)
die("--no-index cannot be used with revs.");
hit = grep_directory(&opt, paths);
if (use_threads)
hit |= wait_all();
return !hit;
}
if (!list.nr) {
int hit;
if (!cached)

2
git.c
View File

@ -317,7 +317,7 @@ static void handle_internal_command(int argc, const char **argv)
{ "fsck-objects", cmd_fsck, RUN_SETUP },
{ "gc", cmd_gc, RUN_SETUP },
{ "get-tar-commit-id", cmd_get_tar_commit_id },
{ "grep", cmd_grep, RUN_SETUP | USE_PAGER },
{ "grep", cmd_grep, USE_PAGER },
{ "hash-object", cmd_hash_object },
{ "help", cmd_help },
{ "index-pack", cmd_index_pack },

2
path.c
View File

@ -336,7 +336,7 @@ char *enter_repo(char *path, int strict)
if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
validate_headref("HEAD") == 0) {
setenv(GIT_DIR_ENVIRONMENT, ".", 1);
set_git_dir(".");
check_repository_format();
return path;
}

View File

@ -404,9 +404,9 @@ const char *setup_git_directory_gently(int *nongit_ok)
inside_work_tree = 0;
if (offset != len) {
cwd[offset] = '\0';
setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
set_git_dir(cwd);
} else
setenv(GIT_DIR_ENVIRONMENT, ".", 1);
set_git_dir(".");
check_repository_format_gently(nongit_ok);
return NULL;
}

View File

@ -434,6 +434,58 @@ test_expect_success 'grep -Fi' '
test_cmp expected actual
'
test_expect_success 'outside of git repository' '
rm -fr non &&
mkdir -p non/git/sub &&
echo hello >non/git/file1 &&
echo world >non/git/sub/file2 &&
echo ".*o*" >non/git/.gitignore &&
{
echo file1:hello &&
echo sub/file2:world
} >non/expect.full &&
echo file2:world >non/expect.sub
(
GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
export GIT_CEILING_DIRECTORIES &&
cd non/git &&
test_must_fail git grep o &&
git grep --no-index o >../actual.full &&
test_cmp ../expect.full ../actual.full
cd sub &&
test_must_fail git grep o &&
git grep --no-index o >../../actual.sub &&
test_cmp ../../expect.sub ../../actual.sub
)
'
test_expect_success 'inside git repository but with --no-index' '
rm -fr is &&
mkdir -p is/git/sub &&
echo hello >is/git/file1 &&
echo world >is/git/sub/file2 &&
echo ".*o*" >is/git/.gitignore &&
{
echo file1:hello &&
echo sub/file2:world
} >is/expect.full &&
: >is/expect.empty &&
echo file2:world >is/expect.sub
(
cd is/git &&
git init &&
test_must_fail git grep o >../actual.full &&
test_cmp ../expect.empty ../actual.full &&
git grep --no-index o >../actual.full &&
test_cmp ../expect.full ../actual.full &&
cd sub &&
test_must_fail git grep o >../../actual.sub &&
test_cmp ../../expect.empty ../../actual.sub &&
git grep --no-index o >../../actual.sub &&
test_cmp ../../expect.sub ../../actual.sub
)
'
test_expect_success 'setup double-dash tests' '
cat >double-dash <<EOF &&
--