* [PATCH] Support working directory located at root
@ 2010-02-08 14:53 Nguyễn Thái Ngọc Duy
2010-02-08 17:43 ` Junio C Hamano
2010-02-08 21:29 ` Johannes Sixt
0 siblings, 2 replies; 7+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2010-02-08 14:53 UTC (permalink / raw)
To: git, João Carlos Mendes Luís, Junio C Hamano
Cc: Nguyễn Thái Ngọc Duy
Git should work regardless where the working directory is located,
even at root. This patch fixes two places where it assumes working
directory always have parent directory.
In setup_git_directory_gently(), when Git goes up to root and finds
.git there, it happily sets worktree to "".
In prefix_path(), loosen the outside repo check a little bit. Usually
when a path XXX is inside worktree /foo, it must be either "/foo", or
"/foo/...". When worktree is simply "/", we can safely ignore the
check: we have a slash at the beginning already.
Not related to worktree, but also set gitdir correctly if a bare repo
is placed (insanely?) at root.
Thanks João Carlos Mendes Luís for pointing out this problem.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
Tell me if this patch is rejected, I'll send another one that makes
setup_git_* die() if worktree/gitdir is to be set empty. I don't think we
expect gitdir or worktree to be empty anywhere.
setup.c | 6 ++++--
1 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/setup.c b/setup.c
index b38cbee..0fcd2fd 100644
--- a/setup.c
+++ b/setup.c
@@ -25,7 +25,7 @@ const char *prefix_path(const char *prefix, int len, const char *path)
len = strlen(work_tree);
total = strlen(sanitized) + 1;
if (strncmp(sanitized, work_tree, len) ||
- (sanitized[len] != '\0' && sanitized[len] != '/')) {
+ (len > 1 && sanitized[len] != '\0' && sanitized[len] != '/')) {
error_out:
die("'%s' is outside repository", orig);
}
@@ -403,7 +403,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
if (!work_tree_env)
inside_work_tree = 0;
if (offset != len) {
- cwd[offset] = '\0';
+ cwd[offset ? offset : 1] = '\0';
set_git_dir(cwd);
} else
set_git_dir(".");
@@ -427,6 +427,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
inside_git_dir = 0;
if (!work_tree_env)
inside_work_tree = 1;
+ if (offset == 0) /* reached root, set worktree to '/' */
+ offset = 1;
git_work_tree_cfg = xstrndup(cwd, offset);
if (check_repository_format_gently(nongit_ok))
return NULL;
--
1.7.0.rc0.54.gd33ef
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] Support working directory located at root
2010-02-08 14:53 Nguyễn Thái Ngọc Duy
@ 2010-02-08 17:43 ` Junio C Hamano
2010-02-09 2:18 ` Nguyen Thai Ngoc Duy
2010-02-08 21:29 ` Johannes Sixt
1 sibling, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2010-02-08 17:43 UTC (permalink / raw)
To: Nguyễn Thái Ngọc Duy
Cc: git, João Carlos Mendes Luís
Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:
> Git should work regardless where the working directory is located,
> even at root. This patch fixes two places where it assumes working
> directory always have parent directory.
>
> In setup_git_directory_gently(), when Git goes up to root and finds
> .git there, it happily sets worktree to "".
If you mean "instead set it to "/" and things will work much better."
I agree with the reasoning (not suggesting to reword---just trying to
make sure I understood what you meant).
> In prefix_path(), loosen the outside repo check a little bit. Usually
> when a path XXX is inside worktree /foo, it must be either "/foo", or
> "/foo/...". When worktree is simply "/", we can safely ignore the
> check: we have a slash at the beginning already.
The logic for the "are we inside?" check above sounds correct. When
work_tree is at root, have "/" in it, and len inside the "if orig is
absolute" block is 1, so memmove() strips out the leading '/' and makes
the result relative to the root level. Am I reading the code right?
> Not related to worktree, but also set gitdir correctly if a bare repo
> is placed (insanely?) at root.
Yuck, but looks correct.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Support working directory located at root
2010-02-08 14:53 Nguyễn Thái Ngọc Duy
2010-02-08 17:43 ` Junio C Hamano
@ 2010-02-08 21:29 ` Johannes Sixt
1 sibling, 0 replies; 7+ messages in thread
From: Johannes Sixt @ 2010-02-08 21:29 UTC (permalink / raw)
To: Nguyễn Thái Ngọc Duy
Cc: git, João Carlos Mendes Luís, Junio C Hamano
On Montag, 8. Februar 2010, Nguyễn Thái Ngọc Duy wrote:
> @@ -25,7 +25,7 @@ const char *prefix_path(const char *prefix, int len,
> const char *path) len = strlen(work_tree);
> total = strlen(sanitized) + 1;
> if (strncmp(sanitized, work_tree, len) ||
> - (sanitized[len] != '\0' && sanitized[len] != '/')) {
> + (len > 1 && sanitized[len] != '\0' && sanitized[len] != '/')) {
> error_out:
> die("'%s' is outside repository", orig);
> }
> @@ -403,7 +403,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
> if (!work_tree_env)
> inside_work_tree = 0;
> if (offset != len) {
> - cwd[offset] = '\0';
> + cwd[offset ? offset : 1] = '\0';
> set_git_dir(cwd);
> } else
> set_git_dir(".");
> @@ -427,6 +427,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
> inside_git_dir = 0;
> if (!work_tree_env)
> inside_work_tree = 1;
> + if (offset == 0) /* reached root, set worktree to '/' */
> + offset = 1;
> git_work_tree_cfg = xstrndup(cwd, offset);
> if (check_repository_format_gently(nongit_ok))
> return NULL;
Does not work:
etc@master:1028> ~/Src/git/git/git add resolv.conf
fatal: pathspec 'tc/resolv.conf' did not match any files
I wonder how this works on Windows where we do not want to strip the slash
from C:/ either.
-- Hannes
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Support working directory located at root
2010-02-08 17:43 ` Junio C Hamano
@ 2010-02-09 2:18 ` Nguyen Thai Ngoc Duy
2010-02-09 2:49 ` Nguyen Thai Ngoc Duy
0 siblings, 1 reply; 7+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2010-02-09 2:18 UTC (permalink / raw)
To: Junio C Hamano, Johannes Sixt; +Cc: git, João Carlos Mendes Luís
2010/2/9 Junio C Hamano <gitster@pobox.com>:
> Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:
>
>> Git should work regardless where the working directory is located,
>> even at root. This patch fixes two places where it assumes working
>> directory always have parent directory.
>>
>> In setup_git_directory_gently(), when Git goes up to root and finds
>> .git there, it happily sets worktree to "".
>
> If you mean "instead set it to "/" and things will work much better."
> I agree with the reasoning (not suggesting to reword---just trying to
> make sure I understood what you meant).
Yes.
>
>> In prefix_path(), loosen the outside repo check a little bit. Usually
>> when a path XXX is inside worktree /foo, it must be either "/foo", or
>> "/foo/...". When worktree is simply "/", we can safely ignore the
>> check: we have a slash at the beginning already.
>
> The logic for the "are we inside?" check above sounds correct. When
> work_tree is at root, have "/" in it, and len inside the "if orig is
> absolute" block is 1, so memmove() strips out the leading '/' and makes
> the result relative to the root level. Am I reading the code right?
You are. But I suspect my change in this code is not enough and caused
the problem (on msys?) for Hannes. If the worktree somehow is '//' and
sanitized is '/etc/resolv.conf', then we could end up eating two
chars, leading to "'tc/resolv.conf' not match" error.
Hannes, can you put a "printf("%s\n", work_tree);" in prefix_path() to
see if it's the case?
--
Duy
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Support working directory located at root
2010-02-09 2:18 ` Nguyen Thai Ngoc Duy
@ 2010-02-09 2:49 ` Nguyen Thai Ngoc Duy
0 siblings, 0 replies; 7+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2010-02-09 2:49 UTC (permalink / raw)
To: Johannes Sixt; +Cc: git, João Carlos Mendes Luís, Junio C Hamano
2010/2/9 Nguyen Thai Ngoc Duy <pclouds@gmail.com>:
> But I suspect my change in this code is not enough and caused
> the problem (on msys?) for Hannes. If the worktree somehow is '//' and
> sanitized is '/etc/resolv.conf', then we could end up eating two
> chars, leading to "'tc/resolv.conf' not match" error.
>
> Hannes, can you put a "printf("%s\n", work_tree);" in prefix_path() to
> see if it's the case?
Silly me. You used relative path, so that code path was untouched.
--
Duy
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Support working directory located at root
@ 2010-02-09 7:16 Johannes Sixt
2010-02-09 12:27 ` Nguyen Thai Ngoc Duy
0 siblings, 1 reply; 7+ messages in thread
From: Johannes Sixt @ 2010-02-09 7:16 UTC (permalink / raw)
To: Nguyen Thai Ngoc Duy
Cc: Junio C Hamano, git, João Carlos Mendes Luís
Nguyen Thai Ngoc Duy schrieb:
> But I suspect my change in this code is not enough and caused
> the problem (on msys?) for Hannes.
My test was on Linux, btw, roughly:
cd /
git init
cd etc
git add resolv.conf
-- Hannes
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Support working directory located at root
2010-02-09 7:16 [PATCH] Support working directory located at root Johannes Sixt
@ 2010-02-09 12:27 ` Nguyen Thai Ngoc Duy
0 siblings, 0 replies; 7+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2010-02-09 12:27 UTC (permalink / raw)
To: Johannes Sixt; +Cc: Junio C Hamano, git, João Carlos Mendes Luís
On 2/9/10, Johannes Sixt <j6t@kdbg.org> wrote:
> Nguyen Thai Ngoc Duy schrieb:
>
> > But I suspect my change in this code is not enough and caused
> > the problem (on msys?) for Hannes.
>
>
> My test was on Linux, btw, roughly:
>
> cd /
> git init
> cd etc
> git add resolv.conf
OK. Just a wild guess since you're mingw port guy :-)
I changed offset near the end of setup_git_*_gently, and in turn
screwed up prefix calculation. I probably forgot to unset
GIT_WORK_TREE while testing. Will work on a test script first before
getting back to the patch. I think I can solve the DOS drive case too.
Just checking, UNC path is not affected, right?
--
Duy
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2010-02-09 12:27 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-02-09 7:16 [PATCH] Support working directory located at root Johannes Sixt
2010-02-09 12:27 ` Nguyen Thai Ngoc Duy
-- strict thread matches above, loose matches on Subject: below --
2010-02-08 14:53 Nguyễn Thái Ngọc Duy
2010-02-08 17:43 ` Junio C Hamano
2010-02-09 2:18 ` Nguyen Thai Ngoc Duy
2010-02-09 2:49 ` Nguyen Thai Ngoc Duy
2010-02-08 21:29 ` Johannes Sixt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).