Skip to content

Commit 2321286

Browse files
peffgitster
authored andcommitted
archive: reorder option parsing and config reading
The archive command does three things during its initialization phase: 1. parse command-line options 2. setup the git directory 3. read config During phase (1), if we see any options that do not require a git directory (like "--list"), we handle them immediately and exit, making it safe to abort step (2) if we are not in a git directory. Step (3) must come after step (2), since the git directory may influence configuration. However, this leaves no possibility of configuration from step (3) impacting the command-line options in step (1) (which is useful, for example, for supporting user-configurable output formats). Instead, let's reorder this to: 1. setup the git directory, if it exists 2. read config 3. parse command-line options 4. if we are not in a git repository, die This should have the same external behavior, but puts configuration before command-line parsing. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2c162b5 commit 2321286

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

archive.c

+14-4
Original file line numberDiff line numberDiff line change
@@ -387,17 +387,27 @@ static int parse_archive_args(int argc, const char **argv,
387387
int write_archive(int argc, const char **argv, const char *prefix,
388388
int setup_prefix)
389389
{
390+
int nongit = 0;
390391
const struct archiver *ar = NULL;
391392
struct archiver_args args;
392393

393-
argc = parse_archive_args(argc, argv, &ar, &args);
394394
if (setup_prefix && prefix == NULL)
395-
prefix = setup_git_directory();
395+
prefix = setup_git_directory_gently(&nongit);
396+
397+
git_config(git_default_config, NULL);
398+
399+
argc = parse_archive_args(argc, argv, &ar, &args);
400+
if (nongit) {
401+
/*
402+
* We know this will die() with an error, so we could just
403+
* die ourselves; but its error message will be more specific
404+
* than what we could write here.
405+
*/
406+
setup_git_directory();
407+
}
396408

397409
parse_treeish_arg(argv, &args, prefix);
398410
parse_pathspec_arg(argv + 1, &args);
399411

400-
git_config(git_default_config, NULL);
401-
402412
return ar->write_archive(&args);
403413
}

0 commit comments

Comments
 (0)