提交 0e94ee94 编写于 作者: J Jeff King 提交者: Junio C Hamano

hash-object: always try to set up the git repository

When "hash-object" is run without "-w", we don't need to be
in a git repository at all; we can just hash the object and
write its sha1 to stdout. However, if we _are_ in a git
repository, we would want to know that so we can follow the
normal rules for respecting config, .gitattributes, etc.

This happens to work at the top-level of a git repository
because we blindly read ".git/config", but as the included
test shows, it does not work when you are in a subdirectory.

The solution is to just do a "gentle" setup in this case. We
already take care to use prefix_filename() on any filename
arguments we get (to handle the "-w" case), so we don't need
to do anything extra to handle the side effects of repo
setup.

An alternative would be to specify RUN_SETUP_GENTLY for this
command in git.c, and then die if "-w" is set but we are not
in a repository. However, the error messages generated at
the time of setup_git_directory() are more detailed, so it's
better to find out which mode we are in, and then call the
appropriate function.
Signed-off-by: NJeff King <peff@peff.net>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 0b65a8db
...@@ -87,6 +87,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix) ...@@ -87,6 +87,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
int stdin_paths = 0; int stdin_paths = 0;
int no_filters = 0; int no_filters = 0;
int literally = 0; int literally = 0;
int nongit = 0;
unsigned flags = HASH_FORMAT_CHECK; unsigned flags = HASH_FORMAT_CHECK;
const char *vpath = NULL; const char *vpath = NULL;
const struct option hash_object_options[] = { const struct option hash_object_options[] = {
...@@ -107,12 +108,14 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix) ...@@ -107,12 +108,14 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, NULL, hash_object_options, argc = parse_options(argc, argv, NULL, hash_object_options,
hash_object_usage, 0); hash_object_usage, 0);
if (flags & HASH_WRITE_OBJECT) { if (flags & HASH_WRITE_OBJECT)
prefix = setup_git_directory(); prefix = setup_git_directory();
prefix_length = prefix ? strlen(prefix) : 0; else
if (vpath && prefix) prefix = setup_git_directory_gently(&nongit);
vpath = prefix_filename(prefix, prefix_length, vpath);
} prefix_length = prefix ? strlen(prefix) : 0;
if (vpath && prefix)
vpath = prefix_filename(prefix, prefix_length, vpath);
git_config(git_default_config, NULL); git_config(git_default_config, NULL);
......
...@@ -121,6 +121,17 @@ test_expect_success 'check that appropriate filter is invoke when --path is used ...@@ -121,6 +121,17 @@ test_expect_success 'check that appropriate filter is invoke when --path is used
git config --unset core.autocrlf git config --unset core.autocrlf
' '
test_expect_success 'gitattributes also work in a subdirectory' '
mkdir subdir &&
(
cd subdir &&
subdir_sha0=$(git hash-object ../file0) &&
subdir_sha1=$(git hash-object ../file1) &&
test "$file0_sha" = "$subdir_sha0" &&
test "$file1_sha" = "$subdir_sha1"
)
'
test_expect_success 'check that --no-filters option works' ' test_expect_success 'check that --no-filters option works' '
echo fooQ | tr Q "\\015" >file0 && echo fooQ | tr Q "\\015" >file0 &&
cp file0 file1 && cp file0 file1 &&
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册