From a9ca8a85ef493aac947f9da44a4fbb5724768897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Nov 2010 22:31:57 +0700 Subject: [PATCH 01/47] builtins: print setup info if repo is found MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- cache.h | 1 + git.c | 4 ++++ trace.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/cache.h b/cache.h index 33decd942d..d1bee5d518 100644 --- a/cache.h +++ b/cache.h @@ -1062,6 +1062,7 @@ __attribute__((format (printf, 1, 2))) extern void trace_printf(const char *format, ...); __attribute__((format (printf, 2, 3))) extern void trace_argv_printf(const char **argv, const char *format, ...); +extern void trace_repo_setup(const char *prefix); /* convert.c */ /* returns 1 if *dst was used */ diff --git a/git.c b/git.c index 0409ac9fd3..6793178210 100644 --- a/git.c +++ b/git.c @@ -264,6 +264,10 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv) use_pager = check_pager_config(p->cmd); if (use_pager == -1 && p->option & USE_PAGER) use_pager = 1; + + if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) && + startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */ + trace_repo_setup(prefix); } commit_pager_choice(); diff --git a/trace.c b/trace.c index 1e560cb0b9..bdb5d2fa77 100644 --- a/trace.c +++ b/trace.c @@ -131,3 +131,45 @@ void trace_argv_printf(const char **argv, const char *fmt, ...) if (need_close) close(fd); } + +static const char *quote_crnl(const char *path) +{ + static char new_path[PATH_MAX]; + const char *p2 = path; + char *p1 = new_path; + + if (!path) + return NULL; + + while (*p2) { + switch (*p2) { + case '\\': *p1++ = '\\'; *p1++ = '\\'; break; + case '\n': *p1++ = '\\'; *p1++ = 'n'; break; + case '\r': *p1++ = '\\'; *p1++ = 'r'; break; + default: + *p1++ = *p2; + } + p2++; + } + *p1 = '\0'; + return new_path; +} + +/* FIXME: move prefix to startup_info struct and get rid of this arg */ +void trace_repo_setup(const char *prefix) +{ + char cwd[PATH_MAX]; + char *trace = getenv("GIT_TRACE"); + + if (!trace || !strcmp(trace, "") || + !strcmp(trace, "0") || !strcasecmp(trace, "false")) + return; + + if (!getcwd(cwd, PATH_MAX)) + die("Unable to get current working directory"); + + trace_printf("setup: git_dir: %s\n", quote_crnl(get_git_dir())); + trace_printf("setup: worktree: %s\n", quote_crnl(get_git_work_tree())); + trace_printf("setup: cwd: %s\n", quote_crnl(cwd)); + trace_printf("setup: prefix: %s\n", quote_crnl(prefix)); +} From 03a2b6effaeaebef86a3c524ecd6c77c70f53273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Nov 2010 22:31:58 +0700 Subject: [PATCH 02/47] Add t1510 and basic rules that run repo setup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 t/t1510-repo-setup.sh diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh new file mode 100755 index 0000000000..d19b9c8e81 --- /dev/null +++ b/t/t1510-repo-setup.sh @@ -0,0 +1,59 @@ +#!/bin/sh + +test_description='Tests of cwd/prefix/worktree/gitdir setup in all cases' + +. ./test-lib.sh + +# +# A few rules for repo setup: +# +# 1. GIT_DIR is relative to user's cwd. --git-dir is equivalent to +# GIT_DIR. +# +# 2. .git file is relative to parent directory. .git file is basically +# symlink in disguise. The directory where .git file points to will +# become new git_dir. +# +# 3. core.worktree is relative to git_dir. +# +# 4. GIT_WORK_TREE is relative to user's cwd. --work-tree is +# equivalent to GIT_WORK_TREE. +# +# 5. GIT_WORK_TREE/core.worktree is only effective if GIT_DIR is set +# Uneffective worktree settings should be warned. +# +# 6. Effective GIT_WORK_TREE overrides core.worktree and core.bare +# +# 7. Effective core.worktree conflicts with core.bare +# +# 8. If GIT_DIR is set but neither worktree nor bare setting is given, +# original cwd becomes worktree. +# +# 9. If .git discovery is done inside a repo, the repo becomes a bare +# repo. .git discovery is performed if GIT_DIR is not set. +# +# 10. If no worktree is available, cwd remains unchanged, prefix is +# NULL. +# +# 11. When user's cwd is outside worktree, cwd remains unchanged, +# prefix is NULL. +# + +test_repo() { + ( + if test -n "$1"; then cd "$1"; fi && + if test -f trace; then rm trace; fi && + GIT_TRACE="`pwd`/trace" git symbolic-ref HEAD >/dev/null && + grep '^setup: ' trace >result && + test_cmp expected result + ) +} + +# Bit 0 = GIT_WORK_TREE +# Bit 1 = GIT_DIR +# Bit 2 = core.worktree +# Bit 3 = .git is a file +# Bit 4 = bare repo +# Case# = encoding of the above 5 bits + +test_done From fc4045ee3eae49c829a466752da1937cfa13e048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Nov 2010 22:31:59 +0700 Subject: [PATCH 03/47] t1510: setup case #0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 53 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index d19b9c8e81..26842274a2 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -41,8 +41,10 @@ test_description='Tests of cwd/prefix/worktree/gitdir setup in all cases' test_repo() { ( - if test -n "$1"; then cd "$1"; fi && - if test -f trace; then rm trace; fi && + cd "$1" && + if test -n "$2"; then GIT_DIR="$2" && export GIT_DIR; fi && + if test -n "$3"; then GIT_WORK_TREE="$3" && export GIT_WORK_TREE; fi && + rm -f trace && GIT_TRACE="`pwd`/trace" git symbolic-ref HEAD >/dev/null && grep '^setup: ' trace >result && test_cmp expected result @@ -56,4 +58,51 @@ test_repo() { # Bit 4 = bare repo # Case# = encoding of the above 5 bits +# +# Case #0 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is not set +# - core.worktree is not set +# - .git is a directory +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# - worktree is .git's parent directory +# - cwd is at worktree root dir +# - prefix is calculated +# - git_dir is set to ".git" +# - cwd can't be outside worktree + +test_expect_success '#0: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 0 0/sub && + cd 0 && git init && cd .. +' + +test_expect_success '#0: at root' ' + cat >0/expected <0/sub/expected < Date: Fri, 26 Nov 2010 22:32:00 +0700 Subject: [PATCH 04/47] t1510: setup case #1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 26842274a2..9324d3ff6c 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -105,4 +105,51 @@ EOF test_repo 0/sub ' +# +# case #1 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is not set +# - core.worktree is not set +# - .git is a directory +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# GIT_WORK_TREE is ignored -> #0 + +test_expect_success '#1: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 1 1/sub 1.wt 1.wt/sub 1/wt 1/wt/sub && + cd 1 && + git init && + GIT_WORK_TREE=non-existent && + export GIT_WORK_TREE && + cd .. +' + +test_expect_failure '#1: at root' ' + cat >1/expected <1/sub/expected < Date: Fri, 26 Nov 2010 22:32:01 +0700 Subject: [PATCH 05/47] t1510: setup case #2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 9324d3ff6c..74750b8d4c 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -152,4 +152,71 @@ EOF test_repo 1/sub ' +# +# case #2 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is set +# - core.worktree is not set +# - .git is a directory +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# - worktree is at original cwd +# - cwd is unchanged +# - prefix is NULL +# - git_dir is set to $GIT_DIR +# - cwd can't be outside worktree + +test_expect_success '#2: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 2 2/sub && + cd 2 && git init && cd .. +' + +test_expect_success '#2: at root' ' + cat >2/expected <2/sub/expected <2/expected <2/sub/expected < Date: Fri, 26 Nov 2010 22:32:02 +0700 Subject: [PATCH 06/47] t1510: setup case #3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 267 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 267 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 74750b8d4c..0e379b5d45 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -219,4 +219,271 @@ EOF test_repo 2/sub ../.git ' +# +# case #3 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is set +# - core.worktree is not set +# - .git is a directory +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# - worktree is set to $GIT_WORK_TREE +# - cwd is at worktree root +# - prefix is calculated +# - git_dir is set to $GIT_DIR +# - cwd can be outside worktree + +test_expect_success '#3: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 3 3/sub 3/sub/sub 3.wt 3.wt/sub 3/wt 3/wt/sub && + cd 3 && git init && cd .. +' + +test_expect_success '#3: GIT_DIR(rel), GIT_WORK_TREE=root at root' ' + cat >3/expected <3/expected <3/expected <3/expected <3/sub/sub/expected <3/sub/sub/expected <3/sub/expected <3/sub/sub/expected <3/expected <3/expected <3/expected <3/expected <3/sub/sub/expected <3/sub/sub/expected <3/sub/sub/expected <3/sub/sub/expected <3/expected <3/expected <3/expected <3/expected <3/sub/sub/expected <3/sub/sub/expected <3/sub/sub/expected <3/sub/sub/expected < Date: Fri, 26 Nov 2010 22:32:03 +0700 Subject: [PATCH 07/47] t1510: setup case #4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 0e379b5d45..1b4070a7e3 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -486,4 +486,50 @@ EOF test_repo 3/sub/sub "$TRASH_DIRECTORY/3/.git" "$TRASH_DIRECTORY" ' +# +# case #4 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is not set +# - core.worktree is set +# - .git is a directory +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# core.worktree is ignored -> #0 + +test_expect_success '#4: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 4 4/sub && + cd 4 && + git init && + git config core.worktree non-existent && + cd .. +' + +test_expect_failure '#4: at root' ' + cat >4/expected <4/sub/expected < Date: Fri, 26 Nov 2010 22:32:04 +0700 Subject: [PATCH 08/47] t1510: setup case #5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 1b4070a7e3..f58d424f00 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -532,4 +532,52 @@ EOF test_repo 4/sub ' +# +# case #5 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is not set +# - core.worktree is set +# - .git is a directory +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# GIT_WORK_TREE/core.worktree are ignored -> #0 + +test_expect_success '#5: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 5 5/sub && + cd 5 && + git init && + git config core.worktree non-existent && + GIT_WORK_TREE=non-existent-too && + export GIT_WORK_TREE && + cd .. +' + +test_expect_failure '#5: at root' ' + cat >5/expected <5/sub/expected < Date: Fri, 26 Nov 2010 22:32:05 +0700 Subject: [PATCH 09/47] t1510: setup case #6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 291 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 291 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index f58d424f00..6e23c98322 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -580,4 +580,295 @@ EOF test_repo 5/sub ' +# +# case #6 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is set +# - core.worktree is set +# - .git is a directory +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# - worktree is at core.worktree +# - cwd is at worktree root +# - prefix is calculated +# - git_dir is at $GIT_DIR +# - cwd can be outside worktree + +test_expect_success '#6: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 6 6/sub 6/sub/sub 6.wt 6.wt/sub 6/wt 6/wt/sub && + cd 6 && git init && cd .. +' + +test_expect_success '#6: GIT_DIR(rel), core.worktree=.. at root' ' + cat >6/expected <6/expected <6/expected <6/expected <6/sub/sub/expected <6/sub/sub/expected <6/sub/expected <6/sub/sub/expected <6/expected <6/expected <6/expected <6/expected <6/sub/sub/expected <6/sub/sub/expected <6/sub/sub/expected <6/sub/sub/expected <6/expected <6/expected <6/expected <6/expected <6/sub/sub/expected <6/sub/sub/expected <6/sub/sub/expected <6/sub/sub/expected < Date: Fri, 26 Nov 2010 22:32:06 +0700 Subject: [PATCH 10/47] t1510: setup case #7 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 266 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 266 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 6e23c98322..8c6c0bca2c 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -871,4 +871,270 @@ EOF test_repo 6/sub/sub "$TRASH_DIRECTORY/6/.git" ' +# +# case #7 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is set +# - core.worktree is set +# - .git is a directory +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# core.worktree is overridden by GIT_WORK_TREE -> #3 + +test_expect_success '#7: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 7 7/sub 7/sub/sub 7.wt 7.wt/sub 7/wt 7/wt/sub && + cd 7 && + git init && + git config core.worktree non-existent && + cd .. +' + +test_expect_success '#7: GIT_DIR(rel), GIT_WORK_TREE=root at root' ' + cat >7/expected <7/expected <7/expected <7/expected <7/sub/sub/expected <7/sub/sub/expected <7/sub/expected <7/sub/sub/expected <7/expected <7/expected <7/expected <7/expected <7/sub/sub/expected <7/sub/sub/expected <7/sub/sub/expected <7/sub/sub/expected <7/expected <7/expected <7/expected <7/expected <7/sub/sub/expected <7/sub/sub/expected <7/sub/sub/expected <7/sub/sub/expected < Date: Fri, 26 Nov 2010 22:32:07 +0700 Subject: [PATCH 11/47] t1510: setup case #8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 8c6c0bca2c..9e9d5f163e 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -1137,4 +1137,51 @@ EOF test_repo 7/sub/sub "$TRASH_DIRECTORY/7/.git" "$TRASH_DIRECTORY" ' +# +# case #8 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is not set +# - core.worktree is not set +# - .git is a file +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# #0 except that git_dir is set by .git file + +test_expect_success '#8: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 8 8/sub && + cd 8 && + git init && + mv .git ../8.git && + echo gitdir: ../8.git >.git && + cd .. +' + +test_expect_success '#8: at root' ' + cat >8/expected <8/sub/expected < Date: Fri, 26 Nov 2010 22:32:08 +0700 Subject: [PATCH 12/47] t1510: setup case #9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 9e9d5f163e..29f0f2a070 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -1184,4 +1184,53 @@ EOF test_repo 8/sub ' +# +# case #9 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is not set +# - core.worktree is not set +# - .git is a file +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# #1 except that git_dir is set by .git file + +test_expect_success '#9: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 9 9/sub 9.wt 9.wt/sub 9/wt 9/wt/sub && + cd 9 && + git init && + mv .git ../9.git && + echo gitdir: ../9.git >.git && + GIT_WORK_TREE=non-existent && + export GIT_WORK_TREE && + cd .. +' + +test_expect_failure '#9: at root' ' + cat >9/expected <9/sub/expected < Date: Fri, 26 Nov 2010 22:32:09 +0700 Subject: [PATCH 13/47] t1510: setup case #10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 29f0f2a070..159ee51ac8 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -1233,4 +1233,71 @@ EOF test_repo 9/sub ' +# +# case #10 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is set +# - core.worktree is not set +# - .git is a file +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# #2 except that git_dir is set by .git file + +test_expect_success '#10: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 10 10/sub && + cd 10 && + git init && + mv .git ../10.git && + echo gitdir: ../10.git >.git && + cd .. +' + +test_expect_failure '#10: at root' ' + cat >10/expected <10/sub/expected <10/expected <10/sub/expected < Date: Fri, 26 Nov 2010 22:32:10 +0700 Subject: [PATCH 14/47] t1510: setup case #11 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 267 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 267 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 159ee51ac8..cf98940ed3 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -1300,4 +1300,271 @@ EOF test_repo 10/sub ../.git ' +# +# case #11 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is set +# - core.worktree is not set +# - .git is a file +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# #3 except that git_dir is set by .git file + +test_expect_success '#11: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 11 11/sub 11/sub/sub 11.wt 11.wt/sub 11/wt 11/wt/sub && + cd 11 && + git init && + mv .git ../11.git && + echo gitdir: ../11.git >.git && + cd .. +' + +test_expect_failure '#11: GIT_DIR(rel), GIT_WORK_TREE=root at root' ' + cat >11/expected <11/expected <11/expected <11/expected <11/sub/sub/expected <11/sub/sub/expected <11/sub/expected <11/sub/sub/expected <11/expected <11/expected <11/expected <11/expected <11/sub/sub/expected <11/sub/sub/expected <11/sub/sub/expected <11/sub/sub/expected <11/expected <11/expected <11/expected <11/expected <11/sub/sub/expected <11/sub/sub/expected <11/sub/sub/expected <11/sub/sub/expected < Date: Fri, 26 Nov 2010 22:32:11 +0700 Subject: [PATCH 15/47] t1510: setup case #12 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index cf98940ed3..2043875a7a 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -1567,4 +1567,53 @@ EOF test_repo 11/sub/sub "$TRASH_DIRECTORY/11/.git" "$TRASH_DIRECTORY" ' +# +# case #12 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is not set +# - core.worktree is set +# - .git is a file +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# #4 except that git_dir is set by .git file + + +test_expect_success '#12: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 12 12/sub 12/sub/sub 12.wt 12.wt/sub 12/wt 12/wt/sub && + cd 12 && + git init && + git config core.worktree non-existent && + mv .git ../12.git && + echo gitdir: ../12.git >.git && + cd .. +' + +test_expect_failure '#12: at root' ' + cat >12/expected <12/sub/expected < Date: Fri, 26 Nov 2010 22:32:12 +0700 Subject: [PATCH 16/47] t1510: setup case #13 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 2043875a7a..b6fdd72374 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -1616,4 +1616,54 @@ EOF test_repo 12/sub ' +# +# case #13 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is not set +# - core.worktree is set +# - .git is a file +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# #5 except that git_dir is set by .git file + +test_expect_success '#13: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 13 13/sub 13/sub/sub 13.wt 13.wt/sub 13/wt 13/wt/sub && + cd 13 && + git init && + git config core.worktree non-existent && + GIT_WORK_TREE=non-existent-too && + export GIT_WORK_TREE && + mv .git ../13.git && + echo gitdir: ../13.git >.git && + cd .. +' + +test_expect_failure '#13: at root' ' + cat >13/expected <13/sub/expected < Date: Fri, 26 Nov 2010 22:32:13 +0700 Subject: [PATCH 17/47] t1510: setup case #14 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 291 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 291 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index b6fdd72374..53816c9c6f 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -1666,4 +1666,295 @@ EOF test_repo 13/sub ' +# +# case #14 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is set +# - core.worktree is set +# - .git is a file +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# #6 except that git_dir is set by .git file + +test_expect_success '#14: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 14 14/sub 14/sub/sub 14.wt 14.wt/sub 14/wt 14/wt/sub && + cd 14 && + git init && + mv .git ../14.git && + echo gitdir: ../14.git >.git && + cd .. +' + +test_expect_failure '#14: GIT_DIR(rel), core.worktree=../14 at root' ' + cat >14/expected <14/expected <14/expected <14/expected <14/sub/sub/expected <14/sub/sub/expected <14/sub/expected <14/sub/sub/expected <14/expected <14/expected <14/expected <14/expected <14/sub/sub/expected <14/sub/sub/expected <14/sub/sub/expected <14/sub/sub/expected <14/expected <14/expected <14/expected <14/expected <14/sub/sub/expected <14/sub/sub/expected <14/sub/sub/expected <14/sub/sub/expected < Date: Fri, 26 Nov 2010 22:32:14 +0700 Subject: [PATCH 18/47] t1510: setup case #15 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 268 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 268 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 53816c9c6f..25f7c015e4 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -1957,4 +1957,272 @@ EOF test_repo 14/sub/sub "$TRASH_DIRECTORY/14/.git" ' +# +# case #15 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is set +# - core.worktree is set +# - .git is a file +# - core.bare is not set, cwd is outside .git +# +# Output: +# +# #7 except that git_dir is set by .git file + +test_expect_success '#15: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 15 15/sub 15/sub/sub 15.wt 15.wt/sub 15/wt 15/wt/sub && + cd 15 && + git init && + git config core.worktree non-existent && + mv .git ../15.git && + echo gitdir: ../15.git >.git && + cd .. +' + +test_expect_failure '#15: GIT_DIR(rel), GIT_WORK_TREE=root at root' ' + cat >15/expected <15/expected <15/expected <15/expected <15/sub/sub/expected <15/sub/sub/expected <15/sub/expected <15/sub/sub/expected <15/expected <15/expected <15/expected <15/expected <15/sub/sub/expected <15/sub/sub/expected <15/sub/sub/expected <15/sub/sub/expected <15/expected <15/expected <15/expected <15/expected <15/sub/sub/expected <15/sub/sub/expected <15/sub/sub/expected <15/sub/sub/expected < Date: Fri, 26 Nov 2010 22:32:15 +0700 Subject: [PATCH 19/47] t1510: setup case #16 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 135 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 25f7c015e4..2596f7ff75 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -2225,4 +2225,139 @@ EOF test_repo 15/sub/sub "$TRASH_DIRECTORY/15/.git" "$TRASH_DIRECTORY" ' +# +# case #16.1 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is not set +# - core.worktree is not set +# - .git is a directory +# - cwd is inside .git +# +# Output: +# +# - no worktree +# - cwd is unchanged +# - prefix is NULL +# - git_dir is set +# - cwd can't be outside worktree + +test_expect_success '#16.1: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 16 16/sub && + cd 16 && + git init && + mkdir .git/wt .git/wt/sub && + cd .. +' + +test_expect_success '#16.1: at .git' ' + cat >16/.git/expected <16/.git/wt/expected <16/.git/wt/sub/expected <16/.git/expected <16/.git/wt/expected <16/.git/wt/sub/expected <16/expected <16/sub/expected < Date: Fri, 26 Nov 2010 22:32:16 +0700 Subject: [PATCH 20/47] t1510: setup case #17 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 129 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 2596f7ff75..8d4ef4cd1f 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -2360,4 +2360,133 @@ EOF test_repo 16/sub ' +# +# case #17.1 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is not set +# - core.worktree is not set +# - .git is a directory +# - cwd is inside .git +# +# Output: +# +# GIT_WORK_TREE is ignored -> #16.1 (with warnings perhaps) + +test_expect_success '#17.1: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 17 17/sub && + cd 17 && + git init && + mkdir .git/wt .git/wt/sub && + GIT_WORK_TREE=non-existent && + export GIT_WORK_TREE && + cd .. +' + +test_expect_failure '#17.1: at .git' ' + cat >17/.git/expected <17/.git/wt/expected <17/.git/wt/sub/expected < #16.2 (with warnings perhaps) + +test_expect_success '#17.2: setup' ' + git config --file="$TRASH_DIRECTORY/17/.git/config" core.bare true +' + +test_expect_failure '#17.2: at .git' ' + cat >17/.git/expected <17/.git/wt/expected <17/.git/wt/sub/expected <17/expected <17/sub/expected < Date: Fri, 26 Nov 2010 22:32:17 +0700 Subject: [PATCH 21/47] t1510: setup case #18 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 8d4ef4cd1f..a828b0355b 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -2489,4 +2489,75 @@ EOF test_repo 17/sub ' +# +# case #18 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is set +# - core.worktree is not set +# - .git is a directory +# - core.bare is set +# +# Output: +# +# - no worktree (rule #8) +# - cwd is unchanged +# - prefix is NULL +# - git_dir is set to $GIT_DIR +# - cwd can't be outside worktree + +test_expect_success '#18: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 18 18/sub && + cd 18 && + git init && + mkdir .git/wt .git/wt/sub && + git config core.bare true && + cd .. +' + +test_expect_success '#18: (rel) at root' ' + cat >18/expected <18/expected <18/sub/expected <18/sub/expected < Date: Fri, 26 Nov 2010 22:32:18 +0700 Subject: [PATCH 22/47] t1510: setup case #19 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 266 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 266 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index a828b0355b..1362902988 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -2560,4 +2560,270 @@ EOF test_repo 18/sub "$TRASH_DIRECTORY/18/.git" ' +# +# case #19 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is set +# - .git is a directory +# - core.worktree is not set +# - core.bare is set +# +# Output: +# +# bare repo is overridden by GIT_WORK_TREE -> #3 + +test_expect_success '#19: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 19 19/sub 19/sub/sub 19.wt 19.wt/sub 19/wt 19/wt/sub && + cd 19 && + git init && + git config core.bare true && + cd .. +' + +test_expect_success '#19: GIT_DIR(rel), GIT_WORK_TREE=root at root' ' + cat >19/expected <19/expected <19/expected <19/expected <19/sub/sub/expected <19/sub/sub/expected <19/sub/expected <19/sub/sub/expected <19/expected <19/expected <19/expected <19/expected <19/sub/sub/expected <19/sub/sub/expected <19/sub/sub/expected <19/sub/sub/expected <19/expected <19/expected <19/expected <19/expected <19/sub/sub/expected <19/sub/sub/expected <19/sub/sub/expected <19/sub/sub/expected < Date: Fri, 26 Nov 2010 22:32:19 +0700 Subject: [PATCH 23/47] t1510: setup case #20 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 128 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 1362902988..8e649e0f09 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -2826,4 +2826,132 @@ EOF test_repo 19/sub/sub "$TRASH_DIRECTORY/19/.git" "$TRASH_DIRECTORY" ' +# +# case #20.1 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is not set +# - core.worktree is set +# - .git is a directory +# - cwd is inside .git +# +# Output: +# +# core.worktree is ignored -> #16.1 + +test_expect_success '#20.1: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 20 20/sub && + cd 20 && + git init && + git config core.worktree non-existent && + mkdir .git/wt .git/wt/sub && + cd .. +' + +test_expect_failure '#20.1: at .git' ' + cat >20/.git/expected <20/.git/wt/expected <20/.git/wt/sub/expected < #16.2 + +test_expect_success '#20.2: setup' ' + git config --file="$TRASH_DIRECTORY/20/.git/config" core.bare true +' + +test_expect_success '#20.2: at .git' ' + cat >20/.git/expected <20/.git/wt/expected <20/.git/wt/sub/expected <20/expected <20/sub/expected < Date: Fri, 26 Nov 2010 22:32:20 +0700 Subject: [PATCH 24/47] t1510: setup case #21 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 130 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 8e649e0f09..1d01a6c8ee 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -2954,4 +2954,134 @@ EOF test_repo 20/sub ' +# +# case #21.1 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is not set +# - core.worktree is set +# - .git is a directory +# - cwd is inside .git +# +# Output: +# +# GIT_WORK_TREE/core.worktree are ignored -> #20.1 + +test_expect_success '#21.1: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 21 21/sub && + cd 21 && + git init && + git config core.worktree non-existent && + GIT_WORK_TREE=non-existent-too && + export GIT_WORK_TREE && + mkdir .git/wt .git/wt/sub && + cd .. +' + +test_expect_failure '#21.1: at .git' ' + cat >21/.git/expected <21/.git/wt/expected <21/.git/wt/sub/expected < #20.2 + +test_expect_success '#21.2: setup' ' + git config --file="$TRASH_DIRECTORY/21/.git/config" core.bare true +' + +test_expect_failure '#21.2: at .git' ' + cat >21/.git/expected <21/.git/wt/expected <21/.git/wt/sub/expected <21/expected <21/sub/expected < Date: Fri, 26 Nov 2010 22:32:21 +0700 Subject: [PATCH 25/47] t1510: setup case #22 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 337 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 337 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 1d01a6c8ee..1720fe25e1 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -3084,4 +3084,341 @@ EOF test_repo 21/sub ' +# +# case #22.1 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is set +# - core.worktree is set +# - .git is a directory +# - cwd is inside .git +# +# Output: +# +# bare attribute is ignored +# +# - worktree is at core.worktree +# - cwd is at worktree root +# - prefix is calculated +# - git_dir is at $GIT_DIR +# - cwd can be outside worktree + +test_expect_success '#22.1: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 22 && + cd 22 && + git init && + mkdir .git/sub .git/wt .git/wt/sub && + cd .. +' + +test_expect_success '#22.1: GIT_DIR(rel), core.worktree=. at .git' ' + cat >22/.git/expected <22/.git/expected <22/.git/expected <22/.git/expected <22/.git/sub/expected <22/.git/sub/expected <22/.git/sub/expected <22/.git/sub/expected <22/.git/expected <22/.git/expected <22/.git/expected <22/.git/expected <22/.git/sub/expected <22/.git/sub/expected <22/.git/sub/expected <22/.git/sub/expected <22/.git/expected <22/.git/expected <22/.git/expected <22/.git/expected <22/.git/sub/expected <22/.git/sub/expected <22/.git/sub/expected <22/.git/sub/expected <result && + grep "core.bare and core.worktree do not make sense" result + ) +' + +test_expect_failure '#22.2: at root' ' + ( + cd 22 && + GIT_DIR=.git && + export GIT_DIR && + test_must_fail git symbolic-ref HEAD 2>result && + grep "core.bare and core.worktree do not make sense" result + ) +' + test_done From 5362cbfcf3b8939edc4bd0058d6b8b08d4621234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Nov 2010 22:32:22 +0700 Subject: [PATCH 26/47] t1510: setup case #23 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 267 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 267 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 1720fe25e1..ad27d1278b 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -3421,4 +3421,271 @@ test_expect_failure '#22.2: at root' ' ) ' +# +# case #23 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is set +# - core.worktree is set +# - .git is a directory +# - core.bare is set +# +# Output: +# +# core.worktree is overridden by GIT_WORK_TREE -> #19 + +test_expect_success '#23: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 23 23/sub 23/sub/sub 23.wt 23.wt/sub 23/wt 23/wt/sub && + cd 23 && + git init && + git config core.bare true && + git config core.worktree non-existent && + cd .. +' + +test_expect_success '#23: GIT_DIR(rel), GIT_WORK_TREE=root at root' ' + cat >23/expected <23/expected <23/expected <23/expected <23/sub/sub/expected <23/sub/sub/expected <23/sub/expected <23/sub/sub/expected <23/expected <23/expected <23/expected <23/expected <23/sub/sub/expected <23/sub/sub/expected <23/sub/sub/expected <23/sub/sub/expected <23/expected <23/expected <23/expected <23/expected <23/sub/sub/expected <23/sub/sub/expected <23/sub/sub/expected <23/sub/sub/expected < Date: Fri, 26 Nov 2010 22:32:23 +0700 Subject: [PATCH 27/47] t1510: setup case #24 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index ad27d1278b..79959f6dc5 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -3688,4 +3688,52 @@ EOF test_repo 23/sub/sub "$TRASH_DIRECTORY/23/.git" "$TRASH_DIRECTORY" ' +# +# case #24 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is not set +# - core.worktree is not set +# - .git is a file +# - core.bare is set +# +# Output: +# +# #16.2 except git_dir is set according to .git file + +test_expect_success '#24: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 24 24/sub && + cd 24 && + git init && + git config core.bare true && + mv .git ../24.git && + echo gitdir: ../24.git >.git && + cd .. +' + +test_expect_success '#24: at root' ' + cat >24/expected <24/sub/expected < Date: Fri, 26 Nov 2010 22:32:24 +0700 Subject: [PATCH 28/47] t1510: setup case #25 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 79959f6dc5..5be85eded3 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -3736,4 +3736,54 @@ EOF test_repo 24/sub ' +# +# case #25 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is not set +# - core.worktree is not set +# - .git is a file +# - core.bare is set +# +# Output: +# +# #17.2 except git_dir is set according to .git file + +test_expect_success '#25: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 25 25/sub && + cd 25 && + git init && + git config core.bare true && + GIT_WORK_TREE=non-existent && + export GIT_WORK_TREE && + mv .git ../25.git && + echo gitdir: ../25.git >.git && + cd .. +' + +test_expect_failure '#25: at root' ' + cat >25/expected <25/sub/expected < Date: Fri, 26 Nov 2010 22:32:25 +0700 Subject: [PATCH 29/47] t1510: setup case #26 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 5be85eded3..626126f543 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -3786,4 +3786,72 @@ EOF test_repo 25/sub ' +# +# case #26 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is set +# - core.worktree is not set +# - .git is a file +# - core.bare is set +# +# Output: +# +# #18 except git_dir is set according to .git file + +test_expect_success '#26: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 26 26/sub && + cd 26 && + git init && + git config core.bare true && + mv .git ../26.git && + echo gitdir: ../26.git >.git && + cd .. +' + +test_expect_failure '#26: (rel) at root' ' + cat >26/expected <26/expected <26/sub/expected <26/sub/expected < Date: Fri, 26 Nov 2010 22:32:26 +0700 Subject: [PATCH 30/47] t1510: setup case #27 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 268 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 268 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 626126f543..f053a7d957 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -3854,4 +3854,272 @@ EOF test_repo 26/sub "$TRASH_DIRECTORY/26/.git" ' +# +# case #27 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is set +# - .git is a file +# - core.worktree is not set +# - core.bare is set +# +# Output: +# +# #19 except git_dir is set according to .git file + +test_expect_success '#27: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 27 27/sub 27/sub/sub 27.wt 27.wt/sub 27/wt 27/wt/sub && + cd 27 && + git init && + git config core.bare true && + mv .git ../27.git && + echo gitdir: ../27.git >.git && + cd .. +' + +test_expect_failure '#27: GIT_DIR(rel), GIT_WORK_TREE=root at root' ' + cat >27/expected <27/expected <27/expected <27/expected <27/sub/sub/expected <27/sub/sub/expected <27/sub/expected <27/sub/sub/expected <27/expected <27/expected <27/expected <27/expected <27/sub/sub/expected <27/sub/sub/expected <27/sub/sub/expected <27/sub/sub/expected <27/expected <27/expected <27/expected <27/expected <27/sub/sub/expected <27/sub/sub/expected <27/sub/sub/expected <27/sub/sub/expected < Date: Fri, 26 Nov 2010 22:32:27 +0700 Subject: [PATCH 31/47] t1510: setup case #28 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index f053a7d957..d67733c896 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -4122,4 +4122,53 @@ EOF test_repo 27/sub/sub "$TRASH_DIRECTORY/27/.git" "$TRASH_DIRECTORY" ' +# +# case #28 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is not set +# - core.worktree is set +# - .git is a file +# - core.bare is set +# +# Output: +# +# core.worktree is ignored -> #24 + +test_expect_success '#28: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 28 28/sub && + cd 28 && + git init && + git config core.bare true && + git config core.worktree non-existent && + mv .git ../28.git && + echo gitdir: ../28.git >.git && + cd .. +' + +test_expect_success '#28: at root' ' + cat >28/expected <28/sub/expected < Date: Fri, 26 Nov 2010 22:32:28 +0700 Subject: [PATCH 32/47] t1510: setup case #29 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index d67733c896..f1b5ea530e 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -4171,4 +4171,54 @@ EOF test_repo 28/sub ' +# +# case #29 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is not set +# - core.worktree is set +# - .git is a file +# - core.bare is set +# +# Output: +# +# GIT_WORK_TREE/core.worktree are ignored -> #28 + +test_expect_success '#29: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 29 29/sub && + cd 29 && + git init && + git config core.bare true && + GIT_WORK_TREE=non-existent && + export GIT_WORK_TREE && + mv .git ../29.git && + echo gitdir: ../29.git >.git && + cd .. +' + +test_expect_failure '#29: at root' ' + cat >29/expected <29/sub/expected < Date: Fri, 26 Nov 2010 22:32:29 +0700 Subject: [PATCH 33/47] t1510: setup case #30 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index f1b5ea530e..6f298a2b1a 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -4221,4 +4221,43 @@ EOF test_repo 29/sub ' +# +# case #30 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is not set +# - GIT_DIR is set +# - core.worktree is set +# - .git is a file +# - core.bare is set +# +# Output: +# +# core.worktree and core.bare conflict, won't fly. + +test_expect_success '#30: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 30 && + cd 30 && + git init && + git config core.bare true && + git config core.worktree non-existent && + mv .git ../30.git && + echo gitdir: ../30.git >.git && + cd .. +' + +test_expect_failure '#30: at root' ' + ( + cd 30 && + GIT_DIR=.git && + export GIT_DIR && + test_must_fail git symbolic-ref HEAD 2>result && + grep "core.bare and core.worktree do not make sense" result + ) +' + test_done From e0d769d1fda4f66a1c0b99f3f52a25d4f4006162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Nov 2010 22:32:30 +0700 Subject: [PATCH 34/47] t1510: setup case #31 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1510-repo-setup.sh | 269 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 269 insertions(+) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 6f298a2b1a..fbab9c7e68 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -4260,4 +4260,273 @@ test_expect_failure '#30: at root' ' ) ' +# +# case #31 +# +############################################################ +# +# Input: +# +# - GIT_WORK_TREE is set +# - GIT_DIR is set +# - core.worktree is set +# - .git is a file +# - core.bare is set +# +# Output: +# +# #23 except git_dir is set according to .git file + +test_expect_success '#31: setup' ' + unset GIT_DIR GIT_WORK_TREE && + mkdir 31 31/sub 31/sub/sub 31.wt 31.wt/sub 31/wt 31/wt/sub && + cd 31 && + git init && + git config core.bare true && + git config core.worktree non-existent && + mv .git ../31.git && + echo gitdir: ../31.git >.git && + cd .. +' + +test_expect_failure '#31: GIT_DIR(rel), GIT_WORK_TREE=root at root' ' + cat >31/expected <31/expected <31/expected <31/expected <31/sub/sub/expected <31/sub/sub/expected <31/sub/expected <31/sub/sub/expected <31/expected <31/expected <31/expected <31/expected <31/sub/sub/expected <31/sub/sub/expected <31/sub/sub/expected <31/sub/sub/expected <31/expected <31/expected <31/expected <31/expected <31/sub/sub/expected <31/sub/sub/expected <31/sub/sub/expected <31/sub/sub/expected < Date: Fri, 26 Nov 2010 22:32:31 +0700 Subject: [PATCH 35/47] git-rev-parse.txt: clarify --git-dir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current behavior is often to print an absolute path rather than a ../../etc string, but callers must be ready to accept a relative path, too. The most common output is ".git" (from the toplevel of an ordinary work tree). Signed-off-by: Jonathan Nieder Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- Documentation/git-rev-parse.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt index 4a27643c1e..ff23cb0219 100644 --- a/Documentation/git-rev-parse.txt +++ b/Documentation/git-rev-parse.txt @@ -136,7 +136,12 @@ appending `/{asterisk}`. directory (typically a sequence of "../", or an empty string). --git-dir:: - Show `$GIT_DIR` if defined else show the path to the .git directory. + Show `$GIT_DIR` if defined. Otherwise show the path to + the .git directory, relative to the current directory. ++ +If `$GIT_DIR` is not defined and the current directory +is not detected to lie in a git repository or work tree +print a message to stderr and exit with nonzero status. --is-inside-git-dir:: When the current working directory is below the repository From dbdf5854b21852676de6a8baf052147217ce809b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Nov 2010 22:32:33 +0700 Subject: [PATCH 36/47] Add git_config_early() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This version of git_config() will be used during repository setup. As a repository is being set up, $GIT_DIR is not nailed down yet, git_pathdup() should not be used to get $GIT_DIR/config. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- cache.h | 1 + config.c | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cache.h b/cache.h index d1bee5d518..123dd4bb93 100644 --- a/cache.h +++ b/cache.h @@ -986,6 +986,7 @@ extern int git_config_parse_parameter(const char *text); extern int git_config_parse_environment(void); extern int git_config_from_parameters(config_fn_t fn, void *data); extern int git_config(config_fn_t fn, void *); +extern int git_config_early(config_fn_t fn, void *, const char *repo_config); extern int git_parse_ulong(const char *, unsigned long *); extern int git_config_int(const char *, const char *); extern unsigned long git_config_ulong(const char *, const char *); diff --git a/config.c b/config.c index 4b0a82040e..c431f41c5a 100644 --- a/config.c +++ b/config.c @@ -835,10 +835,9 @@ int git_config_from_parameters(config_fn_t fn, void *data) return 0; } -int git_config(config_fn_t fn, void *data) +int git_config_early(config_fn_t fn, void *data, const char *repo_config) { int ret = 0, found = 0; - char *repo_config = NULL; const char *home = NULL; /* Setting $GIT_CONFIG makes git read _only_ the given config file. */ @@ -860,12 +859,10 @@ int git_config(config_fn_t fn, void *data) free(user_config); } - repo_config = git_pathdup("config"); - if (!access(repo_config, R_OK)) { + if (repo_config && !access(repo_config, R_OK)) { ret += git_config_from_file(fn, repo_config, data); found += 1; } - free(repo_config); ret += git_config_from_parameters(fn, data); if (config_parameters) @@ -876,6 +873,18 @@ int git_config(config_fn_t fn, void *data) return ret; } +int git_config(config_fn_t fn, void *data) +{ + char *repo_config = NULL; + int ret; + + repo_config = git_pathdup("config"); + ret = git_config_early(fn, data, repo_config); + if (repo_config) + free(repo_config); + return ret; +} + /* * Find all the stuff for git_config_set() below. */ From 337e51cedfdf1f98e1c151b72f06c7ca8bcea0fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Nov 2010 22:32:34 +0700 Subject: [PATCH 37/47] Use git_config_early() instead of git_config() during repo setup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When git_config() is called, either git_dir has already been set (by $GIT_DIR env or set_git_dir()), or it will default git_dir to ".git". git_config_early() gives setup functions more freedom because it does not require git_dir. Give it a config path, it will happily examine it. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- setup.c | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/setup.c b/setup.c index a3b76de2bb..49a1a2525a 100644 --- a/setup.c +++ b/setup.c @@ -243,9 +243,21 @@ void setup_work_tree(void) initialized = 1; } -static int check_repository_format_gently(int *nongit_ok) +static int check_repository_format_gently(const char *gitdir, int *nongit_ok) { - git_config(check_repository_format_version, NULL); + char repo_config[PATH_MAX+1]; + + /* + * git_config() can't be used here because it calls git_pathdup() + * to get $GIT_CONFIG/config. That call will make setup_git_env() + * set git_dir to ".git". + * + * We are in gitdir setup, no git dir has been found useable yet. + * Use a gentler version of git_config() to check if this repo + * is a good one. + */ + snprintf(repo_config, PATH_MAX, "%s/config", gitdir); + git_config_early(check_repository_format_version, NULL, repo_config); if (GIT_REPO_VERSION < repository_format_version) { if (!nongit_ok) die ("Expected git repo version <= %d, found %d", @@ -331,11 +343,11 @@ static const char *setup_explicit_git_dir(const char *gitdirenv, if (!work_tree_env) { retval = set_work_tree(gitdirenv); /* config may override worktree */ - if (check_repository_format_gently(nongit_ok)) + if (check_repository_format_gently(gitdirenv, nongit_ok)) return NULL; return retval; } - if (check_repository_format_gently(nongit_ok)) + if (check_repository_format_gently(gitdirenv, nongit_ok)) return NULL; retval = get_relative_cwd(buffer, sizeof(buffer) - 1, get_git_work_tree()); @@ -357,11 +369,17 @@ static int cwd_contains_git_dir(const char **gitfile_dirp) die("Repository setup failed"); return 1; } - return is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT); + if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) { + *gitfile_dirp = DEFAULT_GIT_DIR_ENVIRONMENT; + return 1; + } + return 0; } static const char *setup_discovered_git_dir(const char *work_tree_env, - int offset, int len, char *cwd, int *nongit_ok) + const char *gitdir, + int offset, int len, + char *cwd, int *nongit_ok) { int root_len; @@ -370,7 +388,7 @@ static const char *setup_discovered_git_dir(const char *work_tree_env, inside_work_tree = 1; root_len = offset_1st_component(cwd); git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len); - if (check_repository_format_gently(nongit_ok)) + if (check_repository_format_gently(gitdir, nongit_ok)) return NULL; if (offset == len) return NULL; @@ -396,9 +414,12 @@ static const char *setup_bare_git_dir(const char *work_tree_env, root_len = offset_1st_component(cwd); cwd[offset > root_len ? offset : root_len] = '\0'; set_git_dir(cwd); - } else + check_repository_format_gently(cwd, nongit_ok); + } + else { set_git_dir("."); - check_repository_format_gently(nongit_ok); + check_repository_format_gently(".", nongit_ok); + } return NULL; } @@ -478,8 +499,10 @@ static const char *setup_git_directory_gently_1(int *nongit_ok) current_device = get_device_or_die(".", NULL); for (;;) { if (cwd_contains_git_dir(&gitfile_dir)) - return setup_discovered_git_dir(work_tree_env, offset, - len, cwd, nongit_ok); + return setup_discovered_git_dir(work_tree_env, + gitfile_dir, + offset, len, + cwd, nongit_ok); if (is_git_directory(".")) return setup_bare_git_dir(work_tree_env, offset, len, cwd, nongit_ok); @@ -590,7 +613,7 @@ int check_repository_format_version(const char *var, const char *value, void *cb int check_repository_format(void) { - return check_repository_format_gently(NULL); + return check_repository_format_gently(get_git_dir(), NULL); } /* From e6aea2dba27798f5d1eca32435e407541caca400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Nov 2010 22:32:35 +0700 Subject: [PATCH 38/47] setup: limit get_git_work_tree()'s to explicit setup case only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit get_git_work_tree() takes input as core.worktree, core.bare, GIT_WORK_TREE and decides correct worktree setting. Unfortunately it does not do its job well. core.worktree and GIT_WORK_TREE should only be taken into account, if GIT_DIR is set (which is handled by setup_explicit_git_dir). For other setup cases, only core.bare matters. Add a temporary variable setup_explicit to adjust get_git_work_tree() behavior as such. This variable will be gone once setup_* rework is done. Also remove is_bare_repository_cfg check in set_git_work_tree() to ease the rework. We are going to check for core.bare and core.worktree early before setting worktree. For example, if core.bare is true, no need to set worktree. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- builtin/init-db.c | 1 + cache.h | 1 + environment.c | 10 +++++-- setup.c | 10 +++++-- t/t1510-repo-setup.sh | 66 +++++++++++++++++++++---------------------- 5 files changed, 51 insertions(+), 37 deletions(-) diff --git a/builtin/init-db.c b/builtin/init-db.c index 9d4886c716..ea064784db 100644 --- a/builtin/init-db.c +++ b/builtin/init-db.c @@ -496,6 +496,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix) if (is_bare_repository_cfg < 0) is_bare_repository_cfg = guess_repository_type(git_dir); + startup_info->setup_explicit = 1; if (!is_bare_repository_cfg) { if (git_dir) { const char *git_dir_parent = strrchr(git_dir, '/'); diff --git a/cache.h b/cache.h index 123dd4bb93..b2cdda7142 100644 --- a/cache.h +++ b/cache.h @@ -1119,6 +1119,7 @@ const char *split_cmdline_strerror(int cmdline_errno); /* git.c */ struct startup_info { int have_repository; + int setup_explicit; }; extern struct startup_info *startup_info; diff --git a/environment.c b/environment.c index de5581fe51..d811049a7d 100644 --- a/environment.c +++ b/environment.c @@ -137,8 +137,6 @@ static int git_work_tree_initialized; */ void set_git_work_tree(const char *new_work_tree) { - if (is_bare_repository_cfg >= 0) - die("cannot set work tree after initialization"); git_work_tree_initialized = 1; free(work_tree); work_tree = xstrdup(make_absolute_path(new_work_tree)); @@ -147,6 +145,14 @@ void set_git_work_tree(const char *new_work_tree) const char *get_git_work_tree(void) { + if (startup_info && !startup_info->setup_explicit) { + if (is_bare_repository_cfg == 1) + return NULL; + if (work_tree) + is_bare_repository_cfg = 0; + return work_tree; + } + if (!git_work_tree_initialized) { work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT); /* core.bare = true overrides implicit and config work tree */ diff --git a/setup.c b/setup.c index 49a1a2525a..c7d71986c0 100644 --- a/setup.c +++ b/setup.c @@ -331,6 +331,8 @@ static const char *setup_explicit_git_dir(const char *gitdirenv, static char buffer[1024 + 1]; const char *retval; + if (startup_info) + startup_info->setup_explicit = 1; if (PATH_MAX - 40 < strlen(gitdirenv)) die("'$%s' too big", GIT_DIR_ENVIRONMENT); if (!is_git_directory(gitdirenv)) { @@ -382,12 +384,15 @@ static const char *setup_discovered_git_dir(const char *work_tree_env, char *cwd, int *nongit_ok) { int root_len; + char *work_tree; inside_git_dir = 0; if (!work_tree_env) inside_work_tree = 1; root_len = offset_1st_component(cwd); - git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len); + work_tree = xstrndup(cwd, offset > root_len ? offset : root_len); + set_git_work_tree(work_tree); + free(work_tree); if (check_repository_format_gently(gitdir, nongit_ok)) return NULL; if (offset == len) @@ -627,7 +632,8 @@ 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) { + if ((!startup_info || startup_info->setup_explicit) && + inside_work_tree < 0) { static char buffer[PATH_MAX + 1]; char *rel; if (retval && chdir(retval)) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index fbab9c7e68..a5f10a5878 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -132,7 +132,7 @@ test_expect_success '#1: setup' ' cd .. ' -test_expect_failure '#1: at root' ' +test_expect_success '#1: at root' ' cat >1/expected <1/sub/expected <4/expected <4/sub/expected <5/expected <5/sub/expected <9/expected <9/sub/expected <12/expected <12/sub/expected <13/expected <13/sub/expected <17/.git/expected <17/.git/wt/expected <17/.git/wt/sub/expected <17/.git/expected <17/.git/wt/expected <17/.git/wt/sub/expected <17/expected <20/.git/expected <20/.git/wt/expected <20/.git/wt/sub/expected <21/.git/expected <21/.git/wt/expected <21/.git/wt/sub/expected <21/.git/expected <21/.git/wt/expected <21/.git/wt/sub/expected <21/expected <24/expected <24/sub/expected <28/expected <28/sub/expected < Date: Fri, 26 Nov 2010 22:32:36 +0700 Subject: [PATCH 39/47] setup: clean up setup_bare_git_dir() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit work_tree_env argument is removed because this function does not need it. GIT_WORK_TREE is only effective inside setup_explicit_git_dir. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- setup.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/setup.c b/setup.c index c7d71986c0..a6cc044730 100644 --- a/setup.c +++ b/setup.c @@ -405,26 +405,25 @@ static const char *setup_discovered_git_dir(const char *work_tree_env, return cwd + offset; } -static const char *setup_bare_git_dir(const char *work_tree_env, - int offset, int len, char *cwd, int *nongit_ok) +/* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */ +static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok) { int root_len; + if (check_repository_format_gently(".", nongit_ok)) + return NULL; + inside_git_dir = 1; - if (!work_tree_env) - inside_work_tree = 0; + inside_work_tree = 0; if (offset != len) { if (chdir(cwd)) die_errno("Cannot come back to cwd"); root_len = offset_1st_component(cwd); cwd[offset > root_len ? offset : root_len] = '\0'; set_git_dir(cwd); - check_repository_format_gently(cwd, nongit_ok); } - else { + else set_git_dir("."); - check_repository_format_gently(".", nongit_ok); - } return NULL; } @@ -509,8 +508,8 @@ static const char *setup_git_directory_gently_1(int *nongit_ok) offset, len, cwd, nongit_ok); if (is_git_directory(".")) - return setup_bare_git_dir(work_tree_env, offset, - len, cwd, nongit_ok); + return setup_bare_git_dir(cwd, offset, len, nongit_ok); + while (--offset > ceil_offset && cwd[offset] != '/'); if (offset <= ceil_offset) return setup_nongit(cwd, nongit_ok); From 101662c22518de4abfbd5b2fc38b3ec1fa638494 Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Fri, 26 Nov 2010 22:32:37 +0700 Subject: [PATCH 40/47] t1020-subdirectory: test alias expansion in a subdirectory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a test for alias expansion in a subdirectory of the worktree. Signed-off-by: Michael J Gruber Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t1020-subdirectory.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/t/t1020-subdirectory.sh b/t/t1020-subdirectory.sh index a3ac33801a..1fd187c5eb 100755 --- a/t/t1020-subdirectory.sh +++ b/t/t1020-subdirectory.sh @@ -110,6 +110,14 @@ test_expect_success 'read-tree' ' ) ' +test_expect_success 'alias expansion' ' + ( + git config alias.ss status && + cd dir && + git status && + git ss + ) +' test_expect_success 'no file/rev ambiguity check inside .git' ' git commit -a -m 1 && ( From 9951d3b37ec7298384640b11d07a990ef031fe83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Nov 2010 22:32:38 +0700 Subject: [PATCH 41/47] setup: clean up setup_discovered_git_dir() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If core.bare is true, discard the discovered worktree, move back to original cwd. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- setup.c | 74 +++++++++++++++++++++---------------------- t/t1510-repo-setup.sh | 24 +++++++------- 2 files changed, 49 insertions(+), 49 deletions(-) diff --git a/setup.c b/setup.c index a6cc044730..3ee9c2eec8 100644 --- a/setup.c +++ b/setup.c @@ -362,39 +362,27 @@ static const char *setup_explicit_git_dir(const char *gitdirenv, return retval; } -static int cwd_contains_git_dir(const char **gitfile_dirp) +static const char *setup_discovered_git_dir(const char *gitdir, + char *cwd, int offset, int len, + int *nongit_ok) { - const char *gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT); - *gitfile_dirp = gitfile_dir; - if (gitfile_dir) { - if (set_git_dir(gitfile_dir)) - die("Repository setup failed"); - return 1; - } - if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) { - *gitfile_dirp = DEFAULT_GIT_DIR_ENVIRONMENT; - return 1; - } - return 0; -} - -static const char *setup_discovered_git_dir(const char *work_tree_env, - const char *gitdir, - int offset, int len, - char *cwd, int *nongit_ok) -{ - int root_len; - char *work_tree; - - inside_git_dir = 0; - if (!work_tree_env) - inside_work_tree = 1; - root_len = offset_1st_component(cwd); - work_tree = xstrndup(cwd, offset > root_len ? offset : root_len); - set_git_work_tree(work_tree); - free(work_tree); if (check_repository_format_gently(gitdir, nongit_ok)) return NULL; + + /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */ + if (is_bare_repository_cfg > 0) { + set_git_dir(offset == len ? gitdir : make_absolute_path(gitdir)); + if (chdir(cwd)) + die_errno("Could not come back to cwd"); + return NULL; + } + + /* #0, #1, #5, #8, #9, #12, #13 */ + set_git_work_tree("."); + if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) + set_git_dir(gitdir); + inside_git_dir = 0; + inside_work_tree = 1; if (offset == len) return NULL; @@ -456,8 +444,8 @@ static const char *setup_git_directory_gently_1(int *nongit_ok) const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); static char cwd[PATH_MAX+1]; - const char *gitdirenv; - const char *gitfile_dir; + const char *gitdirenv, *ret; + char *gitfile; int len, offset, ceil_offset; dev_t current_device = 0; int one_filesystem = 1; @@ -502,11 +490,23 @@ static const char *setup_git_directory_gently_1(int *nongit_ok) if (one_filesystem) current_device = get_device_or_die(".", NULL); for (;;) { - if (cwd_contains_git_dir(&gitfile_dir)) - return setup_discovered_git_dir(work_tree_env, - gitfile_dir, - offset, len, - cwd, nongit_ok); + gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT); + if (gitfile) + gitdirenv = gitfile = xstrdup(gitfile); + else { + if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) + gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; + } + + if (gitdirenv) { + ret = setup_discovered_git_dir(gitdirenv, + cwd, offset, len, + nongit_ok); + free(gitfile); + return ret; + } + free(gitfile); + if (is_git_directory(".")) return setup_bare_git_dir(cwd, offset, len, nongit_ok); diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index a5f10a5878..057f959442 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -2350,7 +2350,7 @@ EOF test_repo 16 ' -test_expect_failure '#16.2: in subdir' ' +test_expect_success '#16.2: in subdir' ' cat >16/sub/expected <17/sub/expected <20/sub/expected <21/sub/expected <24/expected <24/sub/expected <25/expected <25/sub/expected <28/expected <28/sub/expected <29/expected <29/sub/expected < Date: Fri, 26 Nov 2010 22:32:39 +0700 Subject: [PATCH 42/47] setup: rework setup_explicit_git_dir() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function is the most complex one among the three setup_* functions because all GIT_DIR, GIT_WORK_TREE, core.worktree and core.bare are involved. Because core.worktree is only effective inside setup_explicit_git_dir() and the extra code in setup_git_directory() is to handle that. The extra code can now be retired. Also note that setup_explicit assignment is removed, worktree setting is no longer decided by get_git_work_tree(). get_git_work_tree() will be simplified in the next commit. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- setup.c | 138 +++++++++++--------- t/t1510-repo-setup.sh | 286 +++++++++++++++++++++--------------------- 2 files changed, 221 insertions(+), 203 deletions(-) diff --git a/setup.c b/setup.c index 3ee9c2eec8..2e2865cc41 100644 --- a/setup.c +++ b/setup.c @@ -208,24 +208,6 @@ int is_inside_work_tree(void) return inside_work_tree; } -/* - * set_work_tree() is only ever called if you set GIT_DIR explicitly. - * 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 *work_tree, *git_dir; @@ -326,40 +308,92 @@ const char *read_gitfile_gently(const char *path) } static const char *setup_explicit_git_dir(const char *gitdirenv, - const char *work_tree_env, int *nongit_ok) + char *cwd, int len, + int *nongit_ok) { - static char buffer[1024 + 1]; - const char *retval; + const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); + const char *worktree; + char *gitfile; - if (startup_info) - startup_info->setup_explicit = 1; if (PATH_MAX - 40 < strlen(gitdirenv)) die("'$%s' too big", GIT_DIR_ENVIRONMENT); + + gitfile = (char*)read_gitfile_gently(gitdirenv); + if (gitfile) { + gitfile = xstrdup(gitfile); + gitdirenv = gitfile; + } + if (!is_git_directory(gitdirenv)) { if (nongit_ok) { *nongit_ok = 1; + free(gitfile); return NULL; } die("Not a git repository: '%s'", gitdirenv); } - if (!work_tree_env) { - retval = set_work_tree(gitdirenv); - /* config may override worktree */ - if (check_repository_format_gently(gitdirenv, nongit_ok)) - return NULL; - return retval; + + if (check_repository_format_gently(gitdirenv, nongit_ok)) { + free(gitfile); + return NULL; } - if (check_repository_format_gently(gitdirenv, nongit_ok)) + + /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */ + if (work_tree_env) + set_git_work_tree(work_tree_env); + else if (is_bare_repository_cfg > 0) { + if (git_work_tree_cfg) /* #22.2, #30 */ + die("core.bare and core.worktree do not make sense"); + + /* #18, #26 */ + set_git_dir(gitdirenv); + free(gitfile); return NULL; - retval = get_relative_cwd(buffer, sizeof(buffer) - 1, - get_git_work_tree()); - if (!retval || !*retval) + } + else if (git_work_tree_cfg) { /* #6, #14 */ + if (is_absolute_path(git_work_tree_cfg)) + set_git_work_tree(git_work_tree_cfg); + else { + char core_worktree[PATH_MAX]; + if (chdir(gitdirenv)) + die_errno("Could not chdir to '%s'", gitdirenv); + if (chdir(git_work_tree_cfg)) + die_errno("Could not chdir to '%s'", git_work_tree_cfg); + if (!getcwd(core_worktree, PATH_MAX)) + die_errno("Could not get directory '%s'", git_work_tree_cfg); + if (chdir(cwd)) + die_errno("Could not come back to cwd"); + set_git_work_tree(core_worktree); + } + } + else /* #2, #10 */ + set_git_work_tree("."); + + /* set_git_work_tree() must have been called by now */ + worktree = get_git_work_tree(); + + /* both get_git_work_tree() and cwd are already normalized */ + if (!strcmp(cwd, worktree)) { /* cwd == worktree */ + set_git_dir(gitdirenv); + free(gitfile); return NULL; - set_git_dir(make_absolute_path(gitdirenv)); - if (chdir(work_tree_env) < 0) - die_errno ("Could not chdir to '%s'", work_tree_env); - strcat(buffer, "/"); - return retval; + } + + if (!prefixcmp(cwd, worktree) && + cwd[strlen(worktree)] == '/') { /* cwd inside worktree */ + set_git_dir(make_absolute_path(gitdirenv)); + if (chdir(worktree)) + die_errno("Could not chdir to '%s'", worktree); + cwd[len++] = '/'; + cwd[len] = '\0'; + free(gitfile); + return cwd + strlen(worktree) + 1; + } + + /* cwd outside worktree */ + set_git_dir(gitdirenv); + free(gitfile); + return NULL; } static const char *setup_discovered_git_dir(const char *gitdir, @@ -441,7 +475,6 @@ static dev_t get_device_or_die(const char *path, const char *prefix) */ static const char *setup_git_directory_gently_1(int *nongit_ok) { - const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); static char cwd[PATH_MAX+1]; const char *gitdirenv, *ret; @@ -458,6 +491,10 @@ static const char *setup_git_directory_gently_1(int *nongit_ok) if (nongit_ok) *nongit_ok = 0; + if (!getcwd(cwd, sizeof(cwd)-1)) + die_errno("Unable to read current working directory"); + offset = len = strlen(cwd); + /* * If GIT_DIR is set explicitly, we're not going * to do any discovery, but we still do repository @@ -465,10 +502,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok) */ gitdirenv = getenv(GIT_DIR_ENVIRONMENT); if (gitdirenv) - return setup_explicit_git_dir(gitdirenv, work_tree_env, nongit_ok); - - if (!getcwd(cwd, sizeof(cwd)-1)) - die_errno("Unable to read current working directory"); + return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok); ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs); if (ceil_offset < 0 && has_dos_drive_prefix(cwd)) @@ -485,7 +519,6 @@ static const char *setup_git_directory_gently_1(int *nongit_ok) * - ../../.git/ * etc. */ - offset = len = strlen(cwd); one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); if (one_filesystem) current_device = get_device_or_die(".", NULL); @@ -628,20 +661,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 ((!startup_info || startup_info->setup_explicit) && - inside_work_tree < 0) { - static char buffer[PATH_MAX + 1]; - char *rel; - if (retval && chdir(retval)) - die_errno ("Could not jump back into original cwd"); - rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree()); - if (rel && *rel && chdir(get_git_work_tree())) - die_errno ("Could not jump to working directory"); - return rel && *rel ? strcat(rel, "/") : NULL; - } - - return retval; + return setup_git_directory_gently(NULL); } diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index 057f959442..500ffafc22 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -651,7 +651,7 @@ EOF test_repo 6 "$TRASH_DIRECTORY/6/.git" ' -test_expect_failure '#6: GIT_DIR(rel), core.worktree=.. in subdir' ' +test_expect_success '#6: GIT_DIR(rel), core.worktree=.. in subdir' ' cat >6/sub/sub/expected <6/sub/sub/expected <6/expected <6/expected <6/sub/sub/expected <6/sub/sub/expected <10/expected <10/sub/expected <10/expected <10/sub/expected <11/expected <11/expected <11/expected <11/expected <11/sub/sub/expected <11/sub/sub/expected <11/sub/expected <11/sub/sub/expected <11/expected <11/expected <11/expected <11/expected <11/sub/sub/expected <11/sub/sub/expected <11/sub/sub/expected <11/sub/sub/expected <11/expected <11/expected <11/expected <11/expected <11/sub/sub/expected <11/sub/sub/expected <11/sub/sub/expected <11/sub/sub/expected <14/expected <14/expected <14/expected <14/expected <14/sub/sub/expected <14/sub/sub/expected <14/sub/expected <14/sub/sub/expected <14/expected <14/expected <14/expected <14/expected <14/sub/sub/expected <14/sub/sub/expected <14/sub/sub/expected <14/sub/sub/expected <14/expected <14/expected <14/expected <14/expected <14/sub/sub/expected <14/sub/sub/expected <14/sub/sub/expected <14/sub/sub/expected <15/expected <15/expected <15/expected <15/expected <15/sub/sub/expected <15/sub/sub/expected <15/sub/expected <15/sub/sub/expected <15/expected <15/expected <15/expected <15/expected <15/sub/sub/expected <15/sub/sub/expected <15/sub/sub/expected <15/sub/sub/expected <15/expected <15/expected <15/expected <15/expected <15/sub/sub/expected <15/sub/sub/expected <15/sub/sub/expected <15/sub/sub/expected <22/.git/sub/expected <22/.git/sub/expected <22/.git/expected <22/.git/expected <22/.git/sub/expected <22/.git/sub/expected <26/expected <26/expected <26/sub/expected <26/sub/expected <27/expected <27/expected <27/expected <27/expected <27/sub/sub/expected <27/sub/sub/expected <27/sub/expected <27/sub/sub/expected <27/expected <27/expected <27/expected <27/expected <27/sub/sub/expected <27/sub/sub/expected <27/sub/sub/expected <27/sub/sub/expected <27/expected <27/expected <27/expected <27/expected <27/sub/sub/expected <27/sub/sub/expected <27/sub/sub/expected <27/sub/sub/expected <31/expected <31/expected <31/expected <31/expected <31/sub/sub/expected <31/sub/sub/expected <31/sub/expected <31/sub/sub/expected <31/expected <31/expected <31/expected <31/expected <31/sub/sub/expected <31/sub/sub/expected <31/sub/sub/expected <31/sub/sub/expected <31/expected <31/expected <31/expected <31/expected <31/sub/sub/expected <31/sub/sub/expected <31/sub/sub/expected <31/sub/sub/expected < Date: Fri, 26 Nov 2010 22:32:40 +0700 Subject: [PATCH 43/47] Remove all logic from get_git_work_tree() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This logic is now only used by cmd_init_db(). setup_* functions do not rely on it any more. Move all the logic to cmd_init_db() and turn get_git_work_tree() into a simple function. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- builtin/init-db.c | 14 +++++++++++--- cache.h | 1 - environment.c | 32 ++++++++------------------------ 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/builtin/init-db.c b/builtin/init-db.c index ea064784db..e3af9eaa87 100644 --- a/builtin/init-db.c +++ b/builtin/init-db.c @@ -414,6 +414,7 @@ static const char *const init_db_usage[] = { int cmd_init_db(int argc, const char **argv, const char *prefix) { const char *git_dir; + const char *work_tree; const char *template_dir = NULL; unsigned int flags = 0; const struct option init_db_options[] = { @@ -480,8 +481,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix) * without --bare. Catch the error early. */ git_dir = getenv(GIT_DIR_ENVIRONMENT); - if ((!git_dir || is_bare_repository_cfg == 1) - && getenv(GIT_WORK_TREE_ENVIRONMENT)) + work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT); + if ((!git_dir || is_bare_repository_cfg == 1) && work_tree) die("%s (or --work-tree=) not allowed without " "specifying %s (or --git-dir=)", GIT_WORK_TREE_ENVIRONMENT, @@ -496,7 +497,6 @@ int cmd_init_db(int argc, const char **argv, const char *prefix) if (is_bare_repository_cfg < 0) is_bare_repository_cfg = guess_repository_type(git_dir); - startup_info->setup_explicit = 1; if (!is_bare_repository_cfg) { if (git_dir) { const char *git_dir_parent = strrchr(git_dir, '/'); @@ -511,10 +511,18 @@ int cmd_init_db(int argc, const char **argv, const char *prefix) if (!getcwd(git_work_tree_cfg, PATH_MAX)) die_errno ("Cannot access current working directory"); } + if (work_tree) + set_git_work_tree(make_absolute_path(work_tree)); + else + set_git_work_tree(git_work_tree_cfg); if (access(get_git_work_tree(), X_OK)) die_errno ("Cannot access work tree '%s'", get_git_work_tree()); } + else { + if (work_tree) + set_git_work_tree(make_absolute_path(work_tree)); + } set_git_dir(make_absolute_path(git_dir)); diff --git a/cache.h b/cache.h index b2cdda7142..123dd4bb93 100644 --- a/cache.h +++ b/cache.h @@ -1119,7 +1119,6 @@ const char *split_cmdline_strerror(int cmdline_errno); /* git.c */ struct startup_info { int have_repository; - int setup_explicit; }; extern struct startup_info *startup_info; diff --git a/environment.c b/environment.c index d811049a7d..149c132304 100644 --- a/environment.c +++ b/environment.c @@ -137,36 +137,20 @@ static int git_work_tree_initialized; */ void set_git_work_tree(const char *new_work_tree) { + if (git_work_tree_initialized) { + new_work_tree = make_absolute_path(new_work_tree); + if (strcmp(new_work_tree, work_tree)) + die("internal error: work tree has already been set\n" + "Current worktree: %s\nNew worktree: %s", + work_tree, new_work_tree); + return; + } git_work_tree_initialized = 1; - free(work_tree); work_tree = xstrdup(make_absolute_path(new_work_tree)); - is_bare_repository_cfg = 0; } const char *get_git_work_tree(void) { - if (startup_info && !startup_info->setup_explicit) { - if (is_bare_repository_cfg == 1) - return NULL; - if (work_tree) - is_bare_repository_cfg = 0; - return work_tree; - } - - if (!git_work_tree_initialized) { - work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT); - /* core.bare = true overrides implicit and config work tree */ - if (!work_tree && is_bare_repository_cfg < 1) { - work_tree = git_work_tree_cfg; - /* make_absolute_path also normalizes the path */ - if (work_tree && !is_absolute_path(work_tree)) - work_tree = xstrdup(make_absolute_path(git_path("%s", work_tree))); - } else if (work_tree) - work_tree = xstrdup(make_absolute_path(work_tree)); - git_work_tree_initialized = 1; - if (work_tree) - is_bare_repository_cfg = 0; - } return work_tree; } From 4ad8332ef79c53fd7c214fcbe24a29ff243a531e Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Fri, 26 Nov 2010 22:32:41 +0700 Subject: [PATCH 44/47] t0001: test git init when run via an alias MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add some tests to document the correct behavior of (possibly aliased) init when run within and outside a git directory. If I set up a simple git alias “quietinit = init --quiet”, usually it will work just like ‘git init --quiet’. There are some differences, unfortunately, since in the process of checking for aliases, git has to look for a .git/config file. If ‘git quietinit’ is run from a subdirectory of an existing git repository, that repository’s configuration will affect the configuration of the new repository. In particular, the new repository can inherit bogus values for core.bare and core.worktree. Signed-off-by: Jonathan Nieder Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t0001-init.sh | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/t/t0001-init.sh b/t/t0001-init.sh index 7fe8883ae0..28c1858c2b 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -33,6 +33,62 @@ test_expect_success 'plain' ' check_config plain/.git false unset ' +test_expect_success 'plain nested in bare' ' + ( + unset GIT_DIR GIT_WORK_TREE && + git init --bare bare-ancestor.git && + cd bare-ancestor.git && + mkdir plain-nested && + cd plain-nested && + git init + ) && + check_config bare-ancestor.git/plain-nested/.git false unset +' + +test_expect_success 'plain through aliased command, outside any git repo' ' + ( + unset GIT_DIR GIT_WORK_TREE GIT_CONFIG_NOGLOBAL && + HOME=$(pwd)/alias-config && + export HOME && + mkdir alias-config && + echo "[alias] aliasedinit = init" >alias-config/.gitconfig && + + GIT_CEILING_DIRECTORIES=$(pwd) && + export GIT_CEILING_DIRECTORIES && + + mkdir plain-aliased && + cd plain-aliased && + git aliasedinit + ) && + check_config plain-aliased/.git false unset +' + +test_expect_failure 'plain nested through aliased command' ' + ( + unset GIT_DIR GIT_WORK_TREE && + git init plain-ancestor-aliased && + cd plain-ancestor-aliased && + echo "[alias] aliasedinit = init" >>.git/config && + mkdir plain-nested && + cd plain-nested && + git aliasedinit + ) && + check_config plain-ancestor-aliased/plain-nested/.git false unset +' + +test_expect_failure 'plain nested in bare through aliased command' ' + ( + unset GIT_DIR GIT_WORK_TREE && + git init --bare bare-ancestor-aliased.git && + cd bare-ancestor-aliased.git && + echo "[alias] aliasedinit = init" >>config && + mkdir plain-nested && + cd plain-nested && + git aliasedinit + ) && + check_config bare-ancestor-aliased.git/plain-nested/.git false unset +' + test_expect_success 'plain with GIT_WORK_TREE' ' if ( unset GIT_DIR From 98acf75985d15e8c53f1dfa1721b1e1128101737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Nov 2010 22:32:42 +0700 Subject: [PATCH 45/47] Revert "Documentation: always respect core.worktree if set" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit f5e025a9d5b35e24768475f6890b836ce0d5ed15. The commit reflected what the code did. But the code did that because it had bugs. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- Documentation/config.txt | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/Documentation/config.txt b/Documentation/config.txt index 538ebb5e2e..d85563d764 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -317,24 +317,17 @@ false), while all other repositories are assumed to be bare (bare = true). core.worktree:: - Set the path to the root of the work tree. + Set the path to the working tree. The value will not be + used in combination with repositories found automatically in + a .git directory (i.e. $GIT_DIR is not set). This can be overridden by the GIT_WORK_TREE environment variable and the '--work-tree' command line option. It can be - an absolute path or a relative path to the .git directory, - either specified by --git-dir or GIT_DIR, or automatically - discovered. - If --git-dir or GIT_DIR are specified but none of + an absolute path or relative path to the directory specified by + --git-dir or GIT_DIR. + Note: If --git-dir or GIT_DIR are specified but none of --work-tree, GIT_WORK_TREE and core.worktree is specified, - the current working directory is regarded as the root of the - work tree. -+ -Note that this variable is honored even when set in a configuration -file in a ".git" subdirectory of a directory, and its value differs -from the latter directory (e.g. "/path/to/.git/config" has -core.worktree set to "/different/path"), which is most likely a -misconfiguration. Running git commands in "/path/to" directory will -still use "/different/path" as the root of the work tree and can cause -great confusion to the users. + the current working directory is regarded as the top directory + of your working tree. core.logAllRefUpdates:: Enable the reflog. Updates to a ref is logged to the file From e7d1efbe9dc5c03e65a573d6f8c4d8804118eb0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Nov 2010 22:32:43 +0700 Subject: [PATCH 46/47] git.txt: correct where --work-tree path is relative to MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- Documentation/git.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Documentation/git.txt b/Documentation/git.txt index 0c897df6a7..7433601f35 100644 --- a/Documentation/git.txt +++ b/Documentation/git.txt @@ -285,7 +285,7 @@ help ...`. This can also be controlled by setting the GIT_WORK_TREE environment variable and the core.worktree configuration variable. It can be an absolute path or relative path to - the directory specified by --git-dir or GIT_DIR. + current working directory. Note: If --git-dir or GIT_DIR are specified but none of --work-tree, GIT_WORK_TREE and core.worktree is specified, the current working directory is regarded as the top directory From 0ed748134748579a13cf27e1e8b3e55371bcd9dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Mon, 27 Dec 2010 08:26:04 +0700 Subject: [PATCH 47/47] setup_work_tree: adjust relative $GIT_WORK_TREE after moving cwd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When setup_work_tree() is called, it moves cwd to $GIT_WORK_TREE and makes internal copy of $GIT_WORK_TREE absolute. The environt variable, if set by user, remains unchanged. If the variable is relative, it is no longer correct because its base dir has changed. Instead of making $GIT_WORK_TREE absolute too, we just say "." and let subsequent git processes handle it. Reported-by: Michel Briand Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- .gitignore | 1 + Makefile | 1 + setup.c | 8 ++++++++ t/t1501-worktree.sh | 7 +++++++ test-subprocess.c | 21 +++++++++++++++++++++ 5 files changed, 38 insertions(+) create mode 100644 test-subprocess.c diff --git a/.gitignore b/.gitignore index 20560b810b..1dcc082a88 100644 --- a/.gitignore +++ b/.gitignore @@ -175,6 +175,7 @@ /test-sha1 /test-sigchain /test-string-pool +/test-subprocess /test-svn-fe /test-treap /common-cmds.h diff --git a/Makefile b/Makefile index 1f1ce04edf..f96d990d5a 100644 --- a/Makefile +++ b/Makefile @@ -425,6 +425,7 @@ TEST_PROGRAMS_NEED_X += test-run-command TEST_PROGRAMS_NEED_X += test-sha1 TEST_PROGRAMS_NEED_X += test-sigchain TEST_PROGRAMS_NEED_X += test-string-pool +TEST_PROGRAMS_NEED_X += test-subprocess TEST_PROGRAMS_NEED_X += test-svn-fe TEST_PROGRAMS_NEED_X += test-treap TEST_PROGRAMS_NEED_X += test-index-version diff --git a/setup.c b/setup.c index 2e2865cc41..be550af42a 100644 --- a/setup.c +++ b/setup.c @@ -221,6 +221,14 @@ void setup_work_tree(void) git_dir = make_absolute_path(git_dir); if (!work_tree || chdir(work_tree)) die("This operation must be run in a work tree"); + + /* + * Make sure subsequent git processes find correct worktree + * if $GIT_WORK_TREE is set relative + */ + if (getenv(GIT_WORK_TREE_ENVIRONMENT)) + setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1); + set_git_dir(make_relative_path(git_dir, work_tree)); initialized = 1; } diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh index 2c8f01f668..f072a8ed48 100755 --- a/t/t1501-worktree.sh +++ b/t/t1501-worktree.sh @@ -340,4 +340,11 @@ test_expect_success 'make_relative_path handles double slashes in GIT_DIR' ' git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file ' +test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' ' + GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work \ + test-subprocess --setup-work-tree rev-parse --show-toplevel >actual && + echo "$TRASH_DIRECTORY/repo.git/work" >expected && + test_cmp expected actual +' + test_done diff --git a/test-subprocess.c b/test-subprocess.c new file mode 100644 index 0000000000..667d3e5079 --- /dev/null +++ b/test-subprocess.c @@ -0,0 +1,21 @@ +#include "cache.h" +#include "run-command.h" + +int main(int argc, char **argv) +{ + const char *prefix; + struct child_process cp; + int nogit = 0; + + prefix = setup_git_directory_gently(&nogit); + if (nogit) + die("No git repo found"); + if (!strcmp(argv[1], "--setup-work-tree")) { + setup_work_tree(); + argv++; + } + memset(&cp, 0, sizeof(cp)); + cp.git_cmd = 1; + cp.argv = (const char **)argv+1; + return run_command(&cp); +}