Refactor working tree setup
Create a setup_work_tree() that can be used from any command requiring a working tree conditionally. Signed-off-by: Mike Hommey <mh@glandium.org> Acked-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Mike Hommey committed
Nov 3, 2007 at 12:23 UTC
59f0f2f33a420e9a14bb8cef20d38f508f0d098e
3 files changed
+13
-8
cache.h
+1
@@ -222,6 +222,7 @@ extern const char *get_git_work_tree(void);
222
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
223
224
extern const char **get_pathspec(const char *prefix, const char **pathspec);
225
+extern void setup_work_tree(void);
226
extern const char *setup_git_directory_gently(int *);
227
extern const char *setup_git_directory(void);
228
extern const char *prefix_path(const char *prefix, int len, const char *path);
git.c
+3
-8
@@ -249,14 +249,9 @@ static int run_command(struct cmd_struct *p, int argc, const char **argv)
249
prefix = setup_git_directory();
250
if (p->option & USE_PAGER)
251
setup_pager();
252
- if (p->option & NEED_WORK_TREE) {
253
- const char *work_tree = get_git_work_tree();
254
- const char *git_dir = get_git_dir();
255
- if (!is_absolute_path(git_dir))
256
- set_git_dir(make_absolute_path(git_dir));
257
- if (!work_tree || chdir(work_tree))
258
- die("%s must be run in a work tree", p->cmd);
259
- }
252
+ if (p->option & NEED_WORK_TREE)
253
+ setup_work_tree();
254
+
255
trace_argv_printf(argv, argc, "trace: built-in: git");
256
257
status = p->fn(argc, argv, prefix);
setup.c
+9
@@ -206,6 +206,15 @@ static const char *set_work_tree(const char *dir)
206
return NULL;
207
}
208
209
+void setup_work_tree(void) {
210
+ const char *work_tree = get_git_work_tree();
211
+ const char *git_dir = get_git_dir();
212
+ if (!is_absolute_path(git_dir))
213
+ set_git_dir(make_absolute_path(git_dir));
214
+ if (!work_tree || chdir(work_tree))
215
+ die("This operation must be run in a work tree");
216
+}
217
+
218
/*
219
* We cannot decide in this function whether we are in the work tree or
220
* not, since the config can only be read _after_ this function was called.