* [PATCH] Move worktree setup out of setup_git_directory*
@ 2008-02-24 4:46 Nguyễn Thái Ngọc Duy
2008-02-25 1:13 ` Junio C Hamano
0 siblings, 1 reply; 4+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2008-02-24 4:46 UTC (permalink / raw)
To: git; +Cc: Johannes Schindelin
setup_git_directory* now work as if there is no worktree. It may set
some worktree-related variables but its prefix (and current directory)
should not be affected by worktree settings.
Previously only setup_git_directory() takes worktree into account when
calculating prefix (with an exception). This behaviour possibly gives
commands that do only call setup_git_directory_gently() wrong prefix.
The behavior does not expose often as setup_git_directory_gently()
tries to make correct prefix if both GIT_DIR and GIT_WORK_TREE are set
(the mentioned exception). Only if core.worktree is used, the defect
is summoned.
setup_work_tree() will now take the role of recalculating prefix when
worktree is required.
This also effectively reverts dd5c8af (Fix
setup_git_directory_gently() with relative GIT_DIR & GIT_WORK_TREE).
The problem is IMHO that git-diff does not setup worktree when it
needs it, so setting up worktree from setup_git_directory_gently() is
a fix in a wrong place.
While at it, fix builtin-diff-files.c as well with the same problem.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
setup_git_directory() is now just a thin wrapper around _gently(). Sweet.
builtin-blame.c | 4 +-
builtin-diff-files.c | 4 ++-
builtin-diff.c | 5 ++-
builtin-ls-files.c | 10 +++---
builtin-rev-parse.c | 2 +
builtin-rm.c | 2 +-
cache.h | 2 +-
git.c | 2 +-
setup.c | 80 ++++++++++++++++---------------------------------
t/t1501-worktree.sh | 30 ++++++++++++++++++-
10 files changed, 74 insertions(+), 67 deletions(-)
diff --git a/builtin-blame.c b/builtin-blame.c
index bfd562d..f5a878b 100644
--- a/builtin-blame.c
+++ b/builtin-blame.c
@@ -2291,6 +2291,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
/* (3) */
if (argc <= i)
usage(blame_usage);
+ prefix = setup_work_tree(prefix);
path = add_prefix(prefix, argv[i]);
if (i + 1 == argc - 1) {
final_commit_name = argv[i + 1];
@@ -2306,7 +2307,6 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
else if (i != argc - 1)
usage(blame_usage); /* garbage at end */
- setup_work_tree();
if (!has_path_in_work_tree(path))
die("cannot stat path %s: %s",
path, strerror(errno));
@@ -2354,7 +2354,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
* do not default to HEAD, but use the working tree
* or "--contents".
*/
- setup_work_tree();
+ setup_work_tree(prefix);
sb.final = fake_working_tree_commit(path, contents_from);
add_pending_object(&revs, &(sb.final->object), ":");
}
diff --git a/builtin-diff-files.c b/builtin-diff-files.c
index 4abe3c2..e3cc5cc 100644
--- a/builtin-diff-files.c
+++ b/builtin-diff-files.c
@@ -26,8 +26,10 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
argc = 0;
- else
+ else {
+ rev.prefix = prefix = setup_work_tree(prefix);
argc = setup_revisions(argc, argv, &rev, NULL);
+ }
if (!rev.diffopt.output_format)
rev.diffopt.output_format = DIFF_FORMAT_RAW;
result = run_diff_files_cmd(&rev, argc, argv);
diff --git a/builtin-diff.c b/builtin-diff.c
index 444ff2f..93221a0 100644
--- a/builtin-diff.c
+++ b/builtin-diff.c
@@ -244,8 +244,11 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
argc = 0;
- else
+ else {
+ rev.prefix = prefix = setup_work_tree(prefix);
+ rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
argc = setup_revisions(argc, argv, &rev, NULL);
+ }
if (!rev.diffopt.output_format) {
rev.diffopt.output_format = DIFF_FORMAT_PATCH;
if (diff_setup_done(&rev.diffopt) < 0)
diff --git a/builtin-ls-files.c b/builtin-ls-files.c
index 25dbfb4..4d833e1 100644
--- a/builtin-ls-files.c
+++ b/builtin-ls-files.c
@@ -23,7 +23,7 @@ static int show_valid_bit;
static int line_terminator = '\n';
static int prefix_len;
-static int prefix_offset;
+static int prefix_offset = -1;
static const char **pathspec;
static int error_unmatch;
static char *ps_matched;
@@ -435,8 +435,6 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
struct dir_struct dir;
memset(&dir, 0, sizeof(dir));
- if (prefix)
- prefix_offset = strlen(prefix);
git_config(git_default_config);
for (i = 1; i < argc; i++) {
@@ -568,8 +566,10 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
break;
}
- if (require_work_tree && !is_inside_work_tree())
- setup_work_tree();
+ if (require_work_tree)
+ prefix = setup_work_tree(prefix);
+ if (prefix_offset == -1)
+ prefix_offset = prefix ? strlen(prefix) : 0;
pathspec = get_pathspec(prefix, argv + i);
diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c
index b9af1a5..9f05cb3 100644
--- a/builtin-rev-parse.c
+++ b/builtin-rev-parse.c
@@ -472,6 +472,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
continue;
}
if (!strcmp(arg, "--show-prefix")) {
+ if (is_inside_work_tree())
+ prefix = setup_work_tree(prefix);
if (prefix)
puts(prefix);
continue;
diff --git a/builtin-rm.c b/builtin-rm.c
index c0a8bb6..2c15d66 100644
--- a/builtin-rm.c
+++ b/builtin-rm.c
@@ -156,7 +156,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
usage_with_options(builtin_rm_usage, builtin_rm_options);
if (!index_only)
- setup_work_tree();
+ prefix = setup_work_tree(prefix);
pathspec = get_pathspec(prefix, argv);
seen = NULL;
diff --git a/cache.h b/cache.h
index a5b9ffd..dc768db 100644
--- a/cache.h
+++ b/cache.h
@@ -282,7 +282,7 @@ extern const char *get_git_work_tree(void);
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
extern const char **get_pathspec(const char *prefix, const char **pathspec);
-extern void setup_work_tree(void);
+extern const char *setup_work_tree(const char *prefix);
extern const char *setup_git_directory_gently(int *);
extern const char *setup_git_directory(void);
extern const char *prefix_path(const char *prefix, int len, const char *path);
diff --git a/git.c b/git.c
index 4f804a2..90451ee 100644
--- a/git.c
+++ b/git.c
@@ -252,7 +252,7 @@ static int run_command(struct cmd_struct *p, int argc, const char **argv)
if (p->option & USE_PAGER)
setup_pager();
if (p->option & NEED_WORK_TREE)
- setup_work_tree();
+ prefix = setup_work_tree(prefix);
trace_argv_printf(argv, "trace: built-in: git");
diff --git a/setup.c b/setup.c
index 89c81e5..b181b24 100644
--- a/setup.c
+++ b/setup.c
@@ -262,38 +262,25 @@ int is_inside_work_tree(void)
return inside_work_tree;
}
-/*
- * set_work_tree() is only ever called if you set GIT_DIR explicitely.
- * The old behaviour (which we retain here) is to set the work tree root
- * to the cwd, unless overridden by the config, the command line, or
- * GIT_WORK_TREE.
- */
-static const char *set_work_tree(const char *dir)
-{
- char buffer[PATH_MAX + 1];
-
- if (!getcwd(buffer, sizeof(buffer)))
- die ("Could not get the current working directory");
- git_work_tree_cfg = xstrdup(buffer);
- inside_work_tree = 1;
-
- return NULL;
-}
-
-void setup_work_tree(void)
+const char *setup_work_tree(const char *prefix)
{
const char *work_tree, *git_dir;
- static int initialized = 0;
+ static char buffer[PATH_MAX + 1];
+ char *rel;
- if (initialized)
- return;
- work_tree = get_git_work_tree();
git_dir = get_git_dir();
if (!is_absolute_path(git_dir))
set_git_dir(make_absolute_path(git_dir));
- if (!work_tree || chdir(work_tree))
+ work_tree = get_git_work_tree();
+ if (!work_tree)
+ die("This operation must be run in a work tree");
+ if (prefix && chdir(prefix))
+ die ("Could not jump back into original cwd");
+ rel = get_relative_cwd(buffer, PATH_MAX, work_tree);
+ trace_printf("test: worktree = %s\n", rel ? rel : "NULL");
+ if (chdir(work_tree))
die("This operation must be run in a work tree");
- initialized = 1;
+ return rel && *rel ? strcat(rel, "/") : NULL;
}
static int check_repository_format_gently(int *nongit_ok)
@@ -336,24 +323,21 @@ const char *setup_git_directory_gently(int *nongit_ok)
static char buffer[1024 + 1];
const char *retval;
+ /*
+ * The old behaviour (which we retain here) is to set
+ * the work tree root to the cwd, unless overridden by
+ * the config, the command line, or GIT_WORK_TREE.
+ */
if (!work_tree_env) {
- retval = set_work_tree(gitdirenv);
- /* config may override worktree */
- if (check_repository_format_gently(nongit_ok))
- return NULL;
- return retval;
+ char buffer[PATH_MAX + 1];
+
+ if (!getcwd(buffer, sizeof(buffer)))
+ die ("Could not get the current working directory");
+ git_work_tree_cfg = xstrdup(buffer);
+ inside_work_tree = 1;
}
- if (check_repository_format_gently(nongit_ok))
- return NULL;
- retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
- get_git_work_tree());
- if (!retval || !*retval)
- return NULL;
- set_git_dir(make_absolute_path(gitdirenv));
- if (chdir(work_tree_env) < 0)
- die ("Could not chdir to %s", work_tree_env);
- strcat(buffer, "/");
- return retval;
+ check_repository_format_gently(nongit_ok);
+ return NULL;
}
if (nongit_ok) {
*nongit_ok = 1;
@@ -462,17 +446,5 @@ int check_repository_format(void)
const char *setup_git_directory(void)
{
- const char *retval = setup_git_directory_gently(NULL);
-
- /* If the work tree is not the default one, recompute prefix */
- if (inside_work_tree < 0) {
- static char buffer[PATH_MAX + 1];
- char *rel;
- if (retval && chdir(retval))
- die ("Could not jump back into original cwd");
- rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
- return rel && *rel ? strcat(rel, "/") : NULL;
- }
-
- return retval;
+ return setup_git_directory_gently(NULL);
}
diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
index 7ee3820..2b1629b 100755
--- a/t/t1501-worktree.sh
+++ b/t/t1501-worktree.sh
@@ -58,6 +58,34 @@ cd sub/dir || exit 1
test_rev_parse 'subdirectory' false false true sub/dir/
cd ../../.. || exit 1
+test_expect_success 'git ls-files -o gets correct prefix' '
+ (cd work/sub && touch untracked &&
+ test "$(git ls-files -o)" = untracked)'
+
+rm work/sub/untracked || exit 1
+
+cat <<EOF >expected
+:100644 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0000000000000000000000000000000000000000 M sub/tracked
+EOF
+
+test_expect_success 'git diff-files gets correct prefix' '
+ (cd work/sub && touch tracked &&
+ git add tracked && echo modified > tracked &&
+ git diff-files > ../../result &&
+ git rm -f tracked) &&
+ cmp result expected'
+
+cat <<EOF >expected
+:100644 100644 e69de29... 0000000... M sub/tracked
+EOF
+
+test_expect_success 'git diff gets correct prefix' '
+ (cd work/sub && touch tracked &&
+ git add tracked && echo modified > tracked &&
+ git diff --raw > ../../result &&
+ git rm -f tracked) &&
+ cmp result expected'
+
say "GIT_WORK_TREE=relative path (override core.worktree)"
export GIT_DIR=$(pwd)/repo.git
export GIT_CONFIG=$GIT_DIR/config
@@ -103,7 +131,7 @@ test_expect_success 'repo finds its work tree from work tree, too' '
test sub/dir/tracked = "$(git ls-files)")
'
-test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '
+test_expect_success '"git diff" setup worktree properly' '
cd repo.git/work/sub/dir &&
GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
git diff --exit-code tracked &&
--
1.5.4.2.281.g28d0e
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] Move worktree setup out of setup_git_directory*
2008-02-24 4:46 [PATCH] Move worktree setup out of setup_git_directory* Nguyễn Thái Ngọc Duy
@ 2008-02-25 1:13 ` Junio C Hamano
2008-02-25 13:17 ` Nguyen Thai Ngoc Duy
0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2008-02-25 1:13 UTC (permalink / raw)
To: Nguyễn Thái Ngọc Duy; +Cc: git, Johannes Schindelin
Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:
> setup_git_directory* now work as if there is no worktree. It may set
> some worktree-related variables but its prefix (and current directory)
> should not be affected by worktree settings.
> ...
> setup_work_tree() will now take the role of recalculating prefix when
> worktree is required.
This definitely needs documented in-tree, Documentation/technical/.
I think we have a good understanding from the end-user point-of-view
how these things work:
- If you have GIT_DIR exported, then no discovery is attempted.
We use the GIT_DIR you set it, and the repository lives
there. $GIT_DIR/config is the repository config.
- Otherwise we do the usual discovery going up to find the
repository.
- If you have GIT_WORK_TREE exported, or otherwise if the
config has core.worktree, that's where your worktree is.
Otherwise, if you have GIT_DIR exported, you do not have a
worktree. Else one level above your $GIT_DIR is the toplevel
of your worktree.
We would need a set of rules for application writers, written
like this (here is just an example of _how_ the rules might be
written, I do not think the contents are correct):
- At startup:
- If the command always need to run from a repository, call
setup_git_directory(), which would complain and die if you
are outside.
- If the command can optionally run in a repository, use
setup_git_directory_gently(&nongit_ok); it won't complain
but sets nongit_ok to true if run outside a repository.
In either case, they return prefix string to be used for
setup_work_tree() later.
In either case, there is no chdir() done at this point.
- After calling setup_git_directory(), or you made sure you are
in a repository after callilng setup_git_directory_gently(),
call setup_work_tree(). This will chdir to the top of the
work tree.
- When you need to know where $GIT_DIR is, call get_git_dir().
When you need to know where the work tree is, call
get_git_work_tree(). However, it is an error to call these
functions before calling setup_git_directory and
setup_work_tree.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Move worktree setup out of setup_git_directory*
2008-02-25 1:13 ` Junio C Hamano
@ 2008-02-25 13:17 ` Nguyen Thai Ngoc Duy
2008-02-25 19:01 ` Junio C Hamano
0 siblings, 1 reply; 4+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2008-02-25 13:17 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git, Johannes Schindelin
On Mon, Feb 25, 2008 at 8:13 AM, Junio C Hamano <gitster@pobox.com> wrote:
> - At startup:
>
> - If the command always need to run from a repository, call
> setup_git_directory(), which would complain and die if you
> are outside.
>
> - If the command can optionally run in a repository, use
> setup_git_directory_gently(&nongit_ok); it won't complain
> but sets nongit_ok to true if run outside a repository.
>
> In either case, they return prefix string to be used for
> setup_work_tree() later.
>
> In either case, there is no chdir() done at this point.
Interesting. It currently does chdir(). But it is pointless now as
worktree should not be accessed until setup_work_tree() is called.
setup_git_directory* should not return a prefix at all.
> - After calling setup_git_directory(), or you made sure you are
> in a repository after callilng setup_git_directory_gently(),
> call setup_work_tree(). This will chdir to the top of the
> work tree.
>
> - When you need to know where $GIT_DIR is, call get_git_dir().
> When you need to know where the work tree is, call
> get_git_work_tree(). However, it is an error to call these
> functions before calling setup_git_directory and
> setup_work_tree.
I guess I just need to verify your statements and put them to
Doc/technical/api-setup.txt ;)
--
Duy
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Move worktree setup out of setup_git_directory*
2008-02-25 13:17 ` Nguyen Thai Ngoc Duy
@ 2008-02-25 19:01 ` Junio C Hamano
0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2008-02-25 19:01 UTC (permalink / raw)
To: Nguyen Thai Ngoc Duy; +Cc: Junio C Hamano, git, Johannes Schindelin
"Nguyen Thai Ngoc Duy" <pclouds@gmail.com> writes:
> Interesting. It currently does chdir(). But it is pointless now as
> worktree should not be accessed until setup_work_tree() is called.
> setup_git_directory* should not return a prefix at all.
> ...
> I guess I just need to verify your statements and put them to
> Doc/technical/api-setup.txt ;)
The above, as I said, was written out of thin air without
re-reading the current code nor carefully reading what your
patch does, so I am afraid there needs a lot more than that
to happen before we have something to validate the callers
against, but I think you got why I think it is a good idea to
have that documented.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2008-02-25 19:02 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-02-24 4:46 [PATCH] Move worktree setup out of setup_git_directory* Nguyễn Thái Ngọc Duy
2008-02-25 1:13 ` Junio C Hamano
2008-02-25 13:17 ` Nguyen Thai Ngoc Duy
2008-02-25 19:01 ` Junio C Hamano
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).