http-fetch: set up git directory before parsing pack hashes

In dd4b732df7 ("upload-pack: send part of packfile response as uri",
2020-06-10), the git http-fetch code learned how to take  ac --packfile
option.  This option takes an argument, which is the name of a packfile
hash, and parses it using parse_oid_hex.  It does so before calling
setup_git_directory.

However, in a SHA-256 repository this fails to work, since we have not
set the hash algorithm in use and parse_oid_hex fails as a consequence.
To ensure that we can parse packfile hashes of the right length, let's
set up the git directory before we start parsing arguments.

Since we still want to allow the invocation of -h to print the help when
we're not in a repository, gracefully handle us being outside of one and
produce an error after argument parsing has finished.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Reviewed-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
brian m. carlson 2020-07-29 23:14:18 +00:00 committed by Junio C Hamano
parent 6c2adf80e9
commit 439d3a17b6
1 changed files with 5 additions and 1 deletions

View File

@ -84,8 +84,11 @@ int cmd_main(int argc, const char **argv)
int get_verbosely = 0;
int get_recover = 0;
int packfile = 0;
int nongit;
struct object_id packfile_hash;
setup_git_directory_gently(&nongit);
while (arg < argc && argv[arg][0] == '-') {
const char *p;
@ -115,7 +118,8 @@ int cmd_main(int argc, const char **argv)
if (argc != arg + 2 - (commits_on_stdin || packfile))
usage(http_fetch_usage);
setup_git_directory();
if (nongit)
die(_("not a git repository"));
git_config(git_default_config, NULL);