threads / patch / 11027

patchMake Git accept absolute path names for files within the work tree

Subject: [PATCH] Make Git accept absolute path names for files within the work tree

## tl;dr

10 messages between Nov 26, 2007 and Nov 29, 2007. Diffs are folded; open one to read it.

replies: 9people: 3as markdown or json

Robin Rosenberg· Nov 26, 2007, 23:18 UTC · lore

This patch makes it possible to drag files and directories from a graphical browser and drop them onto a shell and feed them to common git operations without editing away the path to the root of the work tree.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 setup.c               |   16 ++++++++++++++
 t/t3904-abspatharg.sh |   53 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 0 deletions(-)
 create mode 100755 t/t3904-abspatharg.sh
Was it this simple?
Show changes to 2 files +69 −0

setup.c, t/t3904-abspatharg.sh

diff --git a/setup.c b/setup.c
index 43cd3f9..9b3a9ff 100644
--- a/setup.c
+++ b/setup.c
@@ -6,6 +6,22 @@ static int inside_work_tree = -1;
 
 const char *prefix_path(const char *prefix, int len, const char *path)
 {
+	if (is_absolute_path(path)) {
+		const char *work_tree = get_git_work_tree();
+		int n = strlen(work_tree);
+		if (!strncmp(path, work_tree, n) && (path[n] == '/' || !path[n])) {
+			if (path[n])
+				path += 1;
+			path += n;
+			if (prefix && !strncmp(path, prefix, len - 1)) {
+			    if (path[len - 1] == '/')
+				    path += len;
+			    else
+				    if (!path[len - 1])
+					    path += len - 1;
+			}
+		}
+	}
 	const char *orig = path;
 	for (;;) {
 		char c;
diff --git a/t/t3904-abspatharg.sh b/t/t3904-abspatharg.sh
new file mode 100755
index 0000000..aa47602
--- /dev/null
+++ b/t/t3904-abspatharg.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+#
+# Copyright (C) 2007 Robin Rosenberg
+#
+
+test_description='Test absolute filename arguments to various git
+commands.  Absolute arguments pointing to a location within the git
+work tree should behave the same as relative arguments.  '
+
+. ./test-lib.sh
+
+test_expect_success 'add files using absolute path names' '
+echo a >afile &&
+echo b >bfile &&
+git-add afile &&
+git-add $(pwd)/bfile &&
+test "afile bfile" = "$(echo $(git ls-files))"
+mkdir x &&
+cd x &&
+echo c >cfile &&
+echo d >dfile &&
+git-add cfile &&
+git-add $(pwd) &&
+cd .. &&
+test "afile bfile x/cfile x/dfile" = "$(echo $(git ls-files))" &&
+test "$(echo $(git ls-files x))" = "$(echo $(git ls-files $(pwd)/x))"
+'
+
+test_expect_success 'commit using absolute path names' '
+git commit -m "foo" &&
+echo aa >>bfile &&
+git commit -m "bb" $(pwd)/bfile
+'
+
+test_expect_success 'log using absolute path names' '
+git log afile >f1.txt &&
+git log $(pwd)/afile >f2.txt &&
+diff f1.txt f2.txt
+'
+
+test_expect_success 'blame using absolute path names' '
+git blame afile >f1.txt &&
+git blame $(pwd)/afile >f2.txt &&
+diff f1.txt f2.txt
+'
+
+test_expect_success 'diff using absolute path names' '
+git diff HEAD^ -- $(pwd)/afile >f1.txt &&
+git diff HEAD^ -- afile >f2.txt &&
+diff f1.txt f2.txt
+'
+
+test_done
-- 
1.5.3.5.1.gb2df9
Junio C Hamano· Nov 27, 2007, 00:24 UTC · re: Robin Rosenberg · lore

Re: [PATCH] Make Git accept absolute path names for files within the work tree

Robin Rosenberg <robin.rosenberg@dewire.com> writes:
Show 27 quoted lines
> Was it this simple?
>
> diff --git a/setup.c b/setup.c
> index 43cd3f9..9b3a9ff 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -6,6 +6,22 @@ static int inside_work_tree = -1;
>  
>  const char *prefix_path(const char *prefix, int len, const char *path)
>  {
> +	if (is_absolute_path(path)) {
> +		const char *work_tree = get_git_work_tree();
> +		int n = strlen(work_tree);
> +		if (!strncmp(path, work_tree, n) && (path[n] == '/' || !path[n])) {
> +			if (path[n])
> +				path += 1;
> +			path += n;
> +			if (prefix && !strncmp(path, prefix, len - 1)) {
> +			    if (path[len - 1] == '/')
> +				    path += len;
> +			    else
> +				    if (!path[len - 1])
> +					    path += len - 1;
> +			}
> +		}
> +	}
>  	const char *orig = path;
Decl after statement.

I do not think there is fundamental reason to object to this change, as long as the prefixing is done to the path that is trying to name a path in the working tree.

Also some codepath that does not require any work tree may want to call prefix_path(). I do not know what would happen in such a case.

Although I didn't look at all the callers, I think the caller from config.c is not talking about a path in the work tree, and not all users of config.c need to have work-tree.

Robin Rosenberg· Nov 27, 2007, 23:20 UTC · re: Junio C Hamano · lore

Re: [PATCH] Make Git accept absolute path names for files within the work tree

tisdag 27 november 2007 skrev Junio C Hamano:
> Robin Rosenberg <robin.rosenberg@dewire.com> writes:
[...]
> >  	const char *orig = path;
> 
> Decl after statement.
Stupid C..
Show 7 quoted lines
> 
> I do not think there is fundamental reason to object to this change, as
> long as the prefixing is done to the path that is trying to name a path
> in the working tree.
> 
> Also some codepath that does not require any work tree may want to call
> prefix_path().  I do not know what would happen in such a case.
ok, I'll add a test for whether we have a work tree.
> Although I didn't look at all the callers, I think the caller from
> config.c is not talking about a path in the work tree, and not all users
> of config.c need to have work-tree.

Oh dear. That wasn't on master when I looked. That one seems to be the only "other" use and it is wrapped inside an if (!absolute_path()) tests so my magic does not conflict there.

-- robin
Robin Rosenberg· Nov 27, 2007, 23:24 UTC · re: Junio C Hamano · lore

This patch makes it possible to drag files and directories from a graphical browser and drop them onto a shell and feed them to common git operations without editing away the path to the root of the work tree.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 setup.c               |   19 +++++++++++++++++
 t/t3904-abspatharg.sh |   55 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+), 0 deletions(-)
 create mode 100755 t/t3904-abspatharg.sh
Show changes to 2 files +74 −0

setup.c, t/t3904-abspatharg.sh

diff --git a/setup.c b/setup.c
index f512ea0..ffc30bf 100644
--- a/setup.c
+++ b/setup.c
@@ -7,6 +7,25 @@ static int inside_work_tree = -1;
 const char *prefix_path(const char *prefix, int len, const char *path)
 {
 	const char *orig = path;
+	const char *work_tree = get_git_work_tree();
+	if (is_absolute_path(path) && work_tree) {
+		int n = strlen(work_tree);
+		if (!strncmp(path, work_tree, n) && (path[n] == '/' || !path[n])) {
+			if (path[n])
+				path += n + 1;
+			else
+				path += n;
+
+			if (prefix && !strncmp(path, prefix, len - 1)) {
+				if (path[len - 1] == '/')
+					path += len;
+				else
+					if (!path[len - 1])
+						path += len - 1;
+			}
+		}
+	}
+
 	for (;;) {
 		char c;
 		if (*path != '.')
diff --git a/t/t3904-abspatharg.sh b/t/t3904-abspatharg.sh
new file mode 100755
index 0000000..cd4a52e
--- /dev/null
+++ b/t/t3904-abspatharg.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+#
+# Copyright (C) 2007 Robin Rosenberg
+#
+
+test_description='Test absolute filename arguments to various git
+commands.  Absolute arguments pointing to a location within the git
+work tree should behave the same as relative arguments.  '
+
+. ./test-lib.sh
+
+test_expect_success 'add files using absolute path names' '
+echo a >afile &&
+echo b >bfile &&
+git-add afile &&
+git-add "$(pwd)/bfile" &&
+test "afile bfile" = "$(echo $(git ls-files))"
+mkdir x &&
+cd x &&
+echo c >cfile &&
+echo d >dfile &&
+git-add cfile &&
+git-add "$(pwd)" &&
+cd .. &&
+test "afile bfile x/cfile x/dfile" = "$(echo $(git ls-files))" &&
+git ls-files x >f1 &&
+git ls-files "$(pwd)/x" >f2 &&
+diff f1 f2
+'
+
+test_expect_success 'commit using absolute path names' '
+git commit -m "foo" &&
+echo aa >>bfile &&
+git commit -m "bb" "$(pwd)/bfile"
+'
+
+test_expect_success 'log using absolute path names' '
+git log afile >f1.txt &&
+git log "$(pwd)/afile" >f2.txt &&
+diff f1.txt f2.txt
+'
+
+test_expect_success 'blame using absolute path names' '
+git blame afile >f1.txt &&
+git blame "$(pwd)/afile" >f2.txt &&
+diff f1.txt f2.txt
+'
+
+test_expect_success 'diff using absolute path names' '
+git diff HEAD^ -- "$(pwd)/afile" >f1.txt &&
+git diff HEAD^ -- afile >f2.txt &&
+diff f1.txt f2.txt
+'
+
+test_done
-- 
1.5.3.5.1.gb2df9
Junio C Hamano· Nov 28, 2007, 08:43 UTC · re: Robin Rosenberg · lore

Re: [PATCH] Make Git accept absolute path names for files within the work tree

Robin Rosenberg <robin.rosenberg@dewire.com> writes:
Show 39 quoted lines
> This patch makes it possible to drag files and directories from
> a graphical browser and drop them onto a shell and feed them
> to common git operations without editing away the path to the
> root of the work tree.
>
> Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
> ---
>  setup.c               |   19 +++++++++++++++++
>  t/t3904-abspatharg.sh |   55 +++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 74 insertions(+), 0 deletions(-)
>  create mode 100755 t/t3904-abspatharg.sh
>
> diff --git a/setup.c b/setup.c
> index f512ea0..ffc30bf 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -7,6 +7,25 @@ static int inside_work_tree = -1;
>  const char *prefix_path(const char *prefix, int len, const char *path)
>  {
>  	const char *orig = path;
> +	const char *work_tree = get_git_work_tree();
> +	if (is_absolute_path(path) && work_tree) {
> +		int n = strlen(work_tree);
> +		if (!strncmp(path, work_tree, n) && (path[n] == '/' || !path[n])) {
> +			if (path[n])
> +				path += n + 1;
> +			else
> +				path += n;
> +
> +			if (prefix && !strncmp(path, prefix, len - 1)) {
> +				if (path[len - 1] == '/')
> +					path += len;
> +				else
> +					if (!path[len - 1])
> +						path += len - 1;
> +			}
> +		}
> +	}
> +

This looks somewhat tighter than the previous one, but still made me worried if the caller of prefix_path() has run the setup sequence enough so that calling get_git_work_tree() is safe, so I ended up auditing the callpath. At least, I do not want to see that unconditional call to get_git_work_tree() when we do not need to do this "ah prefix got an unusual absolute path" stuff.

 * builtin-init-db.c uses prefix_path() to find where the template is
   (this is mingw fallout change); in general, I do not think we would
   want to trigger repository nor worktree discovery inside init-db,
   although I suspect this particular callpath could be made Ok (because
   it is taken only when template_dir is not absolute) if you do not
   unconditionally call get_git_work_tree() in prefix_path().
 * config.c uses prefix_path() to find the ETC_GITCONFIG that is not
   absolute (again, mingw fallout).  When git_config() is called, we
   already should have discovered repository but worktree may not have
   been found yet (config.worktree can be used to specify where it is,
   so you have a chicken and egg problem).  Again, this particular
   callpath happens to be Ok because this is used only for non-absolute
   path, but that is a bit subtle.
 * get_pathspec() uses prefix_path() for obvious reasons, and the prefix
   it gets must have been discovered by finding out where the worktree
   is, so by definition that one is safe.

Everybody else you would get from "git grep prefix_path" are after the proper setup, so they should all be safe.

Show 22 quoted lines
> diff --git a/t/t3904-abspatharg.sh b/t/t3904-abspatharg.sh
> new file mode 100755
> index 0000000..cd4a52e
> --- /dev/null
> +++ b/t/t3904-abspatharg.sh
> @@ -0,0 +1,55 @@
> +#!/bin/sh
> +#
> +# Copyright (C) 2007 Robin Rosenberg
> +#
> +
> +test_description='Test absolute filename arguments to various git
> +commands.  Absolute arguments pointing to a location within the git
> +work tree should behave the same as relative arguments.  '
> +
> +. ./test-lib.sh
> +
> +test_expect_success 'add files using absolute path names' '
> +echo a >afile &&
> +echo b >bfile &&
> +git-add afile &&
> +git-add "$(pwd)/bfile" &&

This looks quite dense. With indent like other existing tests this would become a bit easier to read.

> +test "afile bfile" = "$(echo $(git ls-files))"
Hmmm.  Looks a bit ugly but Ok.
Show 7 quoted lines
> +mkdir x &&
> +cd x &&
> +echo c >cfile &&
> +echo d >dfile &&
> +git-add cfile &&
> +git-add "$(pwd)" &&
> +cd .. &&

If this sequence fails in the middle, the next test will execute in a wrong directory. Instead of "cd there && ... && cd .. &&", it is safer to do "( cd there && ... ) &&".

> +test "afile bfile x/cfile x/dfile" = "$(echo $(git ls-files))" &&
> +git ls-files x >f1 &&
> +git ls-files "$(pwd)/x" >f2 &&
> +diff f1 f2

"diff -u" is much easier for people who ends up reading the output when something goes wrong.

Cases that are still not covered:
	try to add a file in the parent directory from a subdirectory,
	using absolute path;
	try to add a file in a directory that is too high (i.e. outside
	the work tree), using absolute path;

The latter I think should fail. People tend to forget writing negative tests when they are too excited with their shiny new features.

Perhaps adding something like:
	try to add a sub-subdirectory using an absolute path, and make
	sure paths in subdirectory are not added;

would also be prudent to catch future possible breakages (off by one cutting the common directory prefix one level too deep or something), but that is probably just me who tends to be paranoid.

Exactly the same comments apply to tests for other commands. Also I think mv, rm, reset and checkout take pathnames.

Robin Rosenberg· Nov 29, 2007, 01:15 UTC · re: Junio C Hamano · lore

Re: [PATCH] Make Git accept absolute path names for files within the work tree

onsdag 28 november 2007 skrev Junio C Hamano:
Show 21 quoted lines
> This looks somewhat tighter than the previous one, but still made me
> worried if the caller of prefix_path() has run the setup sequence enough
> so that calling get_git_work_tree() is safe, so I ended up auditing the
> callpath.  At least, I do not want to see that unconditional call to
> get_git_work_tree() when we do not need to do this "ah prefix got an
> unusual absolute path" stuff.
> 
>  * builtin-init-db.c uses prefix_path() to find where the template is
>    (this is mingw fallout change); in general, I do not think we would
>    want to trigger repository nor worktree discovery inside init-db,
>    although I suspect this particular callpath could be made Ok (because
>    it is taken only when template_dir is not absolute) if you do not
>    unconditionally call get_git_work_tree() in prefix_path().
> 
>  * config.c uses prefix_path() to find the ETC_GITCONFIG that is not
>    absolute (again, mingw fallout).  When git_config() is called, we
>    already should have discovered repository but worktree may not have
>    been found yet (config.worktree can be used to specify where it is,
>    so you have a chicken and egg problem).  Again, this particular
>    callpath happens to be Ok because this is used only for non-absolute
>    path, but that is a bit subtle.

I wonder if this usage in config and initdb is what prefix_path() was intended for. The interface is declared in cache.h and there are error conditions like '%s is outside repository'.

Maybe we should have a boolean indicating whether the arguments refer to filespecs or not to make this clear or rewite the mingw fallouts in some other way.

Show 6 quoted lines
>  * get_pathspec() uses prefix_path() for obvious reasons, and the prefix
>    it gets must have been discovered by finding out where the worktree
>    is, so by definition that one is safe.
> 
> Everybody else you would get from "git grep prefix_path" are after the
> proper setup, so they should all be safe. 

Thanks for looking at the usages. I'll come up with some more tests too, though writing negative tests sometimes is a challenge. Tests all to easily fail for the wrong reason which is bad when we expect them to fail for the right reason.

-- robin
Junio C Hamano· Nov 29, 2007, 02:05 UTC · re: Robin Rosenberg · lore

Re: [PATCH] Make Git accept absolute path names for files within the work tree

Robin Rosenberg <robin.rosenberg@dewire.com> writes:
> Maybe we should have a boolean indicating whether the arguments refer
> to filespecs or not to make this clear or rewite the mingw fallouts in
> some other way.

I wondered the same, but my vote would be to make them separate functions. You would need a function to abstract out pasting of paths to ease MinGW people even without this "ah absolute -- let's make it relative to work tree first" hack, anyway.

Junio C Hamano· Nov 29, 2007, 00:37 UTC · re: Robin Rosenberg · lore

Re: [PATCH] Make Git accept absolute path names for files within the work tree

Robin Rosenberg <robin.rosenberg@dewire.com> writes:
Show 10 quoted lines
> diff --git a/setup.c b/setup.c
> index f512ea0..ffc30bf 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -7,6 +7,25 @@ static int inside_work_tree = -1;
>  const char *prefix_path(const char *prefix, int len, const char *path)
>  {
>  	const char *orig = path;
> +	const char *work_tree = get_git_work_tree();
> +	if (is_absolute_path(path) && work_tree) {

Could you structure this part to read like this, into a separate function:

static const char *strip_work_tree_path(const char *prefix, int len, const char *path)
{
	...
}
const char *prefix_path(const char *prefix, int len, const char *path)
{
	const char *orig = path;
	if (is_absolute_path(path))
		path = strip_work_tree_path(prefix, len, path);
	...

About the part that would be moved out of line with such a restructuring,

Show 14 quoted lines
> +		int n = strlen(work_tree);
> +		if (!strncmp(path, work_tree, n) && (path[n] == '/' || !path[n])) {
> +			if (path[n])
> +				path += n + 1;
> +			else
> +				path += n;
> +
> +			if (prefix && !strncmp(path, prefix, len - 1)) {
> +				if (path[len - 1] == '/')
> +					path += len;
> +				else
> +					if (!path[len - 1])
> +						path += len - 1;
> +			}

This makes me wonder what happens if after stripping the worktree path path does not match the prefix.

Johannes Sixt· Nov 27, 2007, 08:45 UTC · re: Robin Rosenberg · lore

Re: [PATCH] Make Git accept absolute path names for files within the work tree

Robin Rosenberg schrieb:
Show 5 quoted lines
> +test_expect_success 'add files using absolute path names' '
> +echo a >afile &&
> +echo b >bfile &&
> +git-add afile &&
> +git-add $(pwd)/bfile &&

Please use "$(pwd)/bfile" (d-quotes) throughout. Otherwise the test would fail if conducted in a directory with spaces.

-- Hannes
Robin Rosenberg· Nov 27, 2007, 23:14 UTC · re: Johannes Sixt · lore

Re: [PATCH] Make Git accept absolute path names for files within the work tree

tisdag 27 november 2007 skrev Johannes Sixt:
Show 9 quoted lines
> Robin Rosenberg schrieb:
> > +test_expect_success 'add files using absolute path names' '
> > +echo a >afile &&
> > +echo b >bfile &&
> > +git-add afile &&
> > +git-add $(pwd)/bfile &&
> 
> Please use "$(pwd)/bfile" (d-quotes) throughout. Otherwise the test would 
> fail if conducted in a directory with spaces.

AFAIK, None of the tests works in directories with spaces. But I'll fix it, just in case, so noone can blame /me/ for the tests breaking in /my home/. I cannot verify it because the testing infrastructure breaks before the actual tests start executing.

-- robin

← back to recent threads