Subject: [WIP PATCH 19/26] apply: move up gitdir setup to run_builtin()

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Tue, 16 Feb 2010 23:05:10 +0700

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 builtin-apply.c |   13 ++++++-------
 git.c           |    2 +-
 2 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/builtin-apply.c b/builtin-apply.c
index 2a1004d..44bb74f 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -26,7 +26,7 @@
  *  --cached updates only the cache without ever touching the working tree.
  */
 static const char *prefix;
-static int prefix_length = -1;
+static int prefix_length;
 static int newfd = -1;
 
 static int unidiff_zero;
@@ -2441,7 +2441,7 @@ static int apply_binary(struct image *img, struct patch *patch)
 		return 0; /* deletion patch */
 	}
 
-	if (has_sha1_file(sha1)) {
+	if (startup_info->have_repository && has_sha1_file(sha1)) {
 		/* We already have the postimage */
 		enum object_type type;
 		unsigned long size;
@@ -3512,11 +3512,10 @@ static int option_parse_directory(const struct option *opt,
 	return 0;
 }
 
-int cmd_apply(int argc, const char **argv, const char *unused_prefix)
+int cmd_apply(int argc, const char **argv, const char *prefix_)
 {
 	int i;
 	int errs = 0;
-	int is_not_gitdir;
 	int binary;
 	int force_apply = 0;
 
@@ -3589,7 +3588,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
 		OPT_END()
 	};
 
-	prefix = setup_git_directory_gently(&is_not_gitdir);
+	prefix = prefix_;
 	prefix_length = prefix ? strlen(prefix) : 0;
 	git_config(git_apply_config, NULL);
 	if (apply_default_whitespace)
@@ -3604,10 +3603,10 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
 		apply = apply_verbosely = 1;
 	if (!force_apply && (diffstat || numstat || summary || check || fake_ancestor))
 		apply = 0;
-	if (check_index && is_not_gitdir)
+	if (check_index && !startup_info->have_repository)
 		die("--index outside a repository");
 	if (cached) {
-		if (is_not_gitdir)
+		if (!startup_info->have_repository)
 			die("--cached outside a repository");
 		check_index = 1;
 	}
diff --git a/git.c b/git.c
index ee596bb..96ccedd 100644
--- a/git.c
+++ b/git.c
@@ -307,7 +307,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
 		{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
 		{ "annotate", cmd_annotate, RUN_SETUP },
-		{ "apply", cmd_apply },
+		{ "apply", cmd_apply, RUN_SETUP_GENTLY },
 		{ "archive", cmd_archive, RUN_SETUP_GENTLY },
 		{ "bisect--helper", cmd_bisect__helper, RUN_SETUP | NEED_WORK_TREE },
 		{ "blame", cmd_blame, RUN_SETUP },
-- 
1.7.0.195.g637a2